root / branches / analog / code / projects / libdragonfly / analog.c @ 1384
History | View | Annotate | Download (10.6 KB)
1 |
/**
|
---|---|
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 |
* originally taken from fwr analog file (author: Tom Lauwers)
|
34 |
* loop code written by Kevin Woo and James Kong
|
35 |
**/
|
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 |
int adc_loop_running = 0; |
53 |
int adc_current_port = 0; |
54 |
adc_t an_val[11];
|
55 |
|
56 |
/**
|
57 |
* Initializes the ADC.
|
58 |
* Call analog_init before reading from the analog ports.
|
59 |
*
|
60 |
* @see analog8, analog10, analog_get8, analog_get10
|
61 |
*
|
62 |
* @bug First conversion takes a performance penalty of
|
63 |
* 25 vs. 13 ADC clock cycles of successive conversions.
|
64 |
* Analog_init should run a dummy conversion to pre-empt
|
65 |
* this.
|
66 |
*
|
67 |
* For good 10-bit precision, ACD clock must be between
|
68 |
* 50kHz and 200kHz. Currently, ADC clock is fixed at
|
69 |
* 125kHz using 1/64prescalar. However, most code uses
|
70 |
* 8-bit precision which can work at ADC clock speeds
|
71 |
* higher than 200kHz. Experimental tests needed to
|
72 |
* determine highest clock speed for accurate 8-bit ADC.
|
73 |
*
|
74 |
**/
|
75 |
void analog_init(int start_conversion) { |
76 |
for (int i = 0; i < 11; i++) { |
77 |
an_val[i].adc10 = 0;
|
78 |
an_val[i].adc8 = 0;
|
79 |
} |
80 |
|
81 |
// ADMUX register
|
82 |
// Bit 7,6 - Set voltage reference to AVcc (0b01)
|
83 |
// Bit 5 - ADLAR set to simplify moving from register
|
84 |
// Bit 4 - X
|
85 |
// Bit 3:0 - Sets the current channel
|
86 |
// Initializes to read from AN1 first (AN0 is reservered for the BOM)
|
87 |
ADMUX = 0;
|
88 |
ADMUX |= ADMUX_OPT | _BV(MUX0); |
89 |
|
90 |
// ADC Status Register A
|
91 |
// Bit 7 - ADEN is set (enables analog)
|
92 |
// Bit 6 - Start conversion bit is set (must be done once for free-running mode)
|
93 |
// Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
|
94 |
// Bit 4 - ADC interrupt flag, 0
|
95 |
// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
|
96 |
// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
|
97 |
ADCSRA = 0;
|
98 |
ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); |
99 |
|
100 |
// Set external mux lines to outputs
|
101 |
DDRG |= 0x1C;
|
102 |
|
103 |
// Set up first port for conversions
|
104 |
set_adc_mux(0x00);
|
105 |
adc_current_port = AN1; |
106 |
|
107 |
//Start the conversion if requested
|
108 |
if (start_conversion)
|
109 |
analog_start_loop(); |
110 |
|
111 |
//Conversion loop disabled by default
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Returns the 8-bit analog conversion of which from
|
116 |
* the lookup table. If the requested port is the BOM_PORT
|
117 |
* you will get an automatic 0 since the BOM_PORT is not
|
118 |
* read in the loop and not stored. If you need that port
|
119 |
* you should use the functions in bom.c. There is an analog_get8
|
120 |
* function which for instant lookups but should be avoided unless
|
121 |
* you know what you're doing.
|
122 |
*
|
123 |
* @param which the port that you want to read
|
124 |
*
|
125 |
* @bug may cause a seg fault if which is a larger value
|
126 |
* than exists in an_val table. Not sure if we should fix
|
127 |
* this or not since it would add overhead.
|
128 |
*
|
129 |
* @return 8-bit analog value for the which port requested
|
130 |
*
|
131 |
* @see analog10, analog_get8, analog_get10
|
132 |
**/
|
133 |
unsigned int analog8(int which) { |
134 |
if (which == BOM_PORT) {
|
135 |
return 0; |
136 |
} else {
|
137 |
return an_val[which - 1].adc8; |
138 |
} |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Returns the 10-bit analog conversion of which from
|
143 |
* the lookup table. If the requested port is the BOM_PORT
|
144 |
* you will get an automatic 0 since the BOM_PORT is not
|
145 |
* read in the loop and not stored. If you need that port
|
146 |
* you should use the functions in bom.c. There is an analog_get10
|
147 |
* function which for instant lookups but should be avoided unless
|
148 |
* you know what you are doing.
|
149 |
*
|
150 |
* @param which the port that you want to read
|
151 |
*
|
152 |
* @bug may cause a seg fault if which is a larger value
|
153 |
* than exists in an_val table. Not sure if we should fix
|
154 |
* this or not since it would add overhead.
|
155 |
*
|
156 |
* @return 10-bit analog value for the which port requested
|
157 |
*
|
158 |
* @see analog8, analog_get8, analog_get10
|
159 |
**/
|
160 |
unsigned int analog10(int which) { |
161 |
if (which == BOM_PORT) {
|
162 |
return 0; |
163 |
} else {
|
164 |
return an_val[which - 1].adc10; |
165 |
} |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Starts the analog update loop. Will continue to run
|
170 |
* until analog_stop_loop is called.
|
171 |
*
|
172 |
* @see analog_stop_loop, analog_loop_running
|
173 |
**/
|
174 |
void analog_start_loop(void) { |
175 |
//Start the conversion, enable ADC interrupt
|
176 |
ADCSRA |= _BV(ADSC) | _BV(ADIE); |
177 |
adc_loop_running = 0x1;
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Stops the analog update loop. If there is a current
|
182 |
* read, it will finish up and be stored before the loop
|
183 |
* is interrupted. No further updates will be made until
|
184 |
* the loop is started again.
|
185 |
*
|
186 |
* @param Whether or not to block until current conversion
|
187 |
* to complete. Use WAIT_UNTIL_FINISHED or
|
188 |
* SKIP_WAIT.
|
189 |
*
|
190 |
* @see analog_start_loop, analog_loop_running
|
191 |
**/
|
192 |
void analog_stop_loop(int skip_wait) { |
193 |
//Stop the conversion
|
194 |
adc_loop_running = 0x0;
|
195 |
|
196 |
if(skip_wait) {
|
197 |
//Stop the interrupt
|
198 |
ADCSRA &= _BV(ADIE); |
199 |
return;
|
200 |
} |
201 |
|
202 |
//Wait for the ADC interrupt flag to clear / conversion to complete
|
203 |
while(ADCSRA & _BV(ADIF));
|
204 |
//Stop the interrupt
|
205 |
ADCSRA &= _BV(ADIE); |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Returns the status of loop. 1 for running.
|
210 |
* 0 for not running.
|
211 |
*
|
212 |
* @see analog_start_loop, analog_stop_loop
|
213 |
*
|
214 |
* @bug Doesn't actually tell you whether or
|
215 |
* not the loop has fully stopped. adc_loop_running
|
216 |
* is a signalling variable to tell the ISR to stop.
|
217 |
* need to differentiate the signal from the status.
|
218 |
**/
|
219 |
int analog_loop_running(void) { |
220 |
return adc_loop_running;
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* Reads an 8-bit number from an analog port.
|
225 |
* analog_init must be called before using this function.
|
226 |
* The analog loop must also be stopped before using this
|
227 |
* function or you will mess up the lookup table. You
|
228 |
* must also reenabled the loop when you are done unless
|
229 |
* you are doing more instant reads. See analog_stop_loop
|
230 |
* and analog_start_loop for more information about the loop.
|
231 |
*
|
232 |
* @param which the analog port to read from. One of
|
233 |
* the constants AN0 - AN7.
|
234 |
*
|
235 |
* @return the 8-bit input to the specified port
|
236 |
*
|
237 |
* @see analog_init, analog_get10, analog8, analog_stop_loop,
|
238 |
* analog_start_loop
|
239 |
**/
|
240 |
unsigned int analog_get8(int which) { |
241 |
// Let any previous conversion finish
|
242 |
while (ADCSRA & _BV(ADSC));
|
243 |
|
244 |
if(which < EXT_MUX) {
|
245 |
ADMUX = ADMUX_OPT + which; |
246 |
} else {
|
247 |
ADMUX = ADMUX_OPT + EXT_MUX; |
248 |
set_adc_mux(which - 8);
|
249 |
} |
250 |
|
251 |
// Start the conversion
|
252 |
ADCSRA |= _BV(ADSC); |
253 |
|
254 |
// Wait for the conversion to finish
|
255 |
while (ADCSRA & _BV(ADSC));
|
256 |
|
257 |
return ADCH; //since we left aligned the data, ADCH is the 8 MSB. |
258 |
} |
259 |
|
260 |
/**
|
261 |
* Reads an 10-bit number from an analog port.
|
262 |
* analog_init must be called before using this function.
|
263 |
* The analog loop must also be stopped before using this
|
264 |
* function or you will mess up the lookup table. You
|
265 |
* must also reenabled the loop when you are done unless
|
266 |
* you are doing more instant reads. See analog_stop_loop
|
267 |
* and analog_start_loop for more information about the loop.
|
268 |
*
|
269 |
*
|
270 |
* @param which the analog port to read from. Typically
|
271 |
* a constant, one of AN0 - AN7.
|
272 |
*
|
273 |
* @return the 10-bit number input to the specified port
|
274 |
*
|
275 |
* @see analog_init, analog_get8, analog10, analog_stop_loop,
|
276 |
* analog_start_loop
|
277 |
**/
|
278 |
unsigned int analog_get10(int which) { |
279 |
int adc_h;
|
280 |
int adc_l;
|
281 |
|
282 |
// Let any previous conversion finish
|
283 |
while (ADCSRA & _BV(ADSC));
|
284 |
|
285 |
if(which < EXT_MUX) {
|
286 |
ADMUX = ADMUX_OPT + which; |
287 |
} else {
|
288 |
ADMUX = ADMUX_OPT + EXT_MUX; |
289 |
set_adc_mux(which - 8);
|
290 |
} |
291 |
|
292 |
// Start the conversion
|
293 |
ADCSRA |= _BV(ADSC); |
294 |
|
295 |
// Wait for the conversion to finish
|
296 |
while (ADCSRA & _BV(ADSC));
|
297 |
|
298 |
adc_l = ADCL; |
299 |
adc_h = ADCH; |
300 |
|
301 |
return ((adc_h << 2) | (adc_l >> 6)); |
302 |
} |
303 |
|
304 |
/**
|
305 |
* Returns the current position of the wheel, as an integer
|
306 |
* in the range 0 - 255.
|
307 |
* analog_init must be called before using this function.
|
308 |
*
|
309 |
* @return the orientation of the wheel, as an integer in
|
310 |
* the range 0 - 255.
|
311 |
*
|
312 |
* @see analog_init
|
313 |
**/
|
314 |
int wheel(void) { |
315 |
return analog8(WHEEL_PORT);
|
316 |
} |
317 |
|
318 |
|
319 |
/**
|
320 |
* Sets the value of the external analog mux. Values are read
|
321 |
* on AN7 physical port. (AN8 - AN15 are "virtual" ports).
|
322 |
*
|
323 |
* @param which which analog mux port (0-7) which corresponds
|
324 |
* to AN8-AN15.
|
325 |
*
|
326 |
* @bug FIX THIS IN THE NEXT BOARD REVISION:
|
327 |
* ADDR2 ADDR1 ADDR0
|
328 |
* G2.G4.G3 set mux to port 0-7 via vinary selection
|
329 |
* math would be much cleaner if it was G4.G3.G2
|
330 |
*
|
331 |
* @see analog_init
|
332 |
**/
|
333 |
void set_adc_mux(int which) { |
334 |
// mask so only proper bits are possible.
|
335 |
PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04); |
336 |
} |
337 |
|
338 |
/**@}**/ //end defgroup |
339 |
|
340 |
|
341 |
ISR(ADC_vect) { |
342 |
static volatile int adc_prev_loop_running = 0; |
343 |
int adc_h = 0; |
344 |
int adc_l = 0; |
345 |
|
346 |
//Store the value only if this read isn't for the BOM
|
347 |
if (ADMUX != BOM_PORT) {
|
348 |
adc_l = ADCL; |
349 |
adc_h = ADCH; |
350 |
|
351 |
an_val[adc_current_port - 1].adc10 = (adc_h << 2) | (adc_l >> 6); |
352 |
an_val[adc_current_port - 1].adc8 = adc_h;
|
353 |
} |
354 |
|
355 |
//Save the result only if we just turned off the loop
|
356 |
if (!adc_loop_running && !adc_prev_loop_running)
|
357 |
return;
|
358 |
|
359 |
adc_prev_loop_running = adc_loop_running; |
360 |
|
361 |
//Skip AN7 because it is not a real port
|
362 |
if (adc_current_port == AN6) {
|
363 |
ADMUX = ADMUX_OPT | EXT_MUX; |
364 |
set_adc_mux(AN8 - 8);
|
365 |
adc_current_port = AN8; |
366 |
//Wrap around
|
367 |
} else if (adc_current_port == AN11) { |
368 |
adc_current_port = AN1; |
369 |
ADMUX = ADMUX_OPT | adc_current_port; |
370 |
//Normal increment
|
371 |
} else {
|
372 |
adc_current_port++; |
373 |
|
374 |
if(adc_current_port < EXT_MUX) {
|
375 |
ADMUX = ADMUX_OPT | adc_current_port; |
376 |
} else {
|
377 |
ADMUX = ADMUX_OPT | EXT_MUX; |
378 |
set_adc_mux(adc_current_port - 8);
|
379 |
} |
380 |
} |
381 |
|
382 |
//Initiate next conversion only if we are running a loop
|
383 |
if (!adc_loop_running) {
|
384 |
return;
|
385 |
} else {
|
386 |
ADCSRA |= _BV(ADSC); |
387 |
} |
388 |
|
389 |
return;
|
390 |
} |
391 |
|