Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libdragonfly / analog.c @ 1925

History | View | Annotate | Download (11.6 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 1924 azirbel
    start_conversion=0;                   //forces the analog loop off
78
    for (int i = 0; i < 11; i++) {
79
        an_val[i].adc10 = 0;
80
        an_val[i].adc8 = 0;
81
    }
82 338 bcoltin
83 1924 azirbel
    // 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 1924 azirbel
    // 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
    // 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
    ADCSRA = 0;
100
    ADCSRA |= _BV(ADEN) | /*_BV(ADPS2) | _BV(ADPS1) |*/ _BV(ADPS0);
101 1922 azirbel
102 1924 azirbel
    // Set external mux lines to outputs
103
    DDRG |= 0x1C;
104 1922 azirbel
105 1924 azirbel
    // Set line sensor mux lines to output.  Uses LCD/SPI header
106
    DDRD |=0xE0;
107 338 bcoltin
108 1924 azirbel
    // Set up first port for conversions
109
    set_adc_mux(0x00);
110
    adc_current_port = AN1;
111
112
    //Start the conversion loop if requested
113
    if (start_conversion)
114
        analog_start_loop();
115
116
    //Conversion loop disabled by default
117 338 bcoltin
}
118
119 862 kwoo
/**
120
 * Returns the 8-bit analog conversion of which from
121
 * the lookup table. If the requested port is the BOM_PORT
122
 * you will get an automatic 0 since the BOM_PORT is not
123
 * read in the loop and not stored. If you need that port
124
 * you should use the functions in bom.c. There is an analog_get8
125
 * function which for instant lookups but should be avoided unless
126
 * you know what you're doing.
127
 *
128
 * @param which the port that you want to read
129
 *
130
 * @bug may cause a seg fault if which is a larger value
131
 * than exists in an_val table. Not sure if we should fix
132
 * this or not since it would add overhead.
133
 *
134
 * @return 8-bit analog value for the which port requested
135
 *
136
 * @see analog10, analog_get8, analog_get10
137
 **/
138 338 bcoltin
unsigned int analog8(int which) {
139 1924 azirbel
    /*        if (which == BOM_PORT) {
140
        return 0;
141
        } else {
142
        return an_val[which - 1].adc8;
143
        }
144
     */
145
    return analog_get8(which);
146 338 bcoltin
}
147
148 437 kwoo
/**
149
 * Returns the 10-bit analog conversion of which from
150
 * the lookup table. If the requested port is the BOM_PORT
151
 * you will get an automatic 0 since the BOM_PORT is not
152
 * read in the loop and not stored. If you need that port
153
 * you should use the functions in bom.c. There is an analog_get10
154 862 kwoo
 * function which for instant lookups but should be avoided unless
155
 * you know what you are doing.
156 437 kwoo
 *
157
 * @param which the port that you want to read
158
 *
159
 * @bug may cause a seg fault if which is a larger value
160
 * than exists in an_val table. Not sure if we should fix
161
 * this or not since it would add overhead.
162
 *
163
 * @return 10-bit analog value for the which port requested
164
 *
165
 * @see analog8, analog_get8, analog_get10
166
 **/
167 338 bcoltin
unsigned int analog10(int which) {
168 1924 azirbel
    /*        if (which == BOM_PORT) {
169
        return 0;
170
        } else {
171
        return an_val[which - 1].adc10;
172
        }
173
     */
174
    return analog_get10(which);
175 338 bcoltin
}
176
177 437 kwoo
/**
178
 * Starts the analog update loop. Will continue to run
179
 * until analog_stop_loop is called.
180
 *
181 1406 jykong
 * @see analog_stop_loop, analog_loop_status
182 437 kwoo
 **/
183 1496 jsexton
void analog_start_loop(void) {
184 1924 azirbel
    if(adc_loop_status != ADC_LOOP_RUNNING){
185
        //Start the conversion, enable ADC interrupt
186
        ADCSRA |= _BV(ADIE);
187
        ADCSRA |= _BV(ADSC);
188
        adc_loop_status = ADC_LOOP_RUNNING;
189
    }
190 338 bcoltin
}
191
192 437 kwoo
/**
193
 * Stops the analog update loop. If there is a current
194
 * read, it will finish up and be stored before the loop
195
 * is interrupted. No further updates will be made until
196
 * the loop is started again.
197
 *
198 1406 jykong
 * @see analog_start_loop, analog_loop_status
199 437 kwoo
 **/
200 1496 jsexton
void analog_stop_loop() {
201 1924 azirbel
    //Signal to stop after the next conversion
202
    adc_sig_stop_loop = 1;
203 338 bcoltin
}
204 1406 jykong
205 338 bcoltin
/**
206 1406 jykong
 * Returns the status of loop. 0 for stopped.
207
 * 1 for running. 2 for paused.
208
 *
209
 * @see analog_start_loop, analog_stop_loop
210
 **/
211
int analog_loop_status(void) {
212 1924 azirbel
    return adc_loop_status;
213 1406 jykong
}
214
215
/**
216 862 kwoo
 * Reads an 8-bit number from an analog port.
217 338 bcoltin
 * analog_init must be called before using this function.
218 862 kwoo
 * The analog loop must also be stopped before using this
219
 * function or you will mess up the lookup table. You
220
 * must also reenabled the loop when you are done unless
221
 * you are doing more instant reads. See analog_stop_loop
222
 * and analog_start_loop for more information about the loop.
223 338 bcoltin
 *
224
 * @param which the analog port to read from. One of
225
 * the constants AN0 - AN7.
226
 *
227 862 kwoo
 * @return the 8-bit input to the specified port
228 338 bcoltin
 *
229 862 kwoo
 * @see analog_init, analog_get10, analog8, analog_stop_loop,
230
 * analog_start_loop
231 338 bcoltin
 **/
232 439 kwoo
unsigned int analog_get8(int which) {
233 1924 azirbel
    // Let any previous conversion finish
234
    while (ADCSRA & _BV(ADSC));
235 338 bcoltin
236 1924 azirbel
    if(which < EXT_MUX) {
237
        ADMUX = ADMUX_OPT + which;
238
    } else {
239
        ADMUX = ADMUX_OPT + EXT_MUX;
240
        set_adc_mux(which - 8);
241
    }
242 338 bcoltin
243 1924 azirbel
    // Start the conversion
244
    ADCSRA |= _BV(ADSC);
245
246
    // Wait for the conversion to finish
247
    while (ADCSRA & _BV(ADSC));
248
249
    return ADCH; //since we left aligned the data, ADCH is the 8 MSB.
250 338 bcoltin
}
251
252
/**
253 862 kwoo
 * Reads an 10-bit number from an analog port.
254 338 bcoltin
 * analog_init must be called before using this function.
255 862 kwoo
 * The analog loop must also be stopped before using this
256
 * function or you will mess up the lookup table. You
257
 * must also reenabled the loop when you are done unless
258
 * you are doing more instant reads. See analog_stop_loop
259
 * and analog_start_loop for more information about the loop.
260 338 bcoltin
 *
261
 *
262
 * @param which the analog port to read from. Typically
263
 * a constant, one of AN0 - AN7.
264
 *
265 862 kwoo
 * @return the 10-bit number input to the specified port
266 338 bcoltin
 *
267 862 kwoo
 * @see analog_init, analog_get8, analog10, analog_stop_loop,
268
 * analog_start_loop
269 338 bcoltin
 **/
270 439 kwoo
unsigned int analog_get10(int which) {
271 1924 azirbel
    int adc_h;
272
    int adc_l;
273 338 bcoltin
274 1924 azirbel
    // Let any previous conversion finish
275
    while (ADCSRA & _BV(ADSC));
276 338 bcoltin
277 1924 azirbel
    if(which < EXT_MUX) {
278
        ADMUX = ADMUX_OPT + which;
279
    } else {
280
        ADMUX = ADMUX_OPT + EXT_MUX;
281
        set_adc_mux(which - 8);
282
    }
283 338 bcoltin
284 1924 azirbel
    // Start the conversion
285
    ADCSRA |= _BV(ADSC);
286
287
    // Wait for the conversion to finish
288
    while (ADCSRA & _BV(ADSC));
289
290
    adc_l = ADCL;
291
    adc_h = ADCH;
292
293
    return ((adc_h << 2) | (adc_l >> 6));
294 338 bcoltin
}
295
296 1922 azirbel
297
/** Returns the 10 bit value from the line sensors **/
298
unsigned int read_line(int which) {
299 1924 azirbel
    int adc_h;
300
    int adc_l;
301 1922 azirbel
302 1924 azirbel
    // Let any previous conversion finish
303
    while (ADCSRA & _BV(ADSC));
304 1922 azirbel
305 1924 azirbel
    ADMUX = ADMUX_OPT + AN1;
306 1922 azirbel
307 1924 azirbel
     which = ((which&1)<<2) + ((which&2)) + ((which&4)>>2);
308 1922 azirbel
309 1924 azirbel
    // mask so only proper bits are possible.
310
    PORTD = (PORTD & 0x1F) | ((which & 0x07) << 5);
311 1922 azirbel
312 1925 djacobs
    //For loop is used only as a delay to allow mux to settle
313
    for(volatile int i=0; i<5; i++);
314
315 1924 azirbel
    // Start the conversion
316
    ADCSRA |= _BV(ADSC);
317
318
    // Wait for the conversion to finish
319
    while (ADCSRA & _BV(ADSC));
320
321
    adc_l = ADCL;
322
    adc_h = ADCH;
323
324
    return ((adc_h << 2) | (adc_l >> 6));
325
326 1922 azirbel
}
327
328
329
330
331 338 bcoltin
/**
332
 * Returns the current position of the wheel, as an integer
333
 * in the range 0 - 255.
334
 * analog_init must be called before using this function.
335
 *
336
 * @return the orientation of the wheel, as an integer in
337
 * the range 0 - 255.
338
 *
339
 * @see analog_init
340
 **/
341 439 kwoo
int wheel(void) {
342 1924 azirbel
    return analog8(WHEEL_PORT);
343 338 bcoltin
}
344
345
346
/**
347
 * Sets the value of the external analog mux. Values are read
348
 *         on AN7 physical port. (AN8 - AN15 are "virtual" ports).
349
 *
350
 * @param which which analog mux port (0-7) which corresponds
351
 *                   to AN8-AN15.
352
 *
353
 * @bug FIX THIS IN THE NEXT BOARD REVISION:
354
 *                ADDR2 ADDR1 ADDR0
355
 *                G2.G4.G3 set mux to port 0-7 via vinary selection
356
 *                math would be much cleaner if it was G4.G3.G2
357
 *
358
 * @see analog_init
359
 **/
360 439 kwoo
void set_adc_mux(int which) {
361 1924 azirbel
    // mask so only proper bits are possible.
362
    PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04);
363 338 bcoltin
}
364
365
/**@}**/ //end defgroup
366 1922 azirbel
367
368
ISR(ADC_vect) {
369 1924 azirbel
    int adc_h = 0;
370
    int adc_l = 0;
371 1922 azirbel
372 1924 azirbel
    if(adc_loop_status != ADC_LOOP_RUNNING) return;
373 1922 azirbel
374 1924 azirbel
    //Store the value only if this read isn't for the BOM
375
    if (ADMUX != BOM_PORT) {
376
        adc_l = ADCL;
377
        adc_h = ADCH;
378
379
        an_val[adc_current_port - 1].adc10 = (adc_h << 2) | (adc_l >> 6);
380
        an_val[adc_current_port - 1].adc8 = adc_h;
381
    }
382
383
    //Skip AN7 because it is not a real port
384
    if (adc_current_port == AN6) {
385
        ADMUX = ADMUX_OPT | EXT_MUX;
386
        set_adc_mux(AN8 - 8);
387
        adc_current_port = AN8;
388
        //Wrap around
389
    } else if (adc_current_port == AN11) {
390
        adc_current_port = AN1;
391
        ADMUX = ADMUX_OPT | adc_current_port;
392
        //Normal increment
393
    } else {
394
        adc_current_port++;
395
396
        if(adc_current_port < EXT_MUX) {
397
            ADMUX = ADMUX_OPT | adc_current_port;
398
        } else {
399
            ADMUX = ADMUX_OPT | EXT_MUX;
400
            set_adc_mux(adc_current_port - 8);
401
        }
402
    }
403
404
    //Stop loop if signal is set
405
    if(adc_sig_stop_loop) {
406
        adc_sig_stop_loop = 0;
407
        adc_loop_status = ADC_LOOP_STOPPED;
408
        return;
409
    }
410
411
    //Start next conversion
412
    ADCSRA |= _BV(ADSC);
413 1922 azirbel
}