vbus_sensor.c 630 B

1234567891011121314151617181920212223242526272829
  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_avg = 0;
  8. _vbus.avg_count = 5;
  9. vbus_sample_voltage();
  10. }
  11. void vbus_sample_voltage(void){
  12. u32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
  13. _vbus.voltage_avg = ((float)vadc)/(65536.0f) * ADC_REFERENCE_VOLTAGE / VBUS_PARTITIONING_FACTOR;
  14. if (_vbus.voltage_filted == 0.0f) {
  15. _vbus.voltage_filted = _vbus.voltage_avg;
  16. }else {
  17. LowPass_Filter(_vbus.voltage_filted, _vbus.voltage_avg, 0.1f);
  18. }
  19. }
  20. float vbus_get_voltage(void){
  21. return _vbus.voltage_avg;
  22. }