当前位置:网站首页>关于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中则只能依靠硬件的良好设计了。
边栏推荐
- Jsfinder, wafw00f installation, nmap configuration (msvcr120.dll file is missing)
- 网络安全威胁情报体系
- 【新手上路常见问答】关于项目管理
- 6-3 reading articles (*)
- 網絡安全威脅情報體系
- [practical Script] obtain the line number of a file, and then delete the file content.
- How ZABBIX can customize MySQL monitoring items and trigger alarms
- [collect first and use it sooner or later] 49 Flink high-frequency interview questions series (I)
- Network Security Threat Intelligence System
- 6-2 reverse output of multiple integers recursion
猜你喜欢

How ZABBIX can customize MySQL monitoring items and trigger alarms

Reading summary of nacos2.x source code

Ffmpeg hard codec inter QSV

ctf入门

SISO Decoder for Repetition(补充章节4)

【C】 Compilation preprocessing and environment

Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall

Common shortcut keys for Hello go (x) and GoLand

【无标题】

【先收藏,早晚用得到】49个Flink高频面试题系列(二)
随机推荐
mariadb spider分片引擎初體驗
upload-labs通关未半而中道崩殂
Comparison of mongoose in express, KOA and egg
Initial experience of MariaDB spider sharding engine
Hello go (XV). Go language common standard library V
TestPattern error
SISO Decoder for SPC (补充章节1)
The tle6389 step-down DC-DC switch controller has high efficiency in the whole load range of 1mA to 2.5A - keshijin mall
6-6 batch sum (*)
[MapReduce] a complete Mr program case teaches you how to package and run with idea
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
Dynamic: capturing network dynamics using dynamic graph representation learning
Merge two ordered linked lists ---2022/02/24
vulhub
Valid parentheses ---2022/02/23
【无标题】
Ctfhub SQL Boolean blind annotation
Bracket generation ---2022/02/25
LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
剑指 Offer(第 2 版)