Sat, 03 Dec 2011 12:10:10 +0100
some code cleanup
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
1 | #include "adc.h" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
2 | #include <avr/io.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
3 | #include <string.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
4 | #include <stdint.h> |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
5 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
6 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
7 | // Functions to access the internal ADC |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
8 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
9 | // internal ADC reference configuration |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
10 | static uint8_t adcReference=(1<<REFS0); // = VCC 5V |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
11 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
12 | // get a sample from ADC chip. |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
13 | // chanell must be in range 0-7. |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
14 | uint16_t getADC(int channel) { |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
15 | // if (channel == 0) |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
16 | adcReference = ((1<<REFS1) | (1<<REFS0)); // switch to 2.56V (~2x |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
17 | //adcReference = (1<<REFS1); // switch to 1.1v (~4.5x) - not on ATMEGA16 |
7
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
18 | // else |
f4e250d5402b
fixes, bound lap counter reset to race start button
Malte Bayer <mbayer@neo-soft.org>
parents:
0
diff
changeset
|
19 | // adcReference = (1<<REFS0); // switch to 5v (1x) |
0
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
20 | ADMUX = adcReference + channel; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
21 | // start one sample |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
22 | ADCSRA |= (1<<ADSC); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
23 | // wait for the result |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
24 | loop_until_bit_is_clear(ADCSRA, ADSC); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
25 | return ADC; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
26 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
27 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
28 | // initialize the ADC |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
29 | void initADC() { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
30 | ADCSRA |= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
31 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
32 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
33 | // select reference |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
34 | // value can be "VCC", "1.1V or "2.56V" |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
35 | // returns 1 on success |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
36 | uint8_t setAREF(char* name) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
37 | if (strcmp(name,"VCC")==0) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
38 | adcReference=(1<<REFS0); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
39 | else if (strcmp(name,"1.1V")==0) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
40 | adcReference=(1<<REFS1); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
41 | else if (strcmp(name,"2.56V")==0) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
42 | adcReference=((1<<REFS1) | (1<<REFS0)); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
43 | else if (strcmp(name,"EXT")==0) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
44 | adcReference=0; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
45 | else |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
46 | return 0; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
47 | // Perform one sample to initialize the ADC on the new reference |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
48 | ADMUX = adcReference; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
49 | ADCSRA |= (1<<ADSC); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
50 | loop_until_bit_is_clear(ADCSRA, ADSC); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
51 | (ADC); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
52 | return 1; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
53 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
54 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
55 | #ifdef EXT_AREF |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
56 | // get current current voltage |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
57 | // This assumes that VCC is 5.0 Volt. |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
58 | float getAREF(void) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
59 | uint8_t aref=ADMUX & ((1<<REFS1) | (1<<REFS0)); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
60 | if (aref == 0) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
61 | return EXT_AREF/1024; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
62 | else if (aref == (1<<REFS0)) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
63 | return 5.00/1024; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
64 | else if (aref == (1<<REFS1)) |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
65 | return 1.10/1024; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
66 | else |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
67 | return 2.56/1024; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
68 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
69 | #endif |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
70 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
71 | |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
72 | uint16_t getADC_smooth(uint8_t channel, uint8_t samples) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
73 | uint16_t result = 0; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
74 | unsigned char i; |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
75 | for (i=0; i<samples; i++) { |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
76 | result += getADC(channel); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
77 | } |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
78 | return (result / samples); |
9b7de464f0ea
initial commit - controller1 working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff
changeset
|
79 | }; |