| 1234567891011121314151617181920212223242526272829 |
- #include "vbus_sensor.h"
- #include "bsp/bsp.h"
- #include "bsp/adc.h"
- #include "math/fast_math.h"
- static vbus_t _vbus;
- void vbus_sensor_init(void){
- _vbus.voltage_avg = 0;
- _vbus.avg_count = 5;
- vbus_sample_voltage();
- }
- void vbus_sample_voltage(void){
- u32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
- _vbus.voltage_avg = ((float)vadc)/(65536.0f) * ADC_REFERENCE_VOLTAGE / VBUS_PARTITIONING_FACTOR;
- if (_vbus.voltage_filted == 0.0f) {
- _vbus.voltage_filted = _vbus.voltage_avg;
- }else {
- LowPass_Filter(_vbus.voltage_filted, _vbus.voltage_avg, 0.1f);
- }
- }
- float vbus_get_voltage(void){
- return _vbus.voltage_avg;
- }
|