qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

analog.h (1781B)


      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 "gpio.h"
     21 
     22 #ifdef __cplusplus
     23 extern "C" {
     24 #endif
     25 void analogReference(uint8_t mode);
     26 
     27 int16_t analogReadPin(pin_t pin);
     28 uint8_t pinToMux(pin_t pin);
     29 
     30 int16_t adc_read(uint8_t mux);
     31 #ifdef __cplusplus
     32 }
     33 #endif
     34 
     35 #define ADC_REF_EXTERNAL 0                         // AREF, Internal Vref turned off
     36 #define ADC_REF_POWER _BV(REFS0)                   // AVCC with external capacitor on AREF pin
     37 #define ADC_REF_INTERNAL (_BV(REFS1) | _BV(REFS0)) // Internal 2.56V Voltage Reference with external capacitor on AREF pin (1.1V for 328P)
     38 
     39 // These prescaler values are for high speed mode, ADHSM = 1
     40 #if F_CPU == 16000000L || F_CPU == 12000000L
     41 #    define ADC_PRESCALER (_BV(ADPS2) | _BV(ADPS1)) // /64
     42 #elif F_CPU == 8000000L
     43 #    define ADC_PRESCALER (_BV(ADPS2) | _BV(ADPS0)) // /32
     44 #elif F_CPU == 4000000L
     45 #    define ADC_PRESCALER (_BV(ADPS2)) // /16
     46 #elif F_CPU == 2000000L
     47 #    define ADC_PRESCALER (_BV(ADPS1) | _BV(ADPS0)) // /8
     48 #elif F_CPU == 1000000L
     49 #    define ADC_PRESCALER _BV(ADPS1) // /4
     50 #else
     51 #    define ADC_PRESCALER _BV(ADPS0) // /2
     52 #endif