4093 views|7 replies

72

Posts

0

Resources
The OP

Weird problem with M8while loop [Copy link]

I am using STM8, first I used the touch button
Then I want to use TIM2 interrupt to make a delay.
The TIM2 interrupt is started normally, and the interrupt program can normally decrement the global variable "delay_100ms".
When I was about to use the loop, I found that the loop was bad
while(delay_100ms>1){}//It doesn't work while
(delay_100ms>1){}//It doesn't work
while(1)////It doesn't work
{
if(delay_100ms==0)
break;
}

while(delay_100ms>1)//It works
{
if(delay_100ms==0)
break;
}
What is the problem? Is it the compiler? Or did I write something wrong?
If it is like this, C cannot be written.
This post is from stm32/stm8

Latest reply

                                 Thanks to so many people for their suggestions. It was a volatile problem. I solved it by changing the definition. I remember that I didn't have this problem when I used KEIL51. I paid attention to it when I used ARM. I didn't expect that I still need to pay attention to this problem with M8. I remember it this time. Thank you!  Details Published on 2011-1-19 15:22

69

Posts

0

Resources
2
If delay_100ms is a flag, it is recommended to use _Bool to define the type of this variable.

Then try

while(1)
{
if(delay_100ms==0)
break;
}


while(delay_100ms)
{
if(delay_100ms==0)
break;
}
This post is from stm32/stm8

80

Posts

0

Resources
3
while(delay_100ms)
{
if(delay_100ms==0)
break;
}
It is better not to use this sentence
This post is from stm32/stm8

64

Posts

0

Resources
4


I don't want to waste my breath on this kind of question.

In a word:
Do you know that C language has the keyword volatile?
This post is from stm32/stm8

80

Posts

0

Resources
5
The interrupt program can normally decrement the global variable "delay_100ms"


Is there a way to initialize a value for delay_100ms? Because no value is given, it is initialized to 0
This post is from stm32/stm8

63

Posts

0

Resources
6
                                 top
This post is from stm32/stm8

66

Posts

0

Resources
7


while(delay_100ms>1){}/

means that if delay_100ms is greater than 1, the function will wait in an infinite loop.
In addition, there is another extended meaning: as long as the function does not write a new value to delay_100ms, the function will assume that the value of delay_100ms will never change, because delay_100ms is not defined as volatile.

This post is from stm32/stm8

84

Posts

0

Resources
8
Thanks to so many people for their suggestions. It was a volatile problem. I solved it by changing the definition.
I remember that I didn't have this problem when I used KEIL51. I paid attention to it when I used ARM. I didn't expect that I still need to pay attention to this problem with M8.
I remember it this time. Thank you!
This post is from stm32/stm8

Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list