root / trunk / code / projects / colonet / utilities / manual_control / manualControlRobot / analog.c @ 13
History | View | Annotate | Download (2.72 KB)
1 |
/*
|
---|---|
2 |
analog.c - Contains the function implementations for manipulating the ADC
|
3 |
on the firefly+ board
|
4 |
|
5 |
author: CMU Robotics Club, Colony Project
|
6 |
code mostly taken from fwr analog file (author: Tom Lauwers)
|
7 |
*/
|
8 |
|
9 |
#include <util/delay.h> |
10 |
#include <avr/interrupt.h> |
11 |
#include "analog.h" |
12 |
|
13 |
void analog_init(void){ |
14 |
|
15 |
/*
|
16 |
ADC Status Register A
|
17 |
Bit 7 - ADEN is set (enables analog)
|
18 |
Bit 6 - Start conversion bit is set (must be done once for free-running mode)
|
19 |
Bit 5 - Enable Auto Trigger (for free running mode)
|
20 |
Bit 3 - Enable ADC Interrupt (required to run free-running mode)
|
21 |
Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 16,000,000/128
|
22 |
*/
|
23 |
ADCSRA |= 0xEF;
|
24 |
|
25 |
/*
|
26 |
ADMUX register
|
27 |
Bit 7,6 - Set voltage reference to AVcc (0b01)
|
28 |
Bit 5 - Set ADLAR bit for left adjust to do simple 8-bit reads
|
29 |
Bit 4 - X
|
30 |
Bit 3:0 - Sets the current channel
|
31 |
*/
|
32 |
ADMUX = 0x67;
|
33 |
|
34 |
#ifdef FFPP
|
35 |
DDRA |= 0x70;
|
36 |
set_adc_mux(0x07);
|
37 |
#endif
|
38 |
|
39 |
} |
40 |
|
41 |
#ifdef FFPP
|
42 |
void set_adc_mux(int which) |
43 |
{ |
44 |
// A4..A6 set mux to port 0-7 via binary selection
|
45 |
|
46 |
// mask so only proper bits are possible.
|
47 |
which &= 0x07;
|
48 |
PORTA &= 0x8F;
|
49 |
PORTA |= which << 4;
|
50 |
} |
51 |
|
52 |
void enable_analog(int analog_setting) //why do we need this? |
53 |
{ |
54 |
// Digital input disable - don't disable any of them
|
55 |
// Set any of the lower six bits to a 1 to disable that bit
|
56 |
DDRF = 0x00; //ALL_ANALOG; |
57 |
} |
58 |
#endif
|
59 |
|
60 |
unsigned int analog8(int which){ |
61 |
|
62 |
#ifdef FFPP
|
63 |
|
64 |
if(which < EXT_MUX)
|
65 |
{ |
66 |
ADMUX = 0x60 + which;
|
67 |
} |
68 |
else if(which == EXT_MUX) //Why dont we combine this and the above? |
69 |
{ |
70 |
return 0; |
71 |
} |
72 |
else
|
73 |
{ |
74 |
ADMUX = 0x60 + EXT_MUX;
|
75 |
set_adc_mux(which - 8);
|
76 |
_delay_ms(10);
|
77 |
} |
78 |
|
79 |
|
80 |
#else
|
81 |
ADMUX = 0x60 + which;
|
82 |
#endif
|
83 |
|
84 |
_delay_ms(1); // need at least 130 us between conversions (for fwr robot, how much for new avr?) |
85 |
return ADCH;
|
86 |
} |
87 |
|
88 |
unsigned int analog10(int which){ |
89 |
unsigned int adc_h = 0; |
90 |
unsigned int adc_l = 0; |
91 |
|
92 |
|
93 |
|
94 |
#ifdef FFPP
|
95 |
|
96 |
if(which < EXT_MUX)
|
97 |
{ |
98 |
ADMUX = 0x60 + which;
|
99 |
} |
100 |
else if(which == EXT_MUX) //Why dont we combine this and the above? |
101 |
{ |
102 |
return 0; |
103 |
} |
104 |
else
|
105 |
{ |
106 |
ADMUX = 0x60 + EXT_MUX;
|
107 |
set_adc_mux(which - 8);
|
108 |
_delay_ms(10);
|
109 |
} |
110 |
|
111 |
|
112 |
#else
|
113 |
ADMUX = 0x60 + which;
|
114 |
#endif
|
115 |
_delay_ms(1);
|
116 |
adc_l = ADCL; /* highest 2 bits of ADCL -> least 2 bits of analog val */
|
117 |
adc_h = ADCH; /* ADCH -> 8 highest bits of analog val */
|
118 |
|
119 |
return (adc_h << 2) | (adc_l >> 6); |
120 |
} |
121 |
|
122 |
int wheel(void){ |
123 |
return analog8(WHEEL_PORT);
|
124 |
} |
125 |
|
126 |
int battery(void){ |
127 |
return analog8(BATT_PORT) * 500 >>7; /* 5 volts is the max, 255 is the max 8bit number , *2 for the divider */ |
128 |
} |
129 |
|
130 |
SIGNAL (SIG_ADC) |
131 |
{ |
132 |
// This is just here to catch ADC interrupts because ADC is free running.
|
133 |
// No code needs to be in here.
|
134 |
} |