vbus_sensor.c 568 B

123456789101112131415161718192021222324252627
  1. #include "vbus_sensor.h"
  2. #include "bsp/bsp.h"
  3. #include "bsp/adc.h"
  4. #include "math/fast_math.h"
  5. static vbus_t _vbus;
  6. void vbus_sensor_init(void){
  7. _vbus.voltage = 0;
  8. _vbus.voltage_filted = MAX_VBUS_VOLTAGE;
  9. _vbus.avg_count = 5;
  10. vbus_sample_voltage();
  11. }
  12. void vbus_sample_voltage(void){
  13. u32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
  14. _vbus.voltage = ((float)vadc)/(4096.0f) * ADC_REFERENCE_VOLTAGE * 45 / 1000; //1:44
  15. LowPass_Filter(_vbus.voltage_filted, _vbus.voltage, 0.1f);
  16. }
  17. float vbus_get_filted_voltage(void){
  18. return _vbus.voltage_filted;
  19. }