1.1 Understanding the capabilities of microcontrollers
【Rule 1】Design the most streamlined system that meets the requirements. It is crucial for a microcontroller system designer to correctly estimate the capabilities of the microcontroller, know what the microcontroller can do, and maximize the potential of the microcontroller. We should have such an understanding that the processing power of the microcontroller is very powerful. The early PCs, whose CPU (8086) processing power was equivalent to that of 8051, could handle quite complex tasks. The key to the capabilities of the microcontroller lies in the software written by the software designer. Only by fully understanding the capabilities of the microcontroller can we avoid making a "redundant" system design. And using many peripheral chips to implement the functions that the microcontroller can achieve. Doing so will increase the system cost and may also reduce the reliability of the system. 1.2 System reliability is crucial
【Rule 2】Use a watchdog. The watchdog circuit is usually a piece of hardware that is updated at regular intervals. The update is usually done by the microcontroller. If the watchdog is not updated within a certain interval, the watchdog will generate a reset signal to reset the microcontroller. The specific form of updating the watchdog is to provide a level rising edge to the relevant pins of the watchdog chip or read and write a register of it. The watchdog circuit will reset the microcontroller when the microcontroller fails and freezes. There are currently many watchdog chips, such as MAX802 and MAX813 from MAXIM. Moreover, many microcontrollers have watchdogs integrated in them. An external watchdog is the best because it does not depend on the microcontroller. If possible, the watchdog update program should not be placed in an interrupt or subroutine, but in principle should be placed in the main program. I once saw an engineer who occasionally caused the watchdog to reset when the program he was debugging was running, so he simply cleared the watchdog in the clock interrupt program that interrupted every 10ms. I believe he also knew how to disable the watchdog, but he did not find out the real reason for this phenomenon. Therefore, I would like to remind everyone: no matter what the reason, never ignore the real cause of system failure. High-quality products come from high-quality engineers, and high-quality products create high-quality engineers.
【Rule 3】Make sure the reset signal of the system is reliable. This is a problem that is easily overlooked. When you are designing a single-chip microcomputer system, do you have this concept in mind? What kind of reset signal is reliable? Have you used an oscilloscope to check the reset signal of the product you designed? What consequences may an unstable reset signal have? Have you ever found that the data of the single-chip microcomputer system you designed becomes a mess every time it is powered on and restarted, and the phenomenon is different every time, and there is no pattern to find, or sometimes it simply does not run, or sometimes it enters a dead state, and sometimes it does not run normally at all? In this case, you should check the reset signal of your system. Generally, the requirements for the reset signal required by the single-chip microcomputer are mentioned in the data sheet of the single-chip microcomputer. Generally, the width of the reset signal should be. The width and amplitude of the reset level should meet the requirements of the chip and must remain stable. Another particularly important point is that the reset level should occur at the same time as the power-on, that is, the reset signal is generated as soon as the chip is powered on. Otherwise, since there is no reset, the register values in the microcontroller are random values. When powered on, the program will start running according to the random content in the PC register, which may easily lead to misoperation or a freeze state.
【Rule 4】 Make sure the system initialization is effective. The system program should be delayed for a period of time. This is a common method in many single-chip computer program designs. Why? Because it often takes a while for the chips and devices in the system to reach normal working state from power-on. A delay of a period of time at the start of the program allows all devices in the system to reach normal working state. How much delay is appropriate? This depends on the time it takes for each chip in the system to reach normal working state, usually the slowest one. Generally speaking, a delay of 20-100 milliseconds is sufficient. For "slow-heating" devices such as embedded MODEM in the system, it should be longer. Of course, this needs to be adjusted during the actual operation of the system.
【Rule 5】Test the system when powered on. Testing the system when powered on is a good design in the MCU program. When designing the hardware, you should also carefully consider designing each chip and interface used into a mode that is easy to test using software. Many experienced MCU designers will perform a comprehensive test when the system is powered on (especially when powered on for the first time), or go a step further and divide the system's operating state into a test mode and a normal operating mode. By adding the test mode to perform a detailed test on the system, the batch test of the system is more convenient and easy. In addition, it should be noted that a simple and clear fault display interface is also quite thoughtful. For example: the system's external RAM (data memory) is a commonly used device in the MCU system. If there is a problem with the external RAM, the program will usually become a wild horse. Therefore, the program must test the external RAM when it starts (at least when it is powered on for the first time). The test content includes: 1) Detecting the units in the RAM. This is mainly achieved by keeping the written and read data consistent. 2) Detecting the address data bus between the MCU and the RAM. The buses are neither short-circuited nor connected to the ground. In addition, many chips provide testing methods. For example, serial communication chips such as UART have loop testing functions.
【Rule 6】Design hardware according to EMC test requirements. EMC test requirements have become a necessity for products. There are many articles on this. 1.3 Software Programming and Debugging
【Rule 7】Use Small mode to compile as much as possible. Compared with Large mode and Compact mode, Small mode can generate more compact code. In Small mode, C51 compiler will put all variables that do not use keywords such as idata, pdata, and xdata in the data unit. In programming, for the data area, you can specify to put it in the external memory.
【Rule 8】Be fully prepared before simulation. The MCU hardware simulator has brought great convenience to MCU developers, but it can also easily cause people to rely on it. Many times, the absence of a simulator can prompt engineers to write higher quality programs. Perhaps before hardware simulation debugging, the following preparations will be useful to you: 1) After the program is compiled, check the code carefully line by line. Check the code for errors, establish your own code checklist, and check the places where errors are often made. Check whether the code meets the programming specifications. 2) Test each subroutine. Testing method: Use the program to test the program, compile a code to call the subroutine, establish the entry conditions of the subroutine to be tested, and then see if it outputs the results as expected. 3) If the code has been modified, check the code again. 4) If possible, perform software simulation-Keil C's software simulation function is very powerful. Software simulation can prevent debugging errors caused by hardware errors, such as device damage, line break or short circuit. 5) Start hardware simulation. [page]
【Rule 9】Use library functions to reuse code, especially the code from the standard library, instead of writing your own code manually. This is faster, easier and safer. KeilC provides multiple library functions, and the usage of these library functions is described in detail in the KeilC help file.
【Rule 10】Use const. This is a must-talk point in many classic books on C and C++. In the book "Exceptional C++", there is a wonderful description of this point, which is excerpted as follows: "A gunman without a correct sense of security will not live long in the world. The same is true for programmers who do not have time to wear their hats tightly and electricians who do not have time to check live wires." In C language, the const modifier tells the compiler that this function will not change any value pointed to by the modified variable (except for forced type conversion). When passing a pointer as a parameter, always use const appropriately, which can not only prevent you from accidentally assigning the wrong value, but also prevent you from modifying the value of the object pointed to by the pointer when passing the pointer as a parameter to the function. For example: const int num = 7;num = 9; file:// may get a compiler warning. const char *ptr means that the content pointed to by the pointer will not be changed. If an operation of assigning a value to it occurs in the program, an error message will be displayed during compilation. For example: const char *ptr = "hello"; *ptr = 'H'; file://Error, the content pointed to cannot be changed. You can also put const after the asterisk to declare that the pointer itself cannot be changed. For example: char* const ptr;ptr++; file://Error, the pointer itself cannot be changed. You can also prohibit changing the pointer and the content it refers to at the same time. The form is as follows: const char* const ptr;
【Rule 11】Use staticstatic is a useful tool to reduce naming conflicts. Use static to modify variables and functions in only one module file, so that there will be no name conflicts with functions and variables with the same name in other modules when the modules are connected. Generally speaking, as long as it is not a function provided to other modules and a non-global variable, it should be modified with static. When the variables in a subroutine are modified with static, it means that the memory of this variable is allocated at the beginning of the program and released at the end of the program. They keep their values during the execution of the program. For example: void func1(void){static int time = 0;time++}void func2(void){static int time = 0;time++;}The time variables in the two subroutines are modified with static, so they are static variables. Each time time is called, it will increase by 1 and keep this value. Their functions are similar to the following programs: int time1 = 0; int time2 = 0; void func1(void){time1++} void func2(void){time2++;} We can see that after using the static modifier, the number of global variables in the module is reduced, making the program simpler.
【Rule 12】Do not ignore compiler warnings. The compiler's warnings are targeted, so do not ignore them before finding out the real cause of the warning.
【Rule 13】Pay attention to overflow issues and write safe code. 1.4 KeilC Programming
【Rule 14】In-depth understanding of the tools you use. Carefully read the help files that come with KeilC, and you will find what you have been waiting for. KeilC is currently the best microcontroller development software. To fully utilize the functions of this software, you must have a deep understanding of it.
【规则15】不要使用语言的冷僻特性,并且记住,耍小聪明会贻害无穷。最重要的是编写你理解的代码,理解你编写的代码,你就可能会做得很好。2 推荐书目要成为一个优秀的单片机系统产品设计工程师,兴趣、热情、责任心至关重要。2.1 单片机技术学习《微机原理及应用(从16 位到32 位) 》戴梅萼等著清华大学出版社。学校教材,也是当年我学习单片机的启蒙书。2.2 C51 编程学习《单片机高级语言C51 Windows 环境编程与应用》作者:徐爱钧彭秀华电子工业出版社。这本书几乎覆盖了C51 编程的方方面面,最新版本对当前使用最广的keilC 也有很详细的讲述。对于刚学C51 编程的同志,本书是上上之选,强力推荐。比起现今书市上的所谓什么“C51 编程圣经”之类的书强得多。
2.3 C 语言编程必读《C 陷阱与缺陷》Andrew Koenig著《C 专家编程》Peter Van Der Linden 著C 语言开发技术经典之作,C 程序员必读之书,数十年来经久不衰。如果你想对C 语言全面的掌握,真正了解C 语言的精髓,这两本书是必读之作。由人民邮电出版社出版的中文译本也还不错。2.4 程序设计技术方面《数据结构》, 严蔚敏, 清华大学出版社。清华大学出版社的教材质量稳定,中规中矩,价格相对来说也便宜一点。《程序设计实践》Brian W. Kernighan, Rob Pike著;《代码大全》(网上有下载)。这两本是能让你看后,感觉有大突破的那种书籍,千万别吝惜银子。3 后记从事单片机开发工作已经有差不多三年时间了,自己感觉积累了一些经验和体会。这篇文章就算是一个总结吧。
Previous article:51 MCU serial port problem
Next article:Embedded Internet Access Technology Based on MCS-51
Recommended ReadingLatest update time:2024-11-16 21:53
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- STC32G Series MCU Technical Reference Manual
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Equivalent Circuit
- Over View can customize the format and content of the audio body according to your needs
- What should I do if my WIFI is stolen?
- Architecture Issues
- [RVB2601 Creative Application Development] Network Weather Clock
- The process of responding to interrupts in DSP
- [RVB2601 Creative Application Development] 1. Development Environment Construction
- EEWORLD University ---- Live Replay: Microchip Security Series 6 - Trust Your Firmware: Secure Boot Application Processors
- [Home Smart Lighting Control and Indoor Environment Monitoring System]--8. Merge 2 projects in ON-SEMI development software
- 51 MCU sends data to the serial port