I am developing a project on STM32F103C6 and am new to it. I've implemented a timer with interrupt, which checks the temperature value measured by a sensor(LM35) every second, and if it is more than 38 degrees, the system triggers a buzzer.
The system triggers the buzzer as expected but the system cannot read temperature value from the sensor anymore. That is to say, it stucks in the timer.
Here is the piece of code which performs this part:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim2){
char temp_string[2];
sprintf(temp_string,"%d",temperature);
if(strcmp(temp_string,"38")>0){
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_SET);
}
else{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_RESET);
}
}
}
What can be the reason for the problem?
Any help is appreciated.
temp_string
, as the null terminator won't fit there if the string is more than one character. Sosprintf
might write outside the array. \$\endgroup\$