potentiometer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Potentiometer power control
  3. *
  4. * Copyright (c) 2018-2020 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "compat.h"
  21. #include "debug.h"
  22. #include "potentiometer.h"
  23. #include "main.h"
  24. /* Potentiometer enable pin. */
  25. #if IS_ATMEGAx8
  26. # define POTEN_PORT PORTC
  27. # define POTEN_DDR DDRC
  28. # define POTEN_LO_BIT 4
  29. # define POTEN_HI_BIT 5
  30. #else
  31. # define POTEN_PORT PORTB
  32. # define POTEN_DDR DDRB
  33. # define POTEN_LO_BIT 2
  34. # define POTEN_HI_BIT 3
  35. #endif
  36. /* Enable/disable the power supply to the potentiometer. */
  37. void potentiometer_enable(bool enable)
  38. {
  39. if (USE_DEEP_SLEEP) {
  40. if (enable) {
  41. /* Turn pot power supply on. */
  42. /* HI = driven */
  43. POTEN_PORT |= (1 << POTEN_HI_BIT);
  44. POTEN_DDR |= (1 << POTEN_HI_BIT);
  45. /* LO = driven */
  46. POTEN_PORT &= (uint8_t)~(1 << POTEN_LO_BIT);
  47. POTEN_DDR |= (1 << POTEN_LO_BIT);
  48. } else {
  49. /* Switch pot power supply to high impedance input. */
  50. if (ADC_INVERT) {
  51. /* HI = driven */
  52. POTEN_PORT |= (1 << POTEN_HI_BIT);
  53. POTEN_DDR |= (1 << POTEN_HI_BIT);
  54. /* LO = high impedance */
  55. POTEN_DDR &= (uint8_t)~(1 << POTEN_LO_BIT);
  56. POTEN_PORT &= (uint8_t)~(1 << POTEN_LO_BIT);
  57. } else {
  58. /* LO = driven */
  59. POTEN_PORT &= (uint8_t)~(1 << POTEN_LO_BIT);
  60. POTEN_DDR |= (1 << POTEN_LO_BIT);
  61. /* HI = high impedance */
  62. POTEN_DDR &= (uint8_t)~(1 << POTEN_HI_BIT);
  63. POTEN_PORT &= (uint8_t)~(1 << POTEN_HI_BIT);
  64. }
  65. }
  66. }
  67. }