root / trunk / code / projects / libdragonfly / analog.c @ 1875
History | View | Annotate | Download (9.23 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 | 1496 | jsexton | #include "analog.h" |
40 | 1462 | dsschult | #include "serial.h" |
41 | 338 | bcoltin | // 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 | 1406 | jykong | 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 | 338 | bcoltin | |
57 | /**
|
||
58 | * Initializes the ADC.
|
||
59 | * Call analog_init before reading from the analog ports.
|
||
60 | *
|
||
61 | 437 | kwoo | * @see analog8, analog10, analog_get8, analog_get10
|
62 | 1406 | jykong | *
|
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 | 338 | bcoltin | **/
|
76 | 1496 | jsexton | void analog_init(int start_conversion) { |
77 | 1862 | djacobs | start_conversion=0; //forces the analog loop off |
78 | 1496 | jsexton | for (int i = 0; i < 11; i++) { |
79 | an_val[i].adc10 = 0;
|
||
80 | an_val[i].adc8 = 0;
|
||
81 | } |
||
82 | 338 | bcoltin | |
83 | 1496 | jsexton | // ADMUX register
|
84 | // Bit 7,6 - Set voltage reference to AVcc (0b01)
|
||
85 | // Bit 5 - ADLAR set to simplify moving from register
|
||
86 | // Bit 4 - X
|
||
87 | // Bit 3:0 - Sets the current channel
|
||
88 | // Initializes to read from AN1 first (AN0 is reservered for the BOM)
|
||
89 | ADMUX = 0;
|
||
90 | ADMUX |= ADMUX_OPT | _BV(MUX0); |
||
91 | 338 | bcoltin | |
92 | 1496 | jsexton | // ADC Status Register A
|
93 | // Bit 7 - ADEN is set (enables analog)
|
||
94 | // Bit 6 - Start conversion bit is set (must be done once for free-running mode)
|
||
95 | // Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
|
||
96 | // Bit 4 - ADC interrupt flag, 0
|
||
97 | // Bit 3 - Enable ADC Interrupt (required to run free-running mode)
|
||
98 | 1862 | djacobs | // Bits 2-0 - Set to create a clock divisor of 2, to make ADC clock != 8,000,000/64 = 125kHz (it runs at highest frequency)
|
99 | 1496 | jsexton | ADCSRA = 0;
|
100 | 1862 | djacobs | ADCSRA |= _BV(ADEN) | /*_BV(ADPS2) | _BV(ADPS1) |*/ _BV(ADPS0);
|
101 | 338 | bcoltin | |
102 | 1496 | jsexton | // Set external mux lines to outputs
|
103 | DDRG |= 0x1C;
|
||
104 | 338 | bcoltin | |
105 | 1496 | jsexton | // Set up first port for conversions
|
106 | set_adc_mux(0x00);
|
||
107 | adc_current_port = AN1; |
||
108 | 338 | bcoltin | |
109 | 1406 | jykong | |
110 | 1496 | jsexton | //Conversion loop disabled by default
|
111 | 338 | bcoltin | } |
112 | |||
113 | 862 | kwoo | /**
|
114 | * Returns the 8-bit analog conversion of which from
|
||
115 | * the lookup table. If the requested port is the BOM_PORT
|
||
116 | * you will get an automatic 0 since the BOM_PORT is not
|
||
117 | * read in the loop and not stored. If you need that port
|
||
118 | * you should use the functions in bom.c. There is an analog_get8
|
||
119 | * function which for instant lookups but should be avoided unless
|
||
120 | * you know what you're doing.
|
||
121 | *
|
||
122 | * @param which the port that you want to read
|
||
123 | *
|
||
124 | * @bug may cause a seg fault if which is a larger value
|
||
125 | * than exists in an_val table. Not sure if we should fix
|
||
126 | * this or not since it would add overhead.
|
||
127 | *
|
||
128 | * @return 8-bit analog value for the which port requested
|
||
129 | *
|
||
130 | * @see analog10, analog_get8, analog_get10
|
||
131 | **/
|
||
132 | 338 | bcoltin | unsigned int analog8(int which) { |
133 | 1862 | djacobs | /* if (which == BOM_PORT) {
|
134 | 338 | bcoltin | return 0;
|
135 | } else {
|
||
136 | return an_val[which - 1].adc8;
|
||
137 | }
|
||
138 | 1862 | djacobs | */
|
139 | return analog_get8(which);
|
||
140 | 338 | bcoltin | } |
141 | |||
142 | 437 | kwoo | /**
|
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 | 862 | kwoo | * function which for instant lookups but should be avoided unless
|
149 | * you know what you are doing.
|
||
150 | 437 | kwoo | *
|
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 | 338 | bcoltin | unsigned int analog10(int which) { |
162 | 1862 | djacobs | /* if (which == BOM_PORT) {
|
163 | 338 | bcoltin | return 0;
|
164 | } else {
|
||
165 | return an_val[which - 1].adc10;
|
||
166 | }
|
||
167 | 1862 | djacobs | */
|
168 | return analog_get10(which);
|
||
169 | 338 | bcoltin | } |
170 | |||
171 | 437 | kwoo | /**
|
172 | * Starts the analog update loop. Will continue to run
|
||
173 | * until analog_stop_loop is called.
|
||
174 | *
|
||
175 | 1406 | jykong | * @see analog_stop_loop, analog_loop_status
|
176 | 437 | kwoo | **/
|
177 | 1496 | jsexton | void analog_start_loop(void) { |
178 | if(adc_loop_status != ADC_LOOP_RUNNING){
|
||
179 | //Start the conversion, enable ADC interrupt
|
||
180 | ADCSRA |= _BV(ADIE); |
||
181 | ADCSRA |= _BV(ADSC); |
||
182 | adc_loop_status = ADC_LOOP_RUNNING; |
||
183 | } |
||
184 | 338 | bcoltin | } |
185 | |||
186 | 437 | kwoo | /**
|
187 | * Stops the analog update loop. If there is a current
|
||
188 | * read, it will finish up and be stored before the loop
|
||
189 | * is interrupted. No further updates will be made until
|
||
190 | * the loop is started again.
|
||
191 | *
|
||
192 | 1406 | jykong | * @see analog_start_loop, analog_loop_status
|
193 | 437 | kwoo | **/
|
194 | 1496 | jsexton | void analog_stop_loop() {
|
195 | //Signal to stop after the next conversion
|
||
196 | adc_sig_stop_loop = 1;
|
||
197 | 338 | bcoltin | } |
198 | 1406 | jykong | |
199 | 338 | bcoltin | /**
|
200 | 1406 | jykong | * Returns the status of loop. 0 for stopped.
|
201 | * 1 for running. 2 for paused.
|
||
202 | *
|
||
203 | * @see analog_start_loop, analog_stop_loop
|
||
204 | **/
|
||
205 | int analog_loop_status(void) { |
||
206 | return adc_loop_status;
|
||
207 | } |
||
208 | |||
209 | /**
|
||
210 | 862 | kwoo | * Reads an 8-bit number from an analog port.
|
211 | 338 | bcoltin | * analog_init must be called before using this function.
|
212 | 862 | kwoo | * The analog loop must also be stopped before using this
|
213 | * function or you will mess up the lookup table. You
|
||
214 | * must also reenabled the loop when you are done unless
|
||
215 | * you are doing more instant reads. See analog_stop_loop
|
||
216 | * and analog_start_loop for more information about the loop.
|
||
217 | 338 | bcoltin | *
|
218 | * @param which the analog port to read from. One of
|
||
219 | * the constants AN0 - AN7.
|
||
220 | *
|
||
221 | 862 | kwoo | * @return the 8-bit input to the specified port
|
222 | 338 | bcoltin | *
|
223 | 862 | kwoo | * @see analog_init, analog_get10, analog8, analog_stop_loop,
|
224 | * analog_start_loop
|
||
225 | 338 | bcoltin | **/
|
226 | 439 | kwoo | unsigned int analog_get8(int which) { |
227 | 338 | bcoltin | // Let any previous conversion finish
|
228 | while (ADCSRA & _BV(ADSC));
|
||
229 | |||
230 | if(which < EXT_MUX) {
|
||
231 | ADMUX = ADMUX_OPT + which; |
||
232 | } else {
|
||
233 | ADMUX = ADMUX_OPT + EXT_MUX; |
||
234 | set_adc_mux(which - 8);
|
||
235 | } |
||
236 | |||
237 | // Start the conversion
|
||
238 | ADCSRA |= _BV(ADSC); |
||
239 | |||
240 | // Wait for the conversion to finish
|
||
241 | while (ADCSRA & _BV(ADSC));
|
||
242 | |||
243 | return ADCH; //since we left aligned the data, ADCH is the 8 MSB. |
||
244 | } |
||
245 | |||
246 | /**
|
||
247 | 862 | kwoo | * Reads an 10-bit number from an analog port.
|
248 | 338 | bcoltin | * analog_init must be called before using this function.
|
249 | 862 | kwoo | * The analog loop must also be stopped before using this
|
250 | * function or you will mess up the lookup table. You
|
||
251 | * must also reenabled the loop when you are done unless
|
||
252 | * you are doing more instant reads. See analog_stop_loop
|
||
253 | * and analog_start_loop for more information about the loop.
|
||
254 | 338 | bcoltin | *
|
255 | *
|
||
256 | * @param which the analog port to read from. Typically
|
||
257 | * a constant, one of AN0 - AN7.
|
||
258 | *
|
||
259 | 862 | kwoo | * @return the 10-bit number input to the specified port
|
260 | 338 | bcoltin | *
|
261 | 862 | kwoo | * @see analog_init, analog_get8, analog10, analog_stop_loop,
|
262 | * analog_start_loop
|
||
263 | 338 | bcoltin | **/
|
264 | 439 | kwoo | unsigned int analog_get10(int which) { |
265 | 338 | bcoltin | int adc_h;
|
266 | int adc_l;
|
||
267 | |||
268 | // Let any previous conversion finish
|
||
269 | while (ADCSRA & _BV(ADSC));
|
||
270 | |||
271 | if(which < EXT_MUX) {
|
||
272 | ADMUX = ADMUX_OPT + which; |
||
273 | } else {
|
||
274 | ADMUX = ADMUX_OPT + EXT_MUX; |
||
275 | set_adc_mux(which - 8);
|
||
276 | } |
||
277 | |||
278 | // Start the conversion
|
||
279 | ADCSRA |= _BV(ADSC); |
||
280 | |||
281 | // Wait for the conversion to finish
|
||
282 | while (ADCSRA & _BV(ADSC));
|
||
283 | |||
284 | adc_l = ADCL; |
||
285 | adc_h = ADCH; |
||
286 | |||
287 | return ((adc_h << 2) | (adc_l >> 6)); |
||
288 | } |
||
289 | |||
290 | /**
|
||
291 | * Returns the current position of the wheel, as an integer
|
||
292 | * in the range 0 - 255.
|
||
293 | * analog_init must be called before using this function.
|
||
294 | *
|
||
295 | * @return the orientation of the wheel, as an integer in
|
||
296 | * the range 0 - 255.
|
||
297 | *
|
||
298 | * @see analog_init
|
||
299 | **/
|
||
300 | 439 | kwoo | int wheel(void) { |
301 | 338 | bcoltin | return analog8(WHEEL_PORT);
|
302 | } |
||
303 | |||
304 | |||
305 | /**
|
||
306 | * Sets the value of the external analog mux. Values are read
|
||
307 | * on AN7 physical port. (AN8 - AN15 are "virtual" ports).
|
||
308 | *
|
||
309 | * @param which which analog mux port (0-7) which corresponds
|
||
310 | * to AN8-AN15.
|
||
311 | *
|
||
312 | * @bug FIX THIS IN THE NEXT BOARD REVISION:
|
||
313 | * ADDR2 ADDR1 ADDR0
|
||
314 | * G2.G4.G3 set mux to port 0-7 via vinary selection
|
||
315 | * math would be much cleaner if it was G4.G3.G2
|
||
316 | *
|
||
317 | * @see analog_init
|
||
318 | **/
|
||
319 | 439 | kwoo | void set_adc_mux(int which) { |
320 | 338 | bcoltin | // mask so only proper bits are possible.
|
321 | PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04); |
||
322 | } |
||
323 | |||
324 | /**@}**/ //end defgroup |