当前位置:网站首页>容易让单片机程序跑飞的原因
容易让单片机程序跑飞的原因
2022-06-11 15:09:00 【一只藤井树】
1、意外中断
是否打开了某个中断,但是没有响应和清除中端标志,导致程序一直进入中断,造成死机假象。
2、中断变量处理不妥
若定义某些会在中断中修改的全局变量,这时要注意两个问题:首先为了防止编译器优化中断变量,要在这些变量定义时前加volatile;其次在主循环中读取中断变量前应该首先关闭全局中断,防止读到一半被中断给修改了,读完之后再打开全局中断,否则出现造成数据乱套。
3、地址溢出,常见错误为指针操作错误
着重说的是数组下标使用循环函数中循环变量,如果循环变量没控制好则会出现数组下标越界,意外修改系统的寄存器造成死机,这种情况下如果死机说明运气好,否则后面不知道发生什么头疼的事。
4、无条件的死循环
比如使用while(x),等待电平变化,正常情况下x都会变成0,就怕万一,因此最好加上时间限制。
5、看门狗没有关闭
有的单片机即使没使用看门狗开机时也有可能意外自动开启了最小周期的看门狗,导致软件不断复位,造成死机。这个要看芯片手册,最好在程序复位后首先应该显式清除看门狗再关闭看门狗。
6、堆栈溢出
最难查找的问题,对于容量小的单片机,尽量减少函数调用层级,减少局部变量,从而减少压栈的时候所需的空间。当你把以上几条都试过不能解决问题,试一试把你的被调用少函数直接内置到调用的地方并且把占用RAM大的局部变量改成全局变量,试一试说不定就可以了。
在编程时应多多注意这几个方面
边栏推荐
- C语言简易版webserver
- Guess numbers games
- A brief talk on the feelings after working at home | community essay solicitation
- Mysql database optimization details
- Hashicopy之nomad应用编排方案05(访问web页面)
- 浅谈居家办公后的感想| 社区征文
- Flutter 3.0 was officially released: it stably supports 6 platforms, and byte jitter is the main user
- Hot seek tiger, a list of eco economic models
- 浙江大学搞出了一款无人机,自动规避障碍,像鸟一样穿过树林,真正的蜂群来了...
- When open source meets KPI, globalization vs localization, how can the ideal and reality of open source be reconciled?
猜你喜欢

C # - how to add and read appsetting in the console application JSON file

Tencent interviewers share their interview experience, how to evaluate the interviewers' technical and personal comprehensive quality, and give you some suggestions on the interview
[mysql_12] MySQL data types

What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger

Cartoon: interesting "cake cutting" problem

Summary of some classic embedded C interview questions

Analyse approfondie de la conception du système relationnel du Groupe de cercles

LoveLive! Published an AI paper: generating models to write music scores automatically
![[SystemVerilog interface] ~ interface](/img/dc/0a9750cace1460af772e2f3f6a8763.png)
[SystemVerilog interface] ~ interface

Oauth2的理解
随机推荐
Avenue to simplicity | how to configure self attention for vit is the most reasonable?
Determine whether a string contains the specified string (verified)
Why do I need the public static void main (string[] args) method?
The server prevents repeated payment of orders
Leetcode daily question - Search insertion position
Flutter 3.0 was officially released: it stably supports 6 platforms, and byte jitter is the main user
Leetcode daily question - plus one
新华三交换机系统基本配置命令
NVIDIA R & D director: how does AI improve chip design?
数据库优化
百度某离职员工跳槽字节被判赔107万元;苹果谷歌微软拟“干掉”密码;传吉利已收购魅族|Q资讯
中国技术出海,TiDB 数据库海外探索之路 | 卓越技术团队访谈录
大道至简 | 设计 ViT 到底怎么配置Self-Attention才是最合理的?
Repository Manager之Nexus配置yum仓库
Zhejiang University has developed a UAV, which can automatically avoid obstacles and walk through the woods like a bird. The real swarm is coming
Basic configuration command of Xinhua 3 switch system
Summary of some classic embedded C interview questions
In the "ten billion blue ocean" database, each player can find a boat | c-position face-to-face
How to batch insert 100000 pieces of data
2022 simulated 100 questions and simulated examination of quality officer municipal direction post skills (Quality Officer) examination