aboutsummaryrefslogtreecommitdiff
path: root/platforms/avr/drivers/analog.h
diff options
context:
space:
mode:
Diffstat (limited to 'platforms/avr/drivers/analog.h')
-rw-r--r--platforms/avr/drivers/analog.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/platforms/avr/drivers/analog.h b/platforms/avr/drivers/analog.h
new file mode 100644
index 000000000..058882450
--- /dev/null
+++ b/platforms/avr/drivers/analog.h
@@ -0,0 +1,53 @@
1/* Copyright 2015 Jack Humbert
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19#include <stdint.h>
20#include "quantum.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25void analogReference(uint8_t mode);
26int16_t analogRead(uint8_t pin);
27
28int16_t analogReadPin(pin_t pin);
29uint8_t pinToMux(pin_t pin);
30
31int16_t adc_read(uint8_t mux);
32#ifdef __cplusplus
33}
34#endif
35
36#define ADC_REF_EXTERNAL 0 // AREF, Internal Vref turned off
37#define ADC_REF_POWER _BV(REFS0) // AVCC with external capacitor on AREF pin
38#define ADC_REF_INTERNAL (_BV(REFS1) | _BV(REFS0)) // Internal 2.56V Voltage Reference with external capacitor on AREF pin (1.1V for 328P)
39
40// These prescaler values are for high speed mode, ADHSM = 1
41#if F_CPU == 16000000L || F_CPU == 12000000L
42# define ADC_PRESCALER (_BV(ADPS2) | _BV(ADPS1)) // /64
43#elif F_CPU == 8000000L
44# define ADC_PRESCALER (_BV(ADPS2) | _BV(ADPS0)) // /32
45#elif F_CPU == 4000000L
46# define ADC_PRESCALER (_BV(ADPS2)) // /16
47#elif F_CPU == 2000000L
48# define ADC_PRESCALER (_BV(ADPS1) | _BV(ADPS0)) // /8
49#elif F_CPU == 1000000L
50# define ADC_PRESCALER _BV(ADPS1) // /4
51#else
52# define ADC_PRESCALER _BV(ADPS0) // /2
53#endif