forked from microfarad-de/Adc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adc.h
144 lines (123 loc) · 3.38 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Abstraction layer for the ATmega328p ADC
* Non-blocking read the ADC output as an alternative
* to the blocking analogRead() method.
*
* This source file can be found under:
* http://www.github.com/arduino-library/Adc
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
* http://www.github.com/arduino-library
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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/>.
*/
#ifndef __ADC_H
#define __ADC_H
#include <Arduino.h>
#define ADC_NUM_PINS 8 /* Maximum number of ADC pins */
/*
* ADC prescaler values
*/
enum AdcPrescaler_t {
ADC_PRESCALER_2 = 1,
ADC_PRESCALER_4 = 2,
ADC_PRESCALER_8 = 3,
ADC_PRESCALER_16 = 4,
ADC_PRESCALER_32 = 5,
ADC_PRESCALER_64 = 6,
ADC_PRESCALER_128 = 7
};
/*
* Analog pin numbers
* Note:
* Arduino macro A0 = ADC_PIN0 + 14
* the same applies for A1, A2...
*/
enum AdcPin_t {
ADC_PIN0 = 0,
ADC_PIN1 = 1,
ADC_PIN2 = 2,
ADC_PIN3 = 3,
ADC_PIN4 = 4,
ADC_PIN5 = 5,
ADC_PIN6 = 6,
ADC_PIN7 = 7
};
/*
* Analog reference sources
*/
enum AdcReference_t {
ADC_EXTERNAL = 0, /* external input */
ADC_DEFAULT = _BV(REFS0), /* AVcc */
ADC_INTERNAL = _BV(REFS0) | _BV(REFS1) /* internal 1.1V */
};
/*
* ADC class definition
*/
class AdcClass {
public:
/*
* Initialize the ADC
*/
void initialize (
AdcPrescaler_t prescaler, // ADC prescaler value
AdcReference_t reference, // ADC reference voltage
uint16_t avgSamples, // Number of averaged samples
uint8_t numPins, // Number of ADC pins to be read
AdcPin_t *adcPins // Array of ADC pin numbers of size numPins
);
/*
* Start ADC conversion
*/
void start (
AdcPin_t adcPin // ADC pin number (0..7 for ATmega328p)
);
/*
* Read ADC result
* Return value:
* -1 : no result available yet
* 0..1023 : ADC result
*/
int16_t readVal (void);
/*
* Read all preset ADC inputs
* When called repeatedly it will cycle through the pre-configuraed ADC channels defined by numPins,
* get the ADC results, average them as defined in avgSamples and store them in the result array.
* Return value:
* false : no result available yet
* true : all the channels have been read and averaged
*/
bool readAll (void);
/*
* ADC result can be read from this array
*/
uint32_t result[ADC_NUM_PINS];
private:
bool working = false;
AdcReference_t reference;
AdcPin_t adcPins[ADC_NUM_PINS];
uint16_t avgSamples = 1;
uint16_t avgCount = 0;
uint8_t numPins = 0;
uint8_t pinIdx = 0;
};
/*
* ADC class instantiated as a singleton
*/
extern AdcClass Adc;
#endif // __ADC_H