-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
42 lines (36 loc) · 1.08 KB
/
adc.c
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
#include "adc.h"
static uint16_t adc_read(void);
void adc_init(uint8_t adc_pin)
{
ADCSRA |= (1 << ADEN); // enable ADC
ADCSRA |= ((1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2)); // set prescaler 128
ADMUX |= (1 << REFS0); // select 1.1 V internal reference source
switch(adc_pin)
{
case PB0:
break;
case PB1:
ADMUX |= (1 << MUX0);
break;
case PB2:
ADMUX |= (1 << MUX1);
break;
case PB3:
ADMUX |= ((1 << MUX0) | (1 << MUX1));
break;
default:
break;
}
ADCSRA |= (1 << ADSC); // start first conversion
}
uint32_t adc_measure_millivolts()
{
uint16_t value_read = adc_read();
return ((uint32_t) value_read * AREF_MILLIVOLTS) / ADC_MAX;
}
static uint16_t adc_read()
{
uint16_t adc_result = ADC; // get result for 10-bit resolution
ADCSRA |= (1 << ADSC); // start next conversion
return adc_result;
}