暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * adc.c
  3. *
  4. * Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
  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 3 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. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include "pico/stdlib.h"
  20. #include "hardware/adc.h"
  21. #include "adc.h"
  22. #define ADC_NUM 2
  23. #define ADC_PIN (26 + ADC_NUM)
  24. #define ADC_VREF 3.3
  25. #define ADC_RANGE (1 << 12)
  26. #define ADC_CONVERT (ADC_VREF / (ADC_RANGE - 1))
  27. #define BAT_R1 10000.0f
  28. #define BAT_R2 18000.0f
  29. void bat_init(void) {
  30. adc_init();
  31. adc_gpio_init( ADC_PIN);
  32. adc_select_input( ADC_NUM);
  33. }
  34. float bat_get(void) {
  35. float v_adc = adc_read() * ADC_CONVERT;
  36. // Vadc = Vbat * R2 / (R1 + R2)
  37. float v_bat = v_adc / (BAT_R2 / (BAT_R1 + BAT_R2));
  38. return v_bat;
  39. }