root / trunk / code / projects / libdragonfly / analog.c @ 491
History | View | Annotate | Download (7.91 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 |
* code mostly taken from fwr analog file (author: Tom Lauwers)
|
34 |
**/
|
35 |
|
36 |
#include <util/delay.h> |
37 |
#include <avr/interrupt.h> |
38 |
#include "analog.h" |
39 |
#include "serial.h" |
40 |
// Internal Function Prototypes
|
41 |
void set_adc_mux(int which); |
42 |
|
43 |
/**
|
44 |
* @defgroup analog Analog
|
45 |
* Functions for manipulation the ADC on the dragonfly board.
|
46 |
* All definitions may be found in analog.h.
|
47 |
*
|
48 |
* @{
|
49 |
**/
|
50 |
|
51 |
int adc_loop_running = 0; |
52 |
int adc_current_port = 0; |
53 |
adc_t an_val[11];
|
54 |
|
55 |
/**
|
56 |
* Initializes the ADC.
|
57 |
* Call analog_init before reading from the analog ports.
|
58 |
*
|
59 |
* @see analog8, analog10, analog_get8, analog_get10
|
60 |
**/
|
61 |
void analog_init(int start_conversion) { |
62 |
for (int i = 0; i < 11; i++) { |
63 |
an_val[i].adc10 = 0;
|
64 |
an_val[i].adc8 = 0;
|
65 |
} |
66 |
|
67 |
//cli();
|
68 |
// ADMUX register
|
69 |
// Bit 7,6 - Set voltage reference to AVcc (0b01)
|
70 |
// Bit 5 - ADLAR set to simplify moving from register
|
71 |
// Bit 4 - X
|
72 |
// Bit 3:0 - Sets the current channel
|
73 |
// Initializes to read from AN1 first (AN0 is reservered for the BOM)
|
74 |
ADMUX = 0;
|
75 |
ADMUX |= ADMUX_OPT | _BV(MUX0); |
76 |
|
77 |
|
78 |
// ADC Status Register A
|
79 |
// Bit 7 - ADEN is set (enables analog)
|
80 |
// Bit 6 - Start conversion bit is set (must be done once for free-running mode)
|
81 |
// Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
|
82 |
// Bit 4 - ADC interrupt flag, 0
|
83 |
// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
|
84 |
// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
|
85 |
ADCSRA = 0;
|
86 |
ADCSRA |= _BV(ADEN) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); |
87 |
|
88 |
// Set external mux lines to outputs
|
89 |
DDRG |= 0x1C;
|
90 |
|
91 |
// Set up first port for conversions
|
92 |
set_adc_mux(0x00);
|
93 |
adc_current_port = AN1; |
94 |
|
95 |
//Start the conversion if requested
|
96 |
if (start_conversion)
|
97 |
analog_start_loop(); |
98 |
else
|
99 |
analog_stop_loop(); |
100 |
//sei();
|
101 |
|
102 |
} |
103 |
|
104 |
unsigned int analog8(int which) { |
105 |
if (which == BOM_PORT) {
|
106 |
return 0; |
107 |
} else {
|
108 |
return an_val[which - 1].adc8; |
109 |
} |
110 |
} |
111 |
|
112 |
/**
|
113 |
* Returns the 10-bit analog conversion of which from
|
114 |
* the lookup table. If the requested port is the BOM_PORT
|
115 |
* you will get an automatic 0 since the BOM_PORT is not
|
116 |
* read in the loop and not stored. If you need that port
|
117 |
* you should use the functions in bom.c. There is an analog_get10
|
118 |
* function which for instant lookups but should be avoided.
|
119 |
*
|
120 |
* @param which the port that you want to read
|
121 |
*
|
122 |
* @bug may cause a seg fault if which is a larger value
|
123 |
* than exists in an_val table. Not sure if we should fix
|
124 |
* this or not since it would add overhead.
|
125 |
*
|
126 |
* @return 10-bit analog value for the which port requested
|
127 |
*
|
128 |
* @see analog8, analog_get8, analog_get10
|
129 |
**/
|
130 |
unsigned int analog10(int which) { |
131 |
if (which == BOM_PORT) {
|
132 |
return 0; |
133 |
} else {
|
134 |
return an_val[which - 1].adc10; |
135 |
} |
136 |
} |
137 |
|
138 |
|
139 |
/**
|
140 |
* Starts the analog update loop. Will continue to run
|
141 |
* until analog_stop_loop is called.
|
142 |
*
|
143 |
* @see analog_stop_loop
|
144 |
**/
|
145 |
void analog_start_loop(void) { |
146 |
//Start the conversion
|
147 |
ADCSRA |= _BV(ADSC); |
148 |
adc_loop_running = 0x1;
|
149 |
} |
150 |
|
151 |
/**
|
152 |
* Stops the analog update loop. If there is a current
|
153 |
* read, it will finish up and be stored before the loop
|
154 |
* is interrupted. No further updates will be made until
|
155 |
* the loop is started again.
|
156 |
*
|
157 |
* @see analog_start_loop
|
158 |
**/
|
159 |
void analog_stop_loop(void) { |
160 |
//Stop the conversion
|
161 |
adc_loop_running = 0x0;
|
162 |
} |
163 |
/**
|
164 |
* Reads an eight bit number from an analog port.
|
165 |
* analog_init must be called before using this function.
|
166 |
*
|
167 |
* @param which the analog port to read from. One of
|
168 |
* the constants AN0 - AN7.
|
169 |
*
|
170 |
* @return the eight bit input to the specified port
|
171 |
*
|
172 |
* @see analog_init, analog10
|
173 |
**/
|
174 |
unsigned int analog_get8(int which) { |
175 |
// Let any previous conversion finish
|
176 |
while (ADCSRA & _BV(ADSC));
|
177 |
|
178 |
if(which < EXT_MUX) {
|
179 |
ADMUX = ADMUX_OPT + which; |
180 |
} else {
|
181 |
ADMUX = ADMUX_OPT + EXT_MUX; |
182 |
set_adc_mux(which - 8);
|
183 |
} |
184 |
|
185 |
// Start the conversion
|
186 |
ADCSRA |= _BV(ADSC); |
187 |
|
188 |
// Wait for the conversion to finish
|
189 |
while (ADCSRA & _BV(ADSC));
|
190 |
|
191 |
return ADCH; //since we left aligned the data, ADCH is the 8 MSB. |
192 |
} |
193 |
|
194 |
/**
|
195 |
* Reads a ten bit number from the specified port.
|
196 |
* analog_init must be called before using this function.
|
197 |
*
|
198 |
*
|
199 |
* @param which the analog port to read from. Typically
|
200 |
* a constant, one of AN0 - AN7.
|
201 |
*
|
202 |
* @return the ten bit number input to the specified port
|
203 |
*
|
204 |
* @see analog_init, analog8
|
205 |
**/
|
206 |
unsigned int analog_get10(int which) { |
207 |
int adc_h;
|
208 |
int adc_l;
|
209 |
|
210 |
// Let any previous conversion finish
|
211 |
while (ADCSRA & _BV(ADSC));
|
212 |
|
213 |
if(which < EXT_MUX) {
|
214 |
ADMUX = ADMUX_OPT + which; |
215 |
} else {
|
216 |
ADMUX = ADMUX_OPT + EXT_MUX; |
217 |
set_adc_mux(which - 8);
|
218 |
} |
219 |
|
220 |
// Start the conversion
|
221 |
ADCSRA |= _BV(ADSC); |
222 |
|
223 |
// Wait for the conversion to finish
|
224 |
while (ADCSRA & _BV(ADSC));
|
225 |
|
226 |
adc_l = ADCL; |
227 |
adc_h = ADCH; |
228 |
|
229 |
return ((adc_h << 2) | (adc_l >> 6)); |
230 |
} |
231 |
|
232 |
/**
|
233 |
* Returns the current position of the wheel, as an integer
|
234 |
* in the range 0 - 255.
|
235 |
* analog_init must be called before using this function.
|
236 |
*
|
237 |
* @return the orientation of the wheel, as an integer in
|
238 |
* the range 0 - 255.
|
239 |
*
|
240 |
* @see analog_init
|
241 |
**/
|
242 |
int wheel(void) { |
243 |
return analog8(WHEEL_PORT);
|
244 |
} |
245 |
|
246 |
|
247 |
/**
|
248 |
* Sets the value of the external analog mux. Values are read
|
249 |
* on AN7 physical port. (AN8 - AN15 are "virtual" ports).
|
250 |
*
|
251 |
* @param which which analog mux port (0-7) which corresponds
|
252 |
* to AN8-AN15.
|
253 |
*
|
254 |
* @bug FIX THIS IN THE NEXT BOARD REVISION:
|
255 |
* ADDR2 ADDR1 ADDR0
|
256 |
* G2.G4.G3 set mux to port 0-7 via vinary selection
|
257 |
* math would be much cleaner if it was G4.G3.G2
|
258 |
*
|
259 |
* @see analog_init
|
260 |
**/
|
261 |
void set_adc_mux(int which) { |
262 |
// mask so only proper bits are possible.
|
263 |
PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04); |
264 |
} |
265 |
|
266 |
/**@}**/ //end defgroup |
267 |
|
268 |
|
269 |
ISR(ADC_vect) { |
270 |
static volatile int adc_prev_loop_running = 0; |
271 |
int adc_h = 0; |
272 |
int adc_l = 0; |
273 |
|
274 |
//Store the value only if this read isn't for the BOM
|
275 |
if (ADMUX != BOM_PORT) {
|
276 |
adc_l = ADCL; |
277 |
adc_h = ADCH; |
278 |
|
279 |
an_val[adc_current_port - 1].adc10 = (adc_h << 2) | (adc_l >> 6); |
280 |
an_val[adc_current_port - 1].adc8 = adc_h;
|
281 |
} |
282 |
|
283 |
//Save the result only if we just turned off the loop
|
284 |
if (!adc_loop_running && !adc_prev_loop_running)
|
285 |
return;
|
286 |
|
287 |
adc_prev_loop_running = adc_loop_running; |
288 |
|
289 |
//Skip AN7 because it is not a real port
|
290 |
if (adc_current_port == AN6) {
|
291 |
ADMUX = ADMUX_OPT | EXT_MUX; |
292 |
set_adc_mux(AN8 - 8);
|
293 |
adc_current_port = AN8; |
294 |
//Wrap around
|
295 |
} else if (adc_current_port == AN11) { |
296 |
adc_current_port = AN1; |
297 |
ADMUX = ADMUX_OPT | adc_current_port; |
298 |
//Normal increment
|
299 |
} else {
|
300 |
adc_current_port++; |
301 |
|
302 |
if(adc_current_port < EXT_MUX) {
|
303 |
ADMUX = ADMUX_OPT | adc_current_port; |
304 |
} else {
|
305 |
ADMUX = ADMUX_OPT | EXT_MUX; |
306 |
set_adc_mux(adc_current_port - 8);
|
307 |
} |
308 |
} |
309 |
|
310 |
//Initiate next conversion only if we are running a loop
|
311 |
if (!adc_loop_running) {
|
312 |
return;
|
313 |
} else {
|
314 |
ADCSRA |= _BV(ADSC); |
315 |
} |
316 |
|
317 |
return;
|
318 |
} |
319 |
|