-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperipheral_adc.c
53 lines (40 loc) · 1.31 KB
/
peripheral_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
47
48
49
50
51
52
53
#define ENABLE_BIT_DEFINITIONS
#include <ioavr.h>
#include <iom128rfa1.h>
#include "snappy.h"
#define REFERENCE_VOLTAGE ((1 << REFS1) | (1 << REFS0))
int16_t take_sample()
{
// take a sample
ADCSRA |= (1 << ADSC) | (1 << ADIF);
while ( ADCSRA & (1<<ADSC) ) ;
// return when clear
return ADC;
}
// In this example, we are assuming we're only interested in taking
// samples from a single ended ADC (one of channels ADC0-7).
void enable_adc_and_set_channel(int16_t channel)
{
ADCSRA &= (1 << ADEN);
//ADMUX = (channel & 0x1F);
ADMUX = REFERENCE_VOLTAGE | (channel & 0x1F);
// select the ADC we want
ADCSRB &= ~(1 << MUX5);
// start one converstion to throw away to let the power supply settle
ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADPS1);
// Wait for conversion to complete
while (ADCSRA & (1 << ADSC)) ;
// Now reset the sample rate to 4MHz
ADCSRA = (1 << ADEN) | (1 << ADPS1);
}
int16_t take_averaged_sample(int16_t channel)
{
#define NUM_SAMPLES_PER_AVERAGE 20
int16_t sum = 0;
enable_adc_and_set_channel(channel);
for ( int i = 0; i < NUM_SAMPLES_PER_AVERAGE; ++i )
{
sum += take_sample();
}
return sum / NUM_SAMPLES_PER_AVERAGE;
}