analog.h 1.7 KB

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