1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Your fault description is very vague, but one obvious and major error is that you don't allocate enough space in temp_string, as the null terminator won't fit there if the string is more than one character. So sprintf might write outside the array. \$\endgroup\$
    – Klas-Kenny
    Commented Apr 23, 2022 at 20:53
  • \$\begingroup\$ You declared only a char with 2 characters, in C when the temperature is printed with sprintf the temp_string will contain the characters of your variable temperature and an additional character "\0" i.e. Null. (don't know where you defined that value or get that value of "temperature". Please show the temperature - where is it defined ? Give it a try with e.g. char temp_string[80]; \$\endgroup\$ Commented Apr 23, 2022 at 20:53
  • \$\begingroup\$ Thank you all. It worked like a charm :) I have been trying to fix that for hours... \$\endgroup\$ Commented Apr 23, 2022 at 21:00

1 Answer 1

1
\$\begingroup\$

The first error is not having enough room for the string. You need at least 3 characters to hold two characters and the terminating null character. Basically, if the temperature is larger than 9 degrees, it won't fit and trashes your stack.

The next error is trying to convert the number into string in interrupt context. Depending on what other things you are doing, it may not work. If you are sure that your sprintf() is re-entrant then go ahead, but in general, you just don't do conversions to stringa and comparing strings in interrupts.

The string conversion step is useless extra step anyway, as you can just compare the temperature with an integer and achieve the same result faster.

\$\endgroup\$
2
  • \$\begingroup\$ Actually I have used string comparison because of uint32_t variable type. I tried to make a comparison with an normal integer like 38 with the temperature value coming from the sensor (uint32_t type) but it did not work although i am able to output it to the LCD display. That's why I used string thing as the last resort. Thank you for your answer by the way. \$\endgroup\$ Commented Apr 23, 2022 at 21:08
  • 1
    \$\begingroup\$ I don't see why you need string comparison because of uint32_t variable type. The reasoning makes no sense, so you might be doing something wrong. \$\endgroup\$
    – Justme
    Commented Apr 23, 2022 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.