Contiki-Inga 3.x
adc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
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
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * ADC driver definitions
35  * \author
36  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
37  */
38 
39 /**
40  * \addtogroup inga_sensors_driver
41  * @{
42  */
43 
44 /**
45  * \defgroup adc_driver Analog Digital Converter (internal ADC)
46  *
47  * <p>The ATmega1284p has 8 ADC-channels. You can run the ADC in
48  * different modes like just "single coversion", "free running" or
49  * "auto triggered" by timer or interrupts. Moreover it is possible
50  * to get differential values of two ADC-channels (with or without
51  * a gain factor)</p>
52  * @{
53  *
54  */
55 
56 #ifndef ADCCTR_H_
57 #define ADCCTR_H_
58 
59 #include <avr/io.h>
60 
61 
62 /********************************************************************
63  * ADC mode:
64  ********************************************************************/
65 /**
66  * \name ADC modes
67  * @{ */
68 /*!
69  * mode: ADC Single Conversion
70  * \note The ADC will make just one conversion cycle (normal use)
71  */
72 #define ADC_SINGLE_CONVERSION 0xFF
73 /*!
74  * mode: ADC Free Running Mode
75  * \note The ADC runs all the time. An interrupt occurs whenever one ADC cycle
76  * will finish
77  */
78 #define ADC_FREE_RUNNING 0x00
79 /*!
80  * mode: ADC Analog Comparator
81  * \note Sets the ADC to comparator mode.
82  */
83 #define ADC_ANALOG_COMPARATOR (1 << ADTS0)
84 /*!
85  * mode: External Interrupt Trigger
86  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
87  * changes on external interrupt will occur.
88  */
89 #define ADC_EXT_INTERRUPT (1 << ADTS1)
90 /*!
91  * mode: Timer0 Compare Flag Trigger
92  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
93  * Timer0 Compare Flag is set.
94  */
95 #define ADC_TIMER0_COMP_FLAG ((1 << ADTS1) | (1 << ADTS0))
96 /*!
97  * mode: Timer0 Compare Flag Trigger
98  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
99  * Timer0 overflow occurs.
100  */
101 #define ADC_TIMER0_OVERFLOW (1 << ADTS2)
102 /*!
103  * mode: Timer1 Compare Flag Trigger
104  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
105  * Timer0 Compare Flag is set.
106  */
107 #define ADC_TIMER1_COMP_FLAG ((1 << ADTS2) | (1 << ADTS0))
108 /*!
109  * mode: Timer1 Compare Flag Trigger
110  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
111  * Timer0 overflow occurs.
112  */
113 #define ADC_TIMER1_OVERFLOW ((1 << ADTS2) | (1 << ADTS1))
114 /*!
115  * mode: Timer1 Compare Flag Trigger
116  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
117  * Timer0 Capture Flag is set.
118  */
119 #define ADC_TIMER1_CAPTURE ((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0))
120 /** @} */
121 
122 /********************************************************************
123  * ADC reference voltage source:
124  ********************************************************************/
125 
126 /**
127  * \name ADC reference voltage sources
128  * @{ */
129 /*!
130  * ref: External Reference Voltage (Aref)
131  * \note The ADC reference voltage is provided by the external reference pin Aref
132  */
133 #define ADC_REF_AREF 0
134 /*!
135  * ref: Supply Voltage Reference (AVcc)
136  * \note The ADC reference voltage is provided by the supply voltage.
137  */
138 #define ADC_REF_AVCC (1 << REFS0)
139 /*!
140  * ref: Internal 1.1V Reference
141  * \note The ADC reference voltage is provided by internal 1.1V
142  */
143 #define ADC_REF_1100MV_INT (1 << REFS1)
144 /*!
145  * ref: Internal 2.56V Reference
146  * \note The ADC reference voltage is provided by internal 2.56V
147  */
148 #define ADC_REF_2560MV_INT ((1 << REFS1)| (1 << REFS0))
149 /** @} */
150 
151 
152 /**
153  * \name ADMUX - ADC Multiplexer Selection Register
154  * \{ */
155 #define ADC_CHANNEL_0 0x00
156 #define ADC_CHANNEL_1 0x01
157 #define ADC_CHANNEL_2 0x02
158 #define ADC_CHANNEL_3 0x03
159 #define ADC_CHANNEL_4 0x04
160 #define ADC_CHANNEL_5 0x05
161 #define ADC_CHANNEL_6 0x06
162 #define ADC_CHANNEL_7 0x07
163 
164 #define ADC_DIFF_ADC0_ADC0_10X 0x08
165 #define ADC_DIFF_ADC1_ADC0_10X 0x09
166 #define ADC_DIFF_ADC0_ADC0_200X 0x0A
167 #define ADC_DIFF_ADC1_ADC0_200X 0x0B
168 #define ADC_DIFF_ADC2_ADC2_10X 0x0C
169 #define ADC_DIFF_ADC3_ADC2_10X 0x0D
170 #define ADC_DIFF_ADC2_ADC2_200X 0x0E
171 #define ADC_DIFF_ADC3_ADC2_200X 0x0F
172 #define ADC_DIFF_ADC0_ADC1_1X 0x10
173 #define ADC_DIFF_ADC1_ADC1_1X 0x11
174 #define ADC_DIFF_ADC2_ADC1_1X 0x12
175 #define ADC_DIFF_ADC3_ADC1_1X 0x13
176 #define ADC_DIFF_ADC4_ADC1_1X 0x14
177 #define ADC_DIFF_ADC5_ADC1_1X 0x15
178 #define ADC_DIFF_ADC6_ADC1_1X 0x16
179 #define ADC_DIFF_ADC7_ADC1_1X 0x17
180 #define ADC_DIFF_ADC0_ADC2_1X 0x18
181 #define ADC_DIFF_ADC1_ADC2_1X 0x19
182 #define ADC_DIFF_ADC2_ADC2_1X 0x1A
183 #define ADC_DIFF_ADC3_ADC2_1X 0x1B
184 #define ADC_DIFF_ADC4_ADC2_1X 0x1C
185 /** \} */
186 
187 /*
188  * ADCSRA - ADC control and status register A
189  * \cond
190  */
191 #define ADC_ENABLE (1 << ADEN)
192 
193 #define ADC_START (1 << ADSC)
194 #define ADC_STOP 0x00
195 
196 #define ADC_LEFT_ADJUSTED (1 << ADLAR)
197 #define ADC_RIGHT_ADJUSTED (0 << ADLAR)
198 
199 #define ADC_TRIGGER_ENABLE (1 << ADATE)
200 #define ADC_TRIGGER_DISABLE (0 << ADATE)
201 
202 #define ADC_INTERRUPT_ENABLE (1 << ADIE)
203 #define ADC_INTERRUPT_DISABLE (0 << ADIE)
204 
205 #define ADC_PRESCALE_2 (1 << ADPS0)
206 #define ADC_PRESCALE_4 (1 << ADPS1)
207 #define ADC_PRESCALE_8 ((1 << ADPS0) | (1 << ADPS1))
208 #define ADC_PRESCALE_16 (1 << ADPS2)
209 #define ADC_PRESCALE_32 ((1 << ADPS2) | (1 << ADPS0))
210 #define ADC_PRESCALE_64 ((1 << ADPS2) | (1 << ADPS1)) /*choose this for F_CPU = 8MHZ*/
211 #define ADC_PRESCALE_128 ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
212 /*
213  * \endcond
214  */
215 
216 
217 /**
218  * \brief Initialize the ADC module
219  *
220  * \note This routine performes a pre-check whether the AREF pin
221  * is connected. It aborts initialization if AREF pin is connected but
222  * internal reference is chosen.
223  *
224  * \param mode Select an ADC mode like single conversion or free running, etc.
225  * \param ref The ADC reference voltage source.
226  * \retval 0 succeeded
227  * \retval 1 failed
228  *
229  */
230 void adc_init(uint8_t mode, uint8_t ref);
231 
232 /**
233  * \brief This function returns the ADC data register
234  * value. Use adc_set_mux to preselect the ADC-channel
235  * or differential input.
236  *
237  * \return The ADC-value of preselected mux
238  *
239  */
240 uint16_t adc_get_value(void);
241 
242 /**
243  * \brief This function returns the ADC data register
244  * value of the given ADC channel.
245  *
246  * \param chn The ADC channel (ADC0 ... ADC7)
247  *
248  * \return The ADC-value
249  *
250  * This function is more efficient for the single
251  * conversion mode. Just select the ADC channel (or
252  * differential settings) and get the result.
253  */
254 uint16_t adc_get_value_from(uint8_t chn);
255 
256 /**
257  * \brief With this function you can set the ADX multiplexer.
258  * Here you select the ADC channel or choose differential
259  * value between two ADC-channels with or without gain.
260  *
261  * \param mux Select single ADC channel (ADC_CHANNEL_0 ... ADC_CHANNEL_7)
262  * or differential input of various ADC-channels with or
263  * without gain
264  *
265  */
266 void adc_set_mux(uint8_t mux);
267 
268 /**
269  * \brief This function stops all running ADC
270  *
271  */
272 void adc_deinit(void);
273 
274 /** @} */
275 /** @} */
276 
277 #endif /* ADCCTR_H_ */