-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
47 lines (41 loc) · 1017 Bytes
/
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
43
44
45
46
/* $Id: adc.c $
* Functions for the analog digital converter - used to measure supply voltage
*/
#include <avr/io.h>
#include <avr/power.h>
#include "adc.h"
void adc_init(void)
{
/* Disable ADC for now (gets reenabled for the measurements */
PRR0 |= _BV(PRADC);
}
void adc_power(uint8_t p)
{
if (p) {
/* Reenable ADC */
PRR0 &= (uint8_t)~_BV(PRADC);
/* Select prescaler for ADC, disable autotriggering, turn off ADC */
ADCSRA = _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
/* Select reference voltage (internal 2.56V) and pin A3 on the feather
* (which is ADC4, hooray for consistency!) */
ADMUX = _BV(REFS0) | _BV(REFS1) | 4;
} else {
/* Send ADC to sleep */
ADCSRA &= (uint8_t)~_BV(ADEN);
PRR0 |= _BV(PRADC);
}
}
/* Start ADC conversion */
void adc_start(void)
{
ADCSRA |= _BV(ADEN) | _BV(ADSC);
}
uint16_t adc_read(void)
{
/* Wait for ADC */
while ((ADCSRA & _BV(ADSC))) { }
/* Read result */
uint16_t res = ADCL;
res |= (ADCH << 8);
return res;
}