ntc_sensor.c 619 B

1234567891011121314151617181920212223242526
  1. #include "ntc_sensor.h"
  2. #include "bsp/bsp.h"
  3. static ntc_t _ntc;
  4. void ntc_sensor_init(void) {
  5. _ntc.temp_avg = 0;
  6. _ntc.low_pass_filter = 0.5f;
  7. HAL_ADC1_ChanConfig(TEMP_SENSOR_ADC_CHANNEL);
  8. ntc_sensor_sample();
  9. }
  10. void ntc_sensor_sample(void){
  11. u16 w_temp = HAL_ADC1_ReadValue(TEMP_SENSOR_ADC_CHANNEL);
  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. }