-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
78 lines (67 loc) · 1.51 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
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
75
76
77
/**
* ----------------------------------------------------------------------------
*
* "THE ANY BEVERAGE-WARE LICENSE" (Revision 42 - based on beer-ware license):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a be(ve)er(age) in return. (I don't
* like beer much.)
*
* Matthias Kleemann
*
* ----------------------------------------------------------------------------
*
* \file adc.c
*
* \date Created: 24.02.2012 19:59:06
* \author Matthias Kleemann
**/
#include "util/util.h"
#include "adc.h"
/**
* @brief initializes the ADC
*/
void adc_init(void)
{
PIN_SET_INPUT(ADC_INPUT_PIN);
ADMUX = ADC_REF_SELECT | ADC_INPUT_CHANNEL;
ADCSRA = ADC_PRESCALER;
#if defined (ADC_8BIT_RESOLUTION) || defined (ADC_LEFT_ALIGNED)
ADMUX |= (1 << ADLAR);
#endif
}
/**
* @brief get ADC value
*/
uint16_t adc_get(void)
{
// start conversion
ADCSRA |= (1 << ADSC);
// wait for new conversion to be ready
while (!(ADCSRA & (1 << ADIF)))
;
// get value
uint16_t retVal = ADCL;
#ifdef ADC_8BIT_RESOLUTION
retVal = ADCH;
#else
retVal |= (uint16_t) (ADCH << 8);
#endif
// clear interrupt flag by writing 1
ADCSRA |= (1 << ADIF);
return retVal;
}
/**
* @brief enables ADC for power save
*/
void adc_enable()
{
ADCSRA |= (1 << ADEN);
}
/**
* @brief disables ADC for power save
*/
void adc_disable()
{
ADCSRA &= ~(1 << ADEN);
}