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