当前位置:网站首页>关于keil中,while循环条件不成立却无法跳出的问题
关于keil中,while循环条件不成立却无法跳出的问题
2022-06-11 17:54:00 【Tristan Tsai】
如下图中的while循环
其中变量 如下定义
static uint8_t nb_uart_tx_done = 0;且在中断中将其赋值1,发现当中断中已经赋值,这个while也跳转不出来,如下是仿真观察到的变量和while不跳出情形

查阅资料发现是这么个情况:
在进入循环的时候,实际上是将A从内存加载到寄存器里面运行的,在整个循环中,A这个变量都只是在读取寄存器里面的值。
而当进入中断的时候,中断里面会从内存加载A到寄存器,修改完之后又存到内存里,然后退出中断,再回到循环里面。但这个时候循环里使用的A并没有重新从内存加载A这个变量,所以一直都在循环里面。
解决方法:
nb_uart_tx_done 变量的定义前加上volatile,详细理解自行查找volatile的用法。
static volatile uint8_t nb_uart_tx_done = 0; 如此一来就不会有前面的加载到寄存器的问题了
一般说来,volatile用在如下的几个地方:
1、中断服务程序中修改的供其它程序检测的变量需要加volatile;
2、多任务环境下各任务间共享的标志应该加volatile;
3、存储器映射的硬件寄存器通常也要加volatile说明,因为每次对它的读写都可能有不同意义;另外,以上这几种情况经常还要同时考虑数据的完整性(相互关联的几个标志读了一半被打断了重写),在1中可以通过关中断来实现,2 中可以禁止任务调度,3中则只能依靠硬件的良好设计了。
边栏推荐
- Hash table, inheritance
- GB gb28181 protocol video platform easygbs adds or deletes offline channels
- Install MariaDB 10.5.7 (tar package installation)
- Hwang
- Hello go (XV). Go language common standard library V
- 安装mariadb 10.5.7(tar包安装)
- Spring 2021 daily question [week7 not finished]
- 【MapReduce】一个完整MR程序案例教你如何用IDEA打包及运行
- [collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)
- sqli-labs通关嘿嘿~
猜你喜欢

upload-labs通关未半而中道崩殂

ctf入门

【先收藏,早晚用得到】100个Flink高频面试题系列(二)

送给大模型的「高考」卷:442人联名论文给大模型提出204个任务,谷歌领衔

mariadb spider分片引擎初体验
![Spring 2021 daily question [week5 not finished]](/img/bd/35a8e0ded3b1a0727415c4cd95e781.jpg)
Spring 2021 daily question [week5 not finished]

Ctfhub SQL Boolean blind annotation
![Spring 2021 daily question [week6 not finished]](/img/32/74cff6f057ef9a7941fd6a41dc6635.jpg)
Spring 2021 daily question [week6 not finished]

How to learn and self-study

10 ways to reset any user password
随机推荐
jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
Three steps of ffmpeg CBR precise bitstream control
SISO Decoder for min-sum(补充章节2)
6-8 reading and writing of structured files 1
智能化整体图例,布线、安防、广播会议、电视、楼宇、消防、电气图的图例【转自微信公众号弱电课堂】
Introduction to social engineering practice
TestPattern error
Tle6389-2g V50's unique pwm/pfm control scheme has a duty cycle of up to 100%, forming a very low differential pressure - keshijin mall
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration
spawn ./ gradlew EACCES at Process. ChildProcess._ handle. onexit
【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
Retrofit source code analysis
Hello go (XV). Go language common standard library V
Experiment 2: write a program and verify that the linear table sequence represents all operations
[solution] codeforces round 798 (Div. 2)
Can 400 fans earn 20W a month? How did you do it?
Système d'information sur les menaces à la sécurité des réseaux
10 ways to reset any user password
Bracket generation ---2022/02/25
Seeing the sudden death of a 28 year old employee, I was silent