analog.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #pragma once
  17. #include <stdint.h>
  18. void analogReference(uint8_t mode);
  19. int16_t analogRead(uint8_t pin);
  20. int16_t adc_read(uint8_t mux);
  21. #define ADC_REF_POWER (1<<REFS0)
  22. #define ADC_REF_INTERNAL ((1<<REFS1) | (1<<REFS0))
  23. #define ADC_REF_EXTERNAL (0)
  24. // These prescaler values are for high speed mode, ADHSM = 1
  25. #if F_CPU == 16000000L
  26. #define ADC_PRESCALER ((1<<ADPS2) | (1<<ADPS1))
  27. #elif F_CPU == 8000000L
  28. #define ADC_PRESCALER ((1<<ADPS2) | (1<<ADPS0))
  29. #elif F_CPU == 4000000L
  30. #define ADC_PRESCALER ((1<<ADPS2))
  31. #elif F_CPU == 2000000L
  32. #define ADC_PRESCALER ((1<<ADPS1) | (1<<ADPS0))
  33. #elif F_CPU == 1000000L
  34. #define ADC_PRESCALER ((1<<ADPS1))
  35. #else
  36. #define ADC_PRESCALER ((1<<ADPS0))
  37. #endif
  38. // some avr-libc versions do not properly define ADHSM
  39. #if defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  40. #if !defined(ADHSM)
  41. #define ADHSM (7)
  42. #endif
  43. #endif