-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.h
92 lines (74 loc) · 2.09 KB
/
adc.h
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
YAAB - Yet Another Autococker Board
Arduino based autococker board developed around the platform and ATMEL AVR
chips
Copyright (C) 2012 Dan Silk
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
// ADC Completes faster
#define FASTER_ADC
///
/// Analog to Digital
///
///
/// Init ADC
///
// initialize adc
inline void adc_init()
{
// AREF = AVcc
ADMUX = (1<<REFS0);
// 8-bit resolution
ADMUX |= (1 << ADLAR);
// ADC Auto Trigger Enable
ADCSRA |= (1 << ADATE);
// Clearing all of ADCSRB will enable free running mode
ADCSRB = 0x0;
#if defined FASTER_ADC
// ADC Enable and prescaler of 16 and interrupt ((1 << ADIE))
// 16000000/16 = 1000000
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1 << ADIE);
#else
// ADC Enable and prescaler of 128 and interrupt ((1 << ADIE))
// 16000000/128 = 125000
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1 << ADIE);
#endif
}
///
/// Start Read ADC Channel
///
void adc_start_read(uint8_t ch)
{
// select the corresponding channel 0~7
// ANDing with '7' will always keep the value
// of 'ch' between 0 and 7
ch &= 0x7; // AND operation with 7
ADMUX = (ADMUX & 0xF8) | ch; // clears the bottom 3 bits before ORing
// start single conversion
// write '1' to ADSC
ADCSRA |= (1<<ADSC);
}
void adc_stop_all()
{
// stop auto run
ADCSRA &= ~(1<<ADATE);
// stop conversion
ADCSRA &= ~(1<<ADSC);
}
///
/// Analog to Digital Conversion Complete
///
ISR(ADC_vect)
{
onADCReadComplete();
}