root / trunk / code / projects / colonet / testing / wl_network_colonet / analog.c @ 13
History | View | Annotate | Download (1.71 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 <avr/delay.h> |
10 |
#include <avr/interrupt.h> |
11 |
#include <avr/signal.h> |
12 |
#include "analog.h" |
13 |
|
14 |
void analog_init(void){ |
15 |
|
16 |
/*
|
17 |
ADC Status Register A
|
18 |
Bit 7 - ADEN is set (enables analog)
|
19 |
Bit 6 - Start conversion bit is set (must be done once for free-running mode)
|
20 |
Bit 5 - Enable Auto Trigger (for free running mode)
|
21 |
Bit 3 - Enable ADC Interrupt (required to run free-running mode)
|
22 |
Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 16,000,000/128
|
23 |
*/
|
24 |
ADCSRA |= 0xEF;
|
25 |
|
26 |
/*
|
27 |
ADMUX register
|
28 |
Bit 7,6 - Set voltage reference to AVcc (0b01)
|
29 |
Bit 5 - Set ADLAR bit for left adjust to do simple 8-bit reads
|
30 |
Bit 4 - X
|
31 |
Bit 3:0 - Sets the current channel
|
32 |
*/
|
33 |
ADMUX = 0x67;
|
34 |
} |
35 |
|
36 |
unsigned int analog8(int which){ |
37 |
ADMUX = 0x60 + which;
|
38 |
_delay_ms(1); // need at least 130 us between conversions (for fwr robot, how much for new avr?) |
39 |
return ADCH;
|
40 |
} |
41 |
|
42 |
unsigned int analog10(int which){ |
43 |
unsigned int adc_h = 0; |
44 |
unsigned int adc_l = 0; |
45 |
|
46 |
ADMUX = 0x60 + which;
|
47 |
_delay_ms(1);
|
48 |
adc_l = ADCL; /* highest 2 bits of ADCL -> least 2 bits of analog val */
|
49 |
adc_h = ADCH; /* ADCH -> 8 highest bits of analog val */
|
50 |
|
51 |
return (adc_h << 2) | (adc_l >> 6); |
52 |
} |
53 |
|
54 |
int wheel(void){ |
55 |
return analog8(WHEEL_PORT);
|
56 |
} |
57 |
|
58 |
int battery(void){ |
59 |
return analog8(BATT_PORT) * 500 / 255; /* 5 volts is the max, 255 is the max 8bit number */ |
60 |
} |
61 |
|
62 |
SIGNAL (SIG_ADC) |
63 |
{ |
64 |
// This is just here to catch ADC interrupts because ADC is free running.
|
65 |
// No code needs to be in here.
|
66 |
} |