Ten 51 MCU programming tips

Publisher:快乐的舞蹈Latest update time:2017-11-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    1. Do not define too many variables. The lower 128 bits are the storage area for user-defined variables (by default). You can also put variables in the upper 128 bits, but it is easy to make mistakes. Try to put as few as possible, or better not put them at all. You can view the storage of memory variables through M51. It is best not to exceed 110 bytes, otherwise the program cannot run or errors will occur.

    2. If a variable is used in multiple places, it can be defined as a global variable. For example, loop variables i and j, try to reduce parameter passing.

    3. Static variables are best placed in the program storage area and defined with code.

    4. Variables should be defined as unsigned numbers. Variables that only occupy one bit, such as flags, should be defined as bits.

    5. Subroutines should be declared in advance after the main() main program, and those placed in front do not need to be declared.

    6. If variables and subroutines are defined but not used, the system will give a warning.

    7. Although local variables and global variables have different names, they may be changed by global variables during runtime.

    8. After the program is compiled, it is possible to generate very large files. For example, the original 5k becomes 10k after compilation. This is caused by the arbitrary allocation of program space during C assembly. You can first block part of the program and compile it, then remove the block and compile it again, you can restore it, or copy it part by part to another file and compile it.

    9. If a timer is used to generate a pulse signal, when you want to remove the pulse signal, you should first turn off the timer, and then set the level to high or low as required.

    10. The "watchdog" function can be implemented using software. For example: a program takes 50ms to run a cycle, then the timer can be set to 100ms. When it exceeds 100ms, the program will reset, and the timer will be reloaded or the timer parameters will be reset at the bottom of the program cycle.

Reference address:Ten 51 MCU programming tips

Previous article:How to easily transform 51 MCU to ARM design?
Next article:Comparison of AVR, STC and 51 MCU

Recommended ReadingLatest update time:2024-11-23 02:39

Proteus and Keil Cx51 microcontroller simulation (measure pulse width)
The width of the pulse appearing on the INT0 pin is tested using the gate bit and displayed by the light-emitting diode in the form of a number of machine cycles. Circuit Diagram: U1 source program: #include reg51.h sbit P1_1=P1^1; void main(void) { TMOD=0x02; EA=1; ET0=1; TH0=256-250; TL0=256-250;
[Microcontroller]
Multi-mode 51 single-chip heart-shaped water light + breathing light + buzzer music
1. Basic hardware DIY design Circuit Hardware: STC89C52RC Buzzer/24 LEDs/4 corner buttons 1) Overall schematic diagram 2) PCB circuit 3) 3D_PCB TOP layer 2. MCU Programming 1) Breathing light //Breathing light while(1) { for(high=1;high cycle;high++) { P2=P1=P0=0XFF; delay2(high); P2=P1=P0=0X00;
[Microcontroller]
Multi-mode 51 single-chip heart-shaped water light + breathing light + buzzer music
Hardware expansion method of 51 single chip microcomputer timer range
    The 51 single-chip microcomputer working mode 2 automatically loads the timing value. According to the description in the textbook, the timing capability seems to be relatively accurate, but at this time it is an 8-bit timer: when the crystal oscillator is 12MHz, it can only time 0.256ms.     Upgrade, don't care a
[Microcontroller]
Hardware expansion method of 51 single chip microcomputer timer range
Design of time/displacement commutation controller based on microcontroller AT89C51
introduction In the actual production process, control components with automatic reversing functions are often used, such as round-trip operation (displacement) in machining, forward and reverse output of DC power supply, forward and reverse operation of motors, etc. When the forward (or reverse) operation reaches a
[Microcontroller]
Design of serial communication based on AT89C51 single chip microcomputer application system
1 Introduction In the power equipment status detection system of infrared imaging technology, the application system based on AT89C51 single chip microcomputer adopts the non-contact infrared thermometer model 3iLRL3 of American Ray State Company. The thermometer adopts the RS232C serial communication standar
[Microcontroller]
Design of serial communication based on AT89C51 single chip microcomputer application system
Internal structure of MCS-51 single-chip microcomputer - CPU structure and single-chip microcomputer principle study notes (II)
CPU Structure  The internal CPU of 8051 is a central processing unit with a word length of 8 bits in binary, which means that it processes data in bytes. Similar to the CPU of a microcomputer, the internal CPU of 8051 is also composed of three circuits: arithmetic logic unit (ALU), controller (timing control unit, e
[Microcontroller]
Internal structure of MCS-51 single-chip microcomputer - CPU structure and single-chip microcomputer principle study notes (II)
Beginner's guide to 51 MCU - simple water lamp program
#include reg52.h #include intrins.h //Call library function #define uint unsigned int #define uchar unsigned char void delay(uint); //delay function uchar a,b; void main() { a=0xfe; while(1) { P1=a; delay(500); a=_crol_(a,1); //Use library function to shift the running light to the left   } } void delay(uint z
[Microcontroller]
USB expansion based on C8051F020 single chip microcomputer system
1 Introduction USB (Universal Serial Bus) is one of the most successful interfaces in the history of PC. It has not only become a standard interface for PC, but also developed into a standard interface for consumer electronics and mobile devices. It has the advantages of easy installation, high speed, flexibili
[Microcontroller]
USB expansion based on C8051F020 single chip microcomputer system
Latest Microcontroller Articles
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号