当前位置:网站首页>The reason why it is easy to let the single chip computer program fly

The reason why it is easy to let the single chip computer program fly

2022-06-11 15:11:00 A Fujii tree

1、 Unexpected interruption

Whether an interrupt is turned on , But there is no response and no clear middle end flag , Causes the program to enter the interrupt all the time , Cause a crash .

2、 Improper handling of interrupt variables

If you define some global variables that will be modified in the interrupt , At this time, we should pay attention to two problems : First of all, to prevent the compiler from optimizing the interrupt variables , Before the definition of these variables volatile; Secondly, before reading the interrupt variables in the main loop, the global interrupt should be closed first , Prevent half of the read from being interrupted for modification , Turn on global interrupt after reading , Otherwise, the data will be distorted .

3、 Address overflow , Common errors are pointer operation errors

The key point is that the array subscript uses the loop variable in the loop function , If the loop variable is not well controlled, the array subscript will be out of bounds , An unexpected modification of the system register caused a crash , In this case, if the plane crashes, it means good luck , Otherwise, I don't know what's going on .

4、 An unconditional dead cycle

For example, use while(x), Wait for the level to change , Under normal circumstances x Will become 0, And they were afraid of one thousand , So it's best to add a time limit .

5、 The watchdog is not closed

Some single-chip computers may accidentally open the watchdog with the minimum cycle automatically even when the watchdog is not used , Cause software to reset constantly , Cause a crash . See the chip manual , It is better to clear the watchdog explicitly before closing the watchdog after program reset .

6、 stack overflow

The most difficult problem to find , For the small capacity of the single chip microcomputer , Minimize function call levels , Reduce local variables , This reduces the amount of space needed to stack . When you try all the above, you can't solve the problem , Try to build your less called function directly into the place of call and use it RAM Big local variable to global variable , Give it a try. Maybe it'll do .

Pay more attention to these aspects when programming

原网站

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