root / branches / analog / trunk / code / projects / libdragonfly / analog.c @ 275
History | View | Annotate | Download (5.62 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 |
|
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;
|
52 |
int adc_current_port;
|
53 |
|
54 |
/**
|
55 |
* Initializes the ADC.
|
56 |
* Call analog_init before reading from the analog ports.
|
57 |
*
|
58 |
* @see analog8, analog10
|
59 |
**/
|
60 |
void analog_init(void) |
61 |
{ |
62 |
// ADMUX register
|
63 |
// Bit 7,6 - Set voltage reference to AVcc (0b01)
|
64 |
// Bit 5 - ADLAR not set to simplify moving from register
|
65 |
// Bit 4 - X
|
66 |
// Bit 3:0 - Sets the current channel
|
67 |
// Initializes to read from AN1 first (AN0 is reservered for the BOM)
|
68 |
ADMUX = ~_BV(REFS1) | _BV(REFS0) | ~_BV(ADLAR) | _BV(MUX0); |
69 |
|
70 |
// ADC Status Register A
|
71 |
// Bit 7 - ADEN is set (enables analog)
|
72 |
// Bit 6 - Start conversion bit is set (must be done once for free-running mode)
|
73 |
// Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
|
74 |
// Bit 4 - ADC interrupt flag, 0
|
75 |
// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
|
76 |
// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
|
77 |
ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | ~_BV(ADPS0); |
78 |
|
79 |
// Set external mux lines to outputs
|
80 |
DDRG |= 0x1C;
|
81 |
set_adc_mux(0x07);
|
82 |
|
83 |
adc_loop_running = 1;
|
84 |
adc_current_port = AN1; |
85 |
} |
86 |
|
87 |
unsigned int analog_get8(int which) { |
88 |
return an_val[which - 1].adc8; |
89 |
} |
90 |
|
91 |
unsigned int analog_get10(int which) { |
92 |
return an_val[which - 1].adc10; |
93 |
} |
94 |
|
95 |
|
96 |
/**
|
97 |
* Reads an eight bit number from an analog port.
|
98 |
* analog_init must be called before using this function.
|
99 |
*
|
100 |
* @param which the analog port to read from. One of
|
101 |
* the constants AN0 - AN7.
|
102 |
*
|
103 |
* @return the eight bit input to the specified port
|
104 |
*
|
105 |
* @see analog_init, analog10
|
106 |
**/
|
107 |
unsigned int analog8(int which) |
108 |
{ |
109 |
// Let any previous conversion finish
|
110 |
while (ADCSRA & _BV(ADSC));
|
111 |
|
112 |
if(which < EXT_MUX)
|
113 |
ADMUX = 0x40 + which;
|
114 |
else if(which == EXT_MUX) |
115 |
return 0; |
116 |
else
|
117 |
{ |
118 |
ADMUX = 0x40 + EXT_MUX;
|
119 |
set_adc_mux(which - 8);
|
120 |
} |
121 |
|
122 |
// Start the conversion
|
123 |
ADCSRA |= _BV(ADSC); |
124 |
|
125 |
// Wait for the conversion to finish
|
126 |
while (ADCSRA & _BV(ADSC)) ;
|
127 |
|
128 |
return ADCL; //since we left aligned the data, ADCH is the 8 MSB. |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Reads a ten bit number from the specified port.
|
133 |
* analog_init must be called before using this function.
|
134 |
*
|
135 |
* @param which the analog port to read from. Typically
|
136 |
* a constant, one of AN0 - AN7.
|
137 |
*
|
138 |
* @return the ten bit number input to the specified port
|
139 |
*
|
140 |
* @see analog_init, analog8
|
141 |
**/
|
142 |
unsigned int analog10(int which) |
143 |
{ |
144 |
// Let any previous conversion finish
|
145 |
while (ADCSRA & _BV(ADSC));
|
146 |
|
147 |
if(which < EXT_MUX)
|
148 |
ADMUX = 0x40 + which;
|
149 |
else if(which == EXT_MUX) |
150 |
return 0; |
151 |
else
|
152 |
{ |
153 |
ADMUX = 0x40 + EXT_MUX;
|
154 |
set_adc_mux(which - 8);
|
155 |
} |
156 |
|
157 |
// Start the conversion
|
158 |
ADCSRA |= _BV(ADSC); |
159 |
|
160 |
// Wait for the conversion to finish
|
161 |
while (ADCSRA & _BV(ADSC)) ;
|
162 |
|
163 |
return ((ADCH << 8) | ADCL); |
164 |
} |
165 |
|
166 |
/**
|
167 |
* Returns the current position of the wheel, as an integer
|
168 |
* in the range 0 - 255.
|
169 |
* analog_init must be called before using this function.
|
170 |
*
|
171 |
* @return the orientation of the wheel, as an integer in
|
172 |
* the range 0 - 255.
|
173 |
*
|
174 |
* @see analog_init
|
175 |
**/
|
176 |
int wheel(void) |
177 |
{ |
178 |
return analog_get8(WHEEL_PORT);
|
179 |
} |
180 |
|
181 |
/**@}**/ //end defgroup |
182 |
|
183 |
void set_adc_mux(int which) |
184 |
{ |
185 |
// FIX THIS IN NEXT REVISION
|
186 |
// ADDR2 ADDR1 ADDR0
|
187 |
// G2.G4.G3 set mux to port 0-7 via binary selection
|
188 |
// math would be much cleaner if it was G4.G3.G2
|
189 |
|
190 |
// mask so only proper bits are possible.
|
191 |
PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04); |
192 |
} |
193 |
|
194 |
ISR(ANALOG_COMP_vect) { |
195 |
//Only perform this if we are running the loop
|
196 |
if (!adc_loop_running)
|
197 |
return;
|
198 |
|
199 |
an_val[adc_current_port - 1].adc10 = (ADCH << 8) | ADCL; |
200 |
|
201 |
//Increment current sensor, wrap around if needed
|
202 |
if (adc_current_port == AN10) {
|
203 |
adc_current_port = AN1; |
204 |
ADMUX = 0x40 + adc_current_port;
|
205 |
} else {
|
206 |
adc_current_port++; |
207 |
|
208 |
if(adc_current_port < EXT_MUX) {
|
209 |
ADMUX = 0x40 + adc_current_port;
|
210 |
} else {
|
211 |
ADMUX = 0x40 + EXT_MUX;
|
212 |
set_adc_mux(adc_current_port - 8);
|
213 |
} |
214 |
} |
215 |
|
216 |
ADCSRA |= _BV(ADSC); |
217 |
|
218 |
} |
219 |
|