ntc_sensor.c 597 B

12345678910111213141516171819202122232425
  1. #include "ntc_sensor.h"
  2. #include "bsp/bsp.h"
  3. #include "bsp/adc.h"
  4. static ntc_t _ntc;
  5. void ntc_sensor_init(void) {
  6. _ntc.temp_avg = 0;
  7. _ntc.low_pass_filter = 0.5f;
  8. ntc_sensor_sample();
  9. }
  10. void ntc_sensor_sample(void){
  11. u16 w_temp = adc_sample_regular_channel(MOTOR_TEMP_CHAN, 16);
  12. w_temp -= ( s32 )(V0_V *65536/ ADC_REFERENCE_VOLTAGE );
  13. w_temp *= (ADC_REFERENCE_VOLTAGE/dV_dT);
  14. w_temp = w_temp / 65536 + ( s32 )( T0_C );
  15. _ntc.temp_avg = w_temp * _ntc.low_pass_filter + _ntc.temp_avg * (1.0f - _ntc.low_pass_filter);
  16. }
  17. int ntc_sensor_temperature(void){
  18. return _ntc.temp_avg;
  19. }