Sharing of MCU system design and C51 programming practice

Publisher:superstar10Latest update time:2013-01-16 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 后记从事单片机开发工作已经有差不多三年时间了,自己感觉积累了一些经验和体会。这篇文章就算是一个总结吧。

Reference address:Sharing of MCU system design and C51 programming practice

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

Remote Multi-point Temperature Monitoring System Based on C8051F020
introduction With the development and application of the Internet, more and more embedded systems are connected to the network. However, most embedded systems are used as application servers in the B/S mode, and must respond to client requests at any time, requiring strong real-time performance. mC/OS-II is an
[Microcontroller]
Remote Multi-point Temperature Monitoring System Based on C8051F020
C8051F software new project process (Silicon IDE)
Note: To create a Silicon IDE project, you must first install the Silicon IDE software. ·Silicon IDE installation:   Please go to the Silabs official website:  http://www.silabs.com/products/mcu/Pages/SoftwareDownloads.aspx?cm_sp=-_--_- to download and install. 1. As shown in the figure below, click "Project - New P
[Microcontroller]
C8051F software new project process (Silicon IDE)
Design of digital circuit for acoustic detection system based on CPLD chip and C8051F020
Passive sound source detection and positioning technology is a technology that uses acoustic microphone arrays and electronic devices to receive radiated noise from moving targets to determine the target's location. This article is based on the principles of acoustic detection technology and mature microelectronics
[Microcontroller]
Design of digital circuit for acoustic detection system based on CPLD chip and C8051F020
Design of catenary fault signal analyzer implemented in FPGA chip implanted with 8051 microprocessor
introduction As chips become larger and larger and resources become more abundant, the design complexity of chips also increases greatly. In fact, after the chip design is completed, some controls sometimes need to be changed according to the situation, which is often encountered during use. It would be very undesirab
[Microcontroller]
Design of catenary fault signal analyzer implemented in FPGA chip implanted with 8051 microprocessor
Using 8051 core to make SoC design less complicated
1 Overview With the development of integrated circuit process technology and the rapid improvement of EDA design level, the ability and technology of SoC (System on Chip) design based on intellectual property IP (Intellectual Property) core has been greatly improved. With this technology, the entire system including
[Power Management]
Using 8051 core to make SoC design less complicated
The principle of sending and receiving of 8051 asynchronous serial port
The basic principle of asynchronous serial communication is point-to-point asynchronous communication, no clock line and no address, low level as the start bit, high level idle, the communicating parties agree on the same data frame size, and the same baud rate, and the transmission and reception are carried out smoot
[Microcontroller]
Introduction to MS8005 parameters and features General 8051 Core MCU 256-byte RAM core
MS8005 is a general-purpose 1T 8051 Core MCU. Under the same system clock, it runs faster and has superior performance than the traditional 8051. The instruction code is fully compatible with the traditional 8051; it retains the main features of the standard 8051, including 256 bytes of RAM and two 16-bit timers; the
[Microcontroller]
Design of TCP/IP on IP Core 8051 Embedded in FPGA
introduction As the chip scale becomes larger and the resources become more abundant, the design complexity of the chip has also greatly increased. In fact, after the chip design is completed, sometimes some controls need to be changed according to the situation, which is often encountered during use. At th
[Microcontroller]
Design of TCP/IP on IP Core 8051 Embedded in FPGA
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号