-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadc_avr.h
57 lines (49 loc) · 1.81 KB
/
adc_avr.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
/*
* adc_avr.h - Interface to configure ADC speed and capture data for Atmel AVR chips.
*
* Copyright (C)2015 Laurentiu Badea
*
* This file may be redistributed under the terms of the MIT license.
* A copy of this license has been included with this distribution in the file LICENSE.
*
* http://forum.arduino.cc/index.php/topic,6549.0.html
*/
#ifndef ADC_AVR_H_
#define ADC_AVR_H_
#include "adc.h"
#define ADC_BITS 10
#define ADC_CLOCK_TO_SAMPLING 13
#define ADC_AREF_MV 5000
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
class ADCInput : public ADCBase {
protected:
uint8_t curMode;
bool setPrescaler(uint8_t mode);
public:
static const uint8_t prescalers[];
uint8_t input; // analog input port
uint8_t bits; // ADC resolution in bits
bool init(uint8_t input=0, uint8_t mode=0);
uint8_t getModeCount(); // returns the number of available rates
bool setMode(uint8_t mode); // set sampling rate from the available values
uint32_t getClock(); // Hz for information purposes only
uint32_t getSampleRate(); // Hz for information purposes only
void readMulti(uint16_t *buffer, unsigned size); // multi-sample read
inline uint16_t read() __attribute__((always_inline)){
return analogRead(input);
}
inline uint16_t readFast() __attribute__((always_inline)){
// this runs just the conversion part of analogRead() without the setup
sbi(ADCSRA, ADSC); // ADSC=AD Start Conversion
loop_until_bit_is_clear(ADCSRA, ADSC); // Conversion finished
return ADCL | (ADCH << 8);
}
uint16_t calibrateAREF();
};
#endif /* ADC_AVR_H_ */