Contiki 2.5
adc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in
12  * the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of the copyright holders nor the names of
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \file
32  *
33  * \brief
34  * Functions to control the ADC of the MCU. This is used to read the
35  * joystick.
36  *
37  * \author
38  * Mike Vidales mavida404@gmail.com
39  *
40  */
41 
42 #include "adc.h"
43 
44 /**
45  * \addtogroup lcd
46  * \{
47 */
48 
49 static bool adc_initialized;
50 static bool adc_conversion_started;
51 
52 /*---------------------------------------------------------------------------*/
53 
54 /**
55  * \brief This function will init the ADC with the following parameters.
56  *
57  * \param chan Determines the ADC channel to open.
58  * \param trig Sets what type of trigger is needed.
59  * \param ref Sets the proper reference voltage.
60  * \param prescale Sets the prescale to be used against the XTAL choice.
61  *
62  * \return 0
63 */
64 int
65 adc_init(adc_chan_t chan, adc_trig_t trig, adc_ref_t ref, adc_ps_t prescale)
66 {
67  /* Enable ADC module */
68  PRR &= ~(1 << PRADC);
69 
70  /* Configure */
71  ADCSRA = (1<<ADEN)|prescale;
72  ADMUX = (uint8_t)ref|(uint8_t)chan;
73  ADCSRB = trig;
74 
75  adc_initialized = true;
76  adc_conversion_started = false;
77 
78  return 0;
79 }
80 
81 /*---------------------------------------------------------------------------*/
82 
83 /**
84  * \brief This will disable the adc.
85 */
86 void
88 {
89  /* Disable ADC */
90  ADCSRA &= ~(1<<ADEN);
91  PRR |= (1 << PRADC);
92 
93  adc_initialized = false;
94  adc_conversion_started = false;
95 }
96 
97 /*---------------------------------------------------------------------------*/
98 
99 /**
100  * \brief This will start an ADC conversion
101  *
102  * \return 0
103 */
104 int
106 {
107  if (adc_initialized == false){
108  return EOF;
109  }
110  adc_conversion_started = true;
111  ADCSRA |= (1<<ADSC);
112  return 0;
113 }
114 
115 /*---------------------------------------------------------------------------*/
116 
117 /**
118  * \brief This will read the ADC result during the ADC conversion and return
119  * the raw ADC conversion result.
120  *
121  * \param adjust This will Left or Right Adjust the ADC conversion result.
122  *
123  * \return ADC raw 16-byte ADC conversion result.
124 */
125 int16_t
127 {
128  if (adc_conversion_started == false){
129  return EOF;
130  }
131  if (ADCSRA & (1<<ADSC)){
132  return EOF;
133  }
134  adc_conversion_started = false;
135  ADMUX |= (adjust<<ADLAR);
136  return (int16_t)ADC;
137 }
138 
139 /** \} */