root / trunk / code / projects / libdragonfly / analog.c @ 1461
History | View | Annotate | Download (10.6 KB)
1 | 338 | bcoltin | /**
|
---|---|---|---|
2 | * Copyright (c) 2007 Colony Project
|
||
3 | *
|
||
4 | * Permission is hereby granted, free of charge, to any person
|
||
5 | * obtaining a copy of this software and associated documentation
|
||
6 | * files (the "Software"), to deal in the Software without
|
||
7 | * restriction, including without limitation the rights to use,
|
||
8 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
9 | * copies of the Software, and to permit persons to whom the
|
||
10 | * Software is furnished to do so, subject to the following
|
||
11 | * conditions:
|
||
12 | *
|
||
13 | * The above copyright notice and this permission notice shall be
|
||
14 | * included in all copies or substantial portions of the Software.
|
||
15 | *
|
||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||
18 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||
20 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||
21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||
23 | * OTHER DEALINGS IN THE SOFTWARE.
|
||
24 | **/
|
||
25 | |||
26 | /**
|
||
27 | * @file analog.c
|
||
28 | * @brief Analog input and output
|
||
29 | *
|
||
30 | * Contains functions for manipulating the ADC on the Dragonfly board.
|
||
31 | *
|
||
32 | * @author Colony Project, CMU Robotics Club
|
||
33 | 1406 | jykong | * originally taken from fwr analog file (author: Tom Lauwers)
|
34 | * loop code written by Kevin Woo and James Kong
|
||
35 | 338 | bcoltin | **/
|
36 | |||
37 | #include <util/delay.h> |
||
38 | #include <avr/interrupt.h> |
||
39 | #include "analog.h" |
||
40 | #include "serial.h" |
||
41 | // Internal Function Prototypes
|
||
42 | void set_adc_mux(int which); |
||
43 | |||
44 | /**
|
||
45 | * @defgroup analog Analog
|
||
46 | * Functions for manipulation the ADC on the dragonfly board.
|
||
47 | * All definitions may be found in analog.h.
|
||
48 | *
|
||
49 | * @{
|
||
50 | **/
|
||
51 | |||
52 | 1461 | bneuman | unsigned char analog_initd=0; |
53 | |||
54 | 1406 | jykong | volatile int adc_loop_status = ADC_LOOP_STOPPED; |
55 | volatile int adc_sig_stop_loop = 0; |
||
56 | volatile int adc_current_port = 0; |
||
57 | volatile adc_t an_val[11]; |
||
58 | 338 | bcoltin | |
59 | /**
|
||
60 | * Initializes the ADC.
|
||
61 | * Call analog_init before reading from the analog ports.
|
||
62 | *
|
||
63 | 1461 | bneuman | * @return returns 0 on success, nonzero on error
|
64 | *
|
||
65 | 437 | kwoo | * @see analog8, analog10, analog_get8, analog_get10
|
66 | 1406 | jykong | *
|
67 | * @bug First conversion takes a performance penalty of
|
||
68 | * 25 vs. 13 ADC clock cycles of successive conversions.
|
||
69 | * Analog_init should run a dummy conversion to pre-empt
|
||
70 | * this.
|
||
71 | *
|
||
72 | * For good 10-bit precision, ACD clock must be between
|
||
73 | * 50kHz and 200kHz. Currently, ADC clock is fixed at
|
||
74 | * 125kHz using 1/64prescalar. However, most code uses
|
||
75 | * 8-bit precision which can work at ADC clock speeds
|
||
76 | * higher than 200kHz. Experimental tests needed to
|
||
77 | * determine highest clock speed for accurate 8-bit ADC.
|
||
78 | *
|
||
79 | 338 | bcoltin | **/
|
80 | 1461 | bneuman | int analog_init(int start_conversion) { |
81 | if(analog_initd)
|
||
82 | return ERROR_INIT_ALREADY_INITD;
|
||
83 | 338 | bcoltin | |
84 | 1461 | bneuman | for (int i = 0; i < 11; i++) { |
85 | an_val[i].adc10 = 0;
|
||
86 | an_val[i].adc8 = 0;
|
||
87 | } |
||
88 | 338 | bcoltin | |
89 | 1461 | bneuman | // ADMUX register
|
90 | // Bit 7,6 - Set voltage reference to AVcc (0b01)
|
||
91 | // Bit 5 - ADLAR set to simplify moving from register
|
||
92 | // Bit 4 - X
|
||
93 | // Bit 3:0 - Sets the current channel
|
||
94 | // Initializes to read from AN1 first (AN0 is reservered for the BOM)
|
||
95 | ADMUX = 0;
|
||
96 | ADMUX |= ADMUX_OPT | _BV(MUX0); |
||
97 | |||
98 | // ADC Status Register A
|
||
99 | // Bit 7 - ADEN is set (enables analog)
|
||
100 | // Bit 6 - Start conversion bit is set (must be done once for free-running mode)
|
||
101 | // Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
|
||
102 | // Bit 4 - ADC interrupt flag, 0
|
||
103 | // Bit 3 - Enable ADC Interrupt (required to run free-running mode)
|
||
104 | // Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
|
||
105 | ADCSRA = 0;
|
||
106 | ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); |
||
107 | 338 | bcoltin | |
108 | 1461 | bneuman | // Set external mux lines to outputs
|
109 | DDRG |= 0x1C;
|
||
110 | 338 | bcoltin | |
111 | 1461 | bneuman | // Set up first port for conversions
|
112 | set_adc_mux(0x00);
|
||
113 | adc_current_port = AN1; |
||
114 | 338 | bcoltin | |
115 | 1461 | bneuman | //Start the conversion loop if requested
|
116 | if (start_conversion)
|
||
117 | analog_start_loop(); |
||
118 | 1406 | jykong | |
119 | 1461 | bneuman | //Conversion loop disabled by default
|
120 | |||
121 | analog_initd=1;
|
||
122 | return 0; |
||
123 | 338 | bcoltin | } |
124 | |||
125 | 862 | kwoo | /**
|
126 | * Returns the 8-bit analog conversion of which from
|
||
127 | * the lookup table. If the requested port is the BOM_PORT
|
||
128 | * you will get an automatic 0 since the BOM_PORT is not
|
||
129 | * read in the loop and not stored. If you need that port
|
||
130 | * you should use the functions in bom.c. There is an analog_get8
|
||
131 | * function which for instant lookups but should be avoided unless
|
||
132 | * you know what you're doing.
|
||
133 | *
|
||
134 | * @param which the port that you want to read
|
||
135 | *
|
||
136 | * @bug may cause a seg fault if which is a larger value
|
||
137 | * than exists in an_val table. Not sure if we should fix
|
||
138 | * this or not since it would add overhead.
|
||
139 | *
|
||
140 | * @return 8-bit analog value for the which port requested
|
||
141 | *
|
||
142 | * @see analog10, analog_get8, analog_get10
|
||
143 | **/
|
||
144 | 338 | bcoltin | unsigned int analog8(int which) { |
145 | if (which == BOM_PORT) {
|
||
146 | return 0; |
||
147 | } else {
|
||
148 | return an_val[which - 1].adc8; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | 437 | kwoo | /**
|
153 | * Returns the 10-bit analog conversion of which from
|
||
154 | * the lookup table. If the requested port is the BOM_PORT
|
||
155 | * you will get an automatic 0 since the BOM_PORT is not
|
||
156 | * read in the loop and not stored. If you need that port
|
||
157 | * you should use the functions in bom.c. There is an analog_get10
|
||
158 | 862 | kwoo | * function which for instant lookups but should be avoided unless
|
159 | * you know what you are doing.
|
||
160 | 437 | kwoo | *
|
161 | * @param which the port that you want to read
|
||
162 | *
|
||
163 | * @bug may cause a seg fault if which is a larger value
|
||
164 | * than exists in an_val table. Not sure if we should fix
|
||
165 | * this or not since it would add overhead.
|
||
166 | *
|
||
167 | * @return 10-bit analog value for the which port requested
|
||
168 | *
|
||
169 | * @see analog8, analog_get8, analog_get10
|
||
170 | **/
|
||
171 | 338 | bcoltin | unsigned int analog10(int which) { |
172 | if (which == BOM_PORT) {
|
||
173 | return 0; |
||
174 | } else {
|
||
175 | return an_val[which - 1].adc10; |
||
176 | } |
||
177 | } |
||
178 | |||
179 | 437 | kwoo | /**
|
180 | * Starts the analog update loop. Will continue to run
|
||
181 | * until analog_stop_loop is called.
|
||
182 | *
|
||
183 | 1461 | bneuman | * @return returns 0 on success, nonzero on error
|
184 | *
|
||
185 | 1406 | jykong | * @see analog_stop_loop, analog_loop_status
|
186 | 437 | kwoo | **/
|
187 | 1461 | bneuman | int analog_start_loop(void) { |
188 | if(!analog_initd)
|
||
189 | return ERROR_LIBRARY_NOT_INITD;
|
||
190 | |||
191 | if(adc_loop_status != ADC_LOOP_RUNNING){
|
||
192 | //Start the conversion, enable ADC interrupt
|
||
193 | ADCSRA |= _BV(ADIE); |
||
194 | ADCSRA |= _BV(ADSC); |
||
195 | adc_loop_status = ADC_LOOP_RUNNING; |
||
196 | } |
||
197 | |||
198 | return 0; |
||
199 | 338 | bcoltin | } |
200 | |||
201 | 437 | kwoo | /**
|
202 | * Stops the analog update loop. If there is a current
|
||
203 | * read, it will finish up and be stored before the loop
|
||
204 | * is interrupted. No further updates will be made until
|
||
205 | * the loop is started again.
|
||
206 | *
|
||
207 | 1461 | bneuman | * @return returns 0 on success, nonzero on error
|
208 | *
|
||
209 | 1406 | jykong | * @see analog_start_loop, analog_loop_status
|
210 | 437 | kwoo | **/
|
211 | 1461 | bneuman | int analog_stop_loop() {
|
212 | if(!analog_initd)
|
||
213 | return ERROR_LIBRARY_NOT_INITD;
|
||
214 | |||
215 | //Signal to stop after the next conversion
|
||
216 | adc_sig_stop_loop = 1;
|
||
217 | |||
218 | return 0; |
||
219 | 338 | bcoltin | } |
220 | 1406 | jykong | |
221 | 338 | bcoltin | /**
|
222 | 1406 | jykong | * Returns the status of loop. 0 for stopped.
|
223 | * 1 for running. 2 for paused.
|
||
224 | *
|
||
225 | * @see analog_start_loop, analog_stop_loop
|
||
226 | **/
|
||
227 | int analog_loop_status(void) { |
||
228 | return adc_loop_status;
|
||
229 | } |
||
230 | |||
231 | /**
|
||
232 | 862 | kwoo | * Reads an 8-bit number from an analog port.
|
233 | 338 | bcoltin | * analog_init must be called before using this function.
|
234 | 862 | kwoo | * The analog loop must also be stopped before using this
|
235 | * function or you will mess up the lookup table. You
|
||
236 | * must also reenabled the loop when you are done unless
|
||
237 | * you are doing more instant reads. See analog_stop_loop
|
||
238 | * and analog_start_loop for more information about the loop.
|
||
239 | 338 | bcoltin | *
|
240 | * @param which the analog port to read from. One of
|
||
241 | * the constants AN0 - AN7.
|
||
242 | *
|
||
243 | 862 | kwoo | * @return the 8-bit input to the specified port
|
244 | 338 | bcoltin | *
|
245 | 862 | kwoo | * @see analog_init, analog_get10, analog8, analog_stop_loop,
|
246 | * analog_start_loop
|
||
247 | 338 | bcoltin | **/
|
248 | 439 | kwoo | unsigned int analog_get8(int which) { |
249 | 338 | bcoltin | // Let any previous conversion finish
|
250 | while (ADCSRA & _BV(ADSC));
|
||
251 | |||
252 | if(which < EXT_MUX) {
|
||
253 | ADMUX = ADMUX_OPT + which; |
||
254 | } else {
|
||
255 | ADMUX = ADMUX_OPT + EXT_MUX; |
||
256 | set_adc_mux(which - 8);
|
||
257 | } |
||
258 | |||
259 | // Start the conversion
|
||
260 | ADCSRA |= _BV(ADSC); |
||
261 | |||
262 | // Wait for the conversion to finish
|
||
263 | while (ADCSRA & _BV(ADSC));
|
||
264 | |||
265 | return ADCH; //since we left aligned the data, ADCH is the 8 MSB. |
||
266 | } |
||
267 | |||
268 | /**
|
||
269 | 862 | kwoo | * Reads an 10-bit number from an analog port.
|
270 | 338 | bcoltin | * analog_init must be called before using this function.
|
271 | 862 | kwoo | * The analog loop must also be stopped before using this
|
272 | * function or you will mess up the lookup table. You
|
||
273 | * must also reenabled the loop when you are done unless
|
||
274 | * you are doing more instant reads. See analog_stop_loop
|
||
275 | * and analog_start_loop for more information about the loop.
|
||
276 | 338 | bcoltin | *
|
277 | *
|
||
278 | * @param which the analog port to read from. Typically
|
||
279 | * a constant, one of AN0 - AN7.
|
||
280 | *
|
||
281 | 862 | kwoo | * @return the 10-bit number input to the specified port
|
282 | 338 | bcoltin | *
|
283 | 862 | kwoo | * @see analog_init, analog_get8, analog10, analog_stop_loop,
|
284 | * analog_start_loop
|
||
285 | 338 | bcoltin | **/
|
286 | 439 | kwoo | unsigned int analog_get10(int which) { |
287 | 338 | bcoltin | int adc_h;
|
288 | int adc_l;
|
||
289 | |||
290 | // Let any previous conversion finish
|
||
291 | while (ADCSRA & _BV(ADSC));
|
||
292 | |||
293 | if(which < EXT_MUX) {
|
||
294 | ADMUX = ADMUX_OPT + which; |
||
295 | } else {
|
||
296 | ADMUX = ADMUX_OPT + EXT_MUX; |
||
297 | set_adc_mux(which - 8);
|
||
298 | } |
||
299 | |||
300 | // Start the conversion
|
||
301 | ADCSRA |= _BV(ADSC); |
||
302 | |||
303 | // Wait for the conversion to finish
|
||
304 | while (ADCSRA & _BV(ADSC));
|
||
305 | |||
306 | adc_l = ADCL; |
||
307 | adc_h = ADCH; |
||
308 | |||
309 | return ((adc_h << 2) | (adc_l >> 6)); |
||
310 | } |
||
311 | |||
312 | /**
|
||
313 | * Returns the current position of the wheel, as an integer
|
||
314 | * in the range 0 - 255.
|
||
315 | * analog_init must be called before using this function.
|
||
316 | *
|
||
317 | * @return the orientation of the wheel, as an integer in
|
||
318 | * the range 0 - 255.
|
||
319 | *
|
||
320 | * @see analog_init
|
||
321 | **/
|
||
322 | 439 | kwoo | int wheel(void) { |
323 | 338 | bcoltin | return analog8(WHEEL_PORT);
|
324 | } |
||
325 | |||
326 | |||
327 | /**
|
||
328 | * Sets the value of the external analog mux. Values are read
|
||
329 | * on AN7 physical port. (AN8 - AN15 are "virtual" ports).
|
||
330 | *
|
||
331 | * @param which which analog mux port (0-7) which corresponds
|
||
332 | * to AN8-AN15.
|
||
333 | *
|
||
334 | * @bug FIX THIS IN THE NEXT BOARD REVISION:
|
||
335 | * ADDR2 ADDR1 ADDR0
|
||
336 | * G2.G4.G3 set mux to port 0-7 via vinary selection
|
||
337 | * math would be much cleaner if it was G4.G3.G2
|
||
338 | *
|
||
339 | * @see analog_init
|
||
340 | **/
|
||
341 | 439 | kwoo | void set_adc_mux(int which) { |
342 | 338 | bcoltin | // mask so only proper bits are possible.
|
343 | PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04); |
||
344 | } |
||
345 | |||
346 | /**@}**/ //end defgroup |
||
347 | |||
348 | |||
349 | ISR(ADC_vect) { |
||
350 | int adc_h = 0; |
||
351 | int adc_l = 0; |
||
352 | 1406 | jykong | |
353 | if(adc_loop_status != ADC_LOOP_RUNNING) return; |
||
354 | 338 | bcoltin | |
355 | //Store the value only if this read isn't for the BOM
|
||
356 | if (ADMUX != BOM_PORT) {
|
||
357 | adc_l = ADCL; |
||
358 | adc_h = ADCH; |
||
359 | |||
360 | an_val[adc_current_port - 1].adc10 = (adc_h << 2) | (adc_l >> 6); |
||
361 | an_val[adc_current_port - 1].adc8 = adc_h;
|
||
362 | } |
||
363 | |||
364 | //Skip AN7 because it is not a real port
|
||
365 | if (adc_current_port == AN6) {
|
||
366 | ADMUX = ADMUX_OPT | EXT_MUX; |
||
367 | set_adc_mux(AN8 - 8);
|
||
368 | adc_current_port = AN8; |
||
369 | //Wrap around
|
||
370 | } else if (adc_current_port == AN11) { |
||
371 | adc_current_port = AN1; |
||
372 | ADMUX = ADMUX_OPT | adc_current_port; |
||
373 | //Normal increment
|
||
374 | } else {
|
||
375 | adc_current_port++; |
||
376 | |||
377 | if(adc_current_port < EXT_MUX) {
|
||
378 | ADMUX = ADMUX_OPT | adc_current_port; |
||
379 | } else {
|
||
380 | ADMUX = ADMUX_OPT | EXT_MUX; |
||
381 | set_adc_mux(adc_current_port - 8);
|
||
382 | } |
||
383 | } |
||
384 | |||
385 | 1406 | jykong | //Stop loop if signal is set
|
386 | if(adc_sig_stop_loop) {
|
||
387 | adc_sig_stop_loop = 0;
|
||
388 | adc_loop_status = ADC_LOOP_STOPPED; |
||
389 | 338 | bcoltin | return;
|
390 | 437 | kwoo | } |
391 | 1406 | jykong | |
392 | //Start next conversion
|
||
393 | ADCSRA |= _BV(ADSC); |
||
394 | 338 | bcoltin | } |