-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADC_SingleConversion.ino
74 lines (54 loc) · 1.6 KB
/
ADC_SingleConversion.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Arduino Uno Analog-to-Digital Conversion (ADC)
// by setting the Atmega328 registers directly
// This is an example of a single conversion
// using the A0 port.
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
const float extVolt = 5.14; //voltage measured at Arduino's voltage regulator.
byte low;
byte high;
word res_w;
float res_f;
// registers used for ADC:
// ADMUX: REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0
// ADCSRA: ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
void setup() {
Serial.begin(9600);
// select channel 0 (pin A0 on Arduino Uno)
ADMUX &= 0xF0;
// select Avcc as the reference voltage
cbi(ADMUX,REFS1);
sbi(ADMUX,REFS0);
// right justify result
cbi(ADMUX, ADLAR);
// set the ADC clock frequency prescaler = 128, ~125KHz w/16MHz Crystal
ADCSRA |= 0x07;
// disable the Power Reduction ADC bit, PRADC
cbi(PRR,PRADC);
// enable the ADC
sbi(ADCSRA,ADEN);
}
void loop() {
// start a conversion
sbi(ADCSRA,ADSC);
// wait for the conversion to complete
while( ( ADCSRA & (1<<ADIF) ) == 0 );
// read low and high values
low = ADCL; // read low byte first
high = ADCH; // read high byte
// clear interrupt flag
sbi(ADCSRA,ADIF);
// combine high / low into single value
res_w = word(high,low);
// convert to a voltage value
res_f = extVolt*(float(res_w)/1023);
// print the result
Serial.print("Voltage=");
Serial.println(res_f, DEC);
// delay one sec and start over.
delay(1000);
}