Revision 338
Copying Kevin's analog code over from the analog branch.
analog.c | ||
---|---|---|
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 |
|
|
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 |
|
|
52 |
/** |
|
53 |
* Initializes the ADC. |
|
54 |
* Call analog_init before reading from the analog ports. |
|
55 |
* |
|
56 |
* @see analog8, analog10 |
|
57 |
**/ |
|
58 |
void analog_init(void) |
|
59 |
{ |
|
60 |
// ADC Status Register A |
|
61 |
// Bit 7 - ADEN is set (enables analog) |
|
62 |
// Bit 6 - Start conversion bit is set (must be done once for free-running mode) |
|
63 |
// Bit 5 - Enable Auto Trigger (for free running mode) |
|
64 |
// Bit 4 - ADC interrupt flag, 0 |
|
65 |
// Bit 3 - Enable ADC Interrupt (required to run free-running mode) |
|
66 |
// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/128 |
|
67 |
ADCSRA |= 0xEF; |
|
68 |
|
|
69 |
// ADC Status Register B |
|
70 |
// Bit 7, 5-3 - Must be cleared |
|
71 |
// Bit 2:0 - Set mode, currently cleared for free running operation |
|
72 |
// Bit 6 - Analog comparator mode - cleared |
|
73 |
// ADCSRB = 0x00; |
|
74 |
|
|
75 |
// ADMUX register |
|
76 |
// Bit 7,6 - Set voltage reference to AVcc (0b01) |
|
77 |
// Bit 5 - Set ADLAR bit for left adjust to do simple 8-bit reads |
|
78 |
// Bit 4 - X |
|
79 |
// Bit 3:0 - Sets the current channel, set to ADC7 (the external mux) |
|
80 |
ADMUX = 0x67; |
|
81 |
|
|
82 |
// Set external mux lines to outputs |
|
83 |
DDRG |= 0x1C; |
|
84 |
set_adc_mux(0x07); |
|
85 |
} |
|
86 |
|
|
87 |
|
|
88 |
/** |
|
89 |
* Reads an eight bit number from an analog port. |
|
90 |
* analog_init must be called before using this function. |
|
91 |
* |
|
92 |
* @param which the analog port to read from. One of |
|
93 |
* the constants AN0 - AN7. |
|
94 |
* |
|
95 |
* @return the eight bit input to the specified port |
|
96 |
* |
|
97 |
* @see analog_init, analog10 |
|
98 |
**/ |
|
99 |
unsigned int analog8(int which) |
|
100 |
{ |
|
101 |
if(which < EXT_MUX) |
|
102 |
ADMUX = 0x60 + which; |
|
103 |
else if(which == EXT_MUX) |
|
104 |
return 0; |
|
105 |
else |
|
106 |
{ |
|
107 |
ADMUX = 0x60 + EXT_MUX; |
|
108 |
set_adc_mux(which - 8); |
|
109 |
_delay_ms(1); |
|
110 |
} |
|
111 |
|
|
112 |
_delay_ms(1); // need at least 130 us between conversions |
|
113 |
return ADCH; |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* Reads a ten bit number from the specified port. |
|
118 |
* analog_init must be called before using this function. |
|
119 |
* |
|
120 |
* @param which the analog port to read from. Typically |
|
121 |
* a constant, one of AN0 - AN7. |
|
122 |
* |
|
123 |
* @return the ten bit number input to the specified port |
|
124 |
* |
|
125 |
* @see analog_init, analog8 |
|
126 |
**/ |
|
127 |
unsigned int analog10(int which) |
|
128 |
{ |
|
129 |
unsigned int adc_h = 0; |
|
130 |
unsigned int adc_l = 0; |
|
131 |
|
|
132 |
if(which < EXT_MUX) |
|
133 |
ADMUX = 0x60 + which; |
|
134 |
else if(which == EXT_MUX) |
|
135 |
return 0; |
|
136 |
else |
|
137 |
{ |
|
138 |
ADMUX = 0x60 + EXT_MUX; |
|
139 |
set_adc_mux(which - 8); |
|
140 |
_delay_ms(1); |
|
141 |
} |
|
142 |
|
|
143 |
_delay_ms(1); |
|
144 |
adc_l = ADCL; /* highest 2 bits of ADCL -> least 2 bits of analog val */ |
|
145 |
adc_h = ADCH; /* ADCH -> 8 highest bits of analog val */ |
|
146 |
|
|
147 |
return (adc_h << 2) | (adc_l >> 6); |
|
148 |
} |
|
149 |
|
|
150 |
/** |
|
151 |
* Returns the current position of the wheel, as an integer |
|
152 |
* in the range 0 - 255. |
|
153 |
* analog_init must be called before using this function. |
|
154 |
* |
|
155 |
* @return the orientation of the wheel, as an integer in |
|
156 |
* the range 0 - 255. |
|
157 |
* |
|
158 |
* @see analog_init |
|
159 |
**/ |
|
160 |
int wheel(void) |
|
161 |
{ |
|
162 |
return analog8(WHEEL_PORT); |
|
163 |
} |
|
164 |
|
|
165 |
/**@}**/ //end defgroup |
|
166 |
|
|
167 |
SIGNAL (SIG_ADC) |
|
168 |
{ |
|
169 |
// This is just here to catch ADC interrupts because ADC is free running. |
|
170 |
// No code needs to be in here. |
|
171 |
} |
|
172 |
|
|
173 |
void set_adc_mux(int which) |
|
174 |
{ |
|
175 |
// FIX THIS IN NEXT REVISION |
|
176 |
// ADDR2 ADDR1 ADDR0 |
|
177 |
// G2.G4.G3 set mux to port 0-7 via binary selection |
|
178 |
// math would be much cleaner if it was G4.G3.G2 |
|
179 |
|
|
180 |
// mask so only proper bits are possible. |
|
181 |
PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04); |
|
182 |
} |
|
183 |
|
|
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[10]; |
|
54 |
|
|
55 |
/** |
|
56 |
* Initializes the ADC. |
|
57 |
* Call analog_init before reading from the analog ports. |
|
58 |
* |
|
59 |
* @see analog8, analog10 |
|
60 |
**/ |
|
61 |
void analog_init(int start_conversion) |
|
62 |
{ |
|
63 |
for (int i = 0; i < 10; i++) { |
|
64 |
an_val[i].adc10 = 0; |
|
65 |
an_val[i].adc8 = 0; |
|
66 |
} |
|
67 |
|
|
68 |
//cli(); |
|
69 |
// ADMUX register |
|
70 |
// Bit 7,6 - Set voltage reference to AVcc (0b01) |
|
71 |
// Bit 5 - ADLAR set to simplify moving from register |
|
72 |
// Bit 4 - X |
|
73 |
// Bit 3:0 - Sets the current channel |
|
74 |
// Initializes to read from AN1 first (AN0 is reservered for the BOM) |
|
75 |
ADMUX = 0; |
|
76 |
ADMUX |= ADMUX_OPT | _BV(MUX0); |
|
77 |
|
|
78 |
|
|
79 |
// ADC Status Register A |
|
80 |
// Bit 7 - ADEN is set (enables analog) |
|
81 |
// Bit 6 - Start conversion bit is set (must be done once for free-running mode) |
|
82 |
// Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW |
|
83 |
// Bit 4 - ADC interrupt flag, 0 |
|
84 |
// Bit 3 - Enable ADC Interrupt (required to run free-running mode) |
|
85 |
// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz |
|
86 |
ADCSRA = 0; |
|
87 |
ADCSRA |= _BV(ADEN) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); |
|
88 |
|
|
89 |
// Set external mux lines to outputs |
|
90 |
DDRG |= 0x1C; |
|
91 |
|
|
92 |
// Set up first port for conversions |
|
93 |
set_adc_mux(0x00); |
|
94 |
adc_current_port = AN1; |
|
95 |
|
|
96 |
//Start the conversion if requested |
|
97 |
if (start_conversion) |
|
98 |
analog_start_loop(); |
|
99 |
else |
|
100 |
analog_stop_loop(); |
|
101 |
//sei(); |
|
102 |
|
|
103 |
} |
|
104 |
|
|
105 |
unsigned int analog8(int which) { |
|
106 |
if (which == BOM_PORT) { |
|
107 |
return 0; |
|
108 |
} else { |
|
109 |
return an_val[which - 1].adc8; |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
unsigned int analog10(int which) { |
|
114 |
if (which == BOM_PORT) { |
|
115 |
return 0; |
|
116 |
} else { |
|
117 |
return an_val[which - 1].adc10; |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
void analog_start_loop(void) { |
|
122 |
//Start the conversion |
|
123 |
ADCSRA |= _BV(ADSC); |
|
124 |
adc_loop_running = 0x1; |
|
125 |
} |
|
126 |
|
|
127 |
//will stop after current conversion finishes |
|
128 |
void analog_stop_loop(void) { |
|
129 |
//Stop the conversion |
|
130 |
adc_loop_running = 0x0; |
|
131 |
} |
|
132 |
/** |
|
133 |
* Reads an eight bit number from an analog port. |
|
134 |
* analog_init must be called before using this function. |
|
135 |
* |
|
136 |
* @param which the analog port to read from. One of |
|
137 |
* the constants AN0 - AN7. |
|
138 |
* |
|
139 |
* @return the eight bit input to the specified port |
|
140 |
* |
|
141 |
* @see analog_init, analog10 |
|
142 |
**/ |
|
143 |
unsigned int analog_get8(int which) |
|
144 |
{ |
|
145 |
// Let any previous conversion finish |
|
146 |
while (ADCSRA & _BV(ADSC)); |
|
147 |
|
|
148 |
if(which < EXT_MUX) { |
|
149 |
ADMUX = ADMUX_OPT + which; |
|
150 |
} else { |
|
151 |
ADMUX = ADMUX_OPT + EXT_MUX; |
|
152 |
set_adc_mux(which - 8); |
|
153 |
} |
|
154 |
|
|
155 |
// Start the conversion |
|
156 |
ADCSRA |= _BV(ADSC); |
|
157 |
|
|
158 |
// Wait for the conversion to finish |
|
159 |
while (ADCSRA & _BV(ADSC)); |
|
160 |
|
|
161 |
return ADCH; //since we left aligned the data, ADCH is the 8 MSB. |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
165 |
* Reads a ten bit number from the specified port. |
|
166 |
* analog_init must be called before using this function. |
|
167 |
* |
|
168 |
* |
|
169 |
* @param which the analog port to read from. Typically |
|
170 |
* a constant, one of AN0 - AN7. |
|
171 |
* |
|
172 |
* @return the ten bit number input to the specified port |
|
173 |
* |
|
174 |
* @see analog_init, analog8 |
|
175 |
**/ |
|
176 |
unsigned int analog_get10(int which) |
|
177 |
{ |
|
178 |
int adc_h; |
|
179 |
int adc_l; |
|
180 |
|
|
181 |
// Let any previous conversion finish |
|
182 |
while (ADCSRA & _BV(ADSC)); |
|
183 |
|
|
184 |
if(which < EXT_MUX) { |
|
185 |
ADMUX = ADMUX_OPT + which; |
|
186 |
} else { |
|
187 |
ADMUX = ADMUX_OPT + EXT_MUX; |
|
188 |
set_adc_mux(which - 8); |
|
189 |
} |
|
190 |
|
|
191 |
// Start the conversion |
|
192 |
ADCSRA |= _BV(ADSC); |
|
193 |
|
|
194 |
// Wait for the conversion to finish |
|
195 |
while (ADCSRA & _BV(ADSC)); |
|
196 |
|
|
197 |
adc_l = ADCL; |
|
198 |
adc_h = ADCH; |
|
199 |
|
|
200 |
return ((adc_h << 2) | (adc_l >> 6)); |
|
201 |
} |
|
202 |
|
|
203 |
/** |
|
204 |
* Returns the current position of the wheel, as an integer |
|
205 |
* in the range 0 - 255. |
|
206 |
* analog_init must be called before using this function. |
|
207 |
* |
|
208 |
* @return the orientation of the wheel, as an integer in |
|
209 |
* the range 0 - 255. |
|
210 |
* |
|
211 |
* @see analog_init |
|
212 |
**/ |
|
213 |
int wheel(void) |
|
214 |
{ |
|
215 |
return analog8(WHEEL_PORT); |
|
216 |
} |
|
217 |
|
|
218 |
|
|
219 |
/** |
|
220 |
* Sets the value of the external analog mux. Values are read |
|
221 |
* on AN7 physical port. (AN8 - AN15 are "virtual" ports). |
|
222 |
* |
|
223 |
* @param which which analog mux port (0-7) which corresponds |
|
224 |
* to AN8-AN15. |
|
225 |
* |
|
226 |
* @bug FIX THIS IN THE NEXT BOARD REVISION: |
|
227 |
* ADDR2 ADDR1 ADDR0 |
|
228 |
* G2.G4.G3 set mux to port 0-7 via vinary selection |
|
229 |
* math would be much cleaner if it was G4.G3.G2 |
|
230 |
* |
|
231 |
* @see analog_init |
|
232 |
**/ |
|
233 |
void set_adc_mux(int which) |
|
234 |
{ |
|
235 |
// mask so only proper bits are possible. |
|
236 |
PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04); |
|
237 |
} |
|
238 |
|
|
239 |
/**@}**/ //end defgroup |
|
240 |
|
|
241 |
|
|
242 |
ISR(ADC_vect) { |
|
243 |
static volatile int adc_prev_loop_running = 0; |
|
244 |
|
|
245 |
int adc_h = 0; |
|
246 |
int adc_l = 0; |
|
247 |
|
|
248 |
//usb_putc('p'); |
|
249 |
//usb_puti(adc_current_port); |
|
250 |
//usb_putc('r'); |
|
251 |
//usb_puti(adc_loop_running); |
|
252 |
//usb_puts("\n\r"); |
|
253 |
|
|
254 |
//Store the value only if this read isn't for the BOM |
|
255 |
if (ADMUX != BOM_PORT) { |
|
256 |
adc_l = ADCL; |
|
257 |
adc_h = ADCH; |
|
258 |
|
|
259 |
an_val[adc_current_port - 1].adc10 = (adc_h << 2) | (adc_l >> 6); |
|
260 |
an_val[adc_current_port - 1].adc8 = adc_h; |
|
261 |
//usb_puti(an_val[adc_current_port - 1].adc10); |
|
262 |
//usb_puts("\n\r"); |
|
263 |
//usb_puti(an_val[adc_current_port - 1].adc8); |
|
264 |
//usb_puti(ADCH); |
|
265 |
//usb_puts("\n\r"); |
|
266 |
} |
|
267 |
|
|
268 |
//Save the result only if we just turned off the loop |
|
269 |
if (!adc_loop_running && !adc_prev_loop_running) |
|
270 |
return; |
|
271 |
|
|
272 |
adc_prev_loop_running = adc_loop_running; |
|
273 |
|
|
274 |
//Skip AN7 because it is not a real port |
|
275 |
if (adc_current_port == AN6) { |
|
276 |
ADMUX = ADMUX_OPT | EXT_MUX; |
|
277 |
set_adc_mux(AN8 - 8); |
|
278 |
adc_current_port = AN8; |
|
279 |
//Wrap around |
|
280 |
} else if (adc_current_port == AN11) { |
|
281 |
adc_current_port = AN1; |
|
282 |
ADMUX = ADMUX_OPT | adc_current_port; |
|
283 |
//Normal increment |
|
284 |
} else { |
|
285 |
adc_current_port++; |
|
286 |
|
|
287 |
if(adc_current_port < EXT_MUX) { |
|
288 |
ADMUX = ADMUX_OPT | adc_current_port; |
|
289 |
} else { |
|
290 |
ADMUX = ADMUX_OPT | EXT_MUX; |
|
291 |
set_adc_mux(adc_current_port - 8); |
|
292 |
} |
|
293 |
} |
|
294 |
|
|
295 |
//Initiate next conversion only if we are running a loop |
|
296 |
if (!adc_loop_running) |
|
297 |
return; |
|
298 |
|
|
299 |
ADCSRA |= _BV(ADSC); |
|
300 |
|
|
301 |
//if (ADCSRA & _BV(ADSC)) |
|
302 |
// usb_putc('s'); |
|
303 |
|
|
304 |
return; |
|
305 |
} |
|
306 |
|
Also available in: Unified diff