当前位置:网站首页>On the problem that the while loop condition in keil does not hold but cannot jump out

On the problem that the while loop condition in keil does not hold but cannot jump out

2022-06-11 18:20:00 Tristan Tsai

As shown in the figure below while loop

Among variables As defined below

static uint8_t nb_uart_tx_done = 0;

And assign a value to it in the interrupt 1, It is found that when the interrupt has been assigned , This while I can't jump out , The following are the variables observed by the simulation and while Do not jump out of the situation

Looking up the data, we found that this is the case :

        When entering the cycle , It's actually going to be A Loaded from memory into registers to run , In the whole cycle ,A This variable is just reading the value in the register .
        And when entering the interrupt , Interrupt will be loaded from memory A To register , After modification, it is stored in memory , Then exit the interrupt , Back in the loop . But it is used in the cycle at this time A No Reload from memory A This variable , So it's always in the loop .

resolvent :
  nb_uart_tx_done Add... Before the definition of variables volatile, Understand self search in detail volatile Usage of .

static volatile uint8_t nb_uart_tx_done = 0;

   In this way, there will be no previous problem of loading into registers
   As a general rule ,volatile Used in the following places :
  1、 The variables modified in interrupt service program for other programs to detect need to add volatile;
  2、 In a multitasking environment, the shared flag among tasks should be marked with volatile;
  3、 Memory mapped hardware registers are usually added with volatile explain , Because every time you read and write it, it may have a different meaning ; in addition , In the above cases, data integrity is often considered at the same time ( Several interrelated symbols are interrupted and rewritten in the middle of reading ), stay 1 It can be realized by closing the middle of the line ,2 Task scheduling can be disabled in ,3 We can only rely on the good design of hardware .

原网站

版权声明
本文为[Tristan Tsai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111754411264.html