For some state sets, use enum instead of #define to define as much as possible.
For example:
enum {OFF = 0, ON = 1}; is much better
than
#define OFF 0
#define ON 1. The two ways of writing have the same compilation effect, but the feeling is different for the programmer and the programmer. When using enum to define, you can think that OFF and ON are a set and are related (and can reduce the definition of #define in the program). But when using #define to define, you can completely think that OFF and ON are irrelevant. Especially when there are too many #define definitions in the program, it is a pain to find and understand its meaning. (For example: uc/os, seeing its large number of #defines is a headache, and it takes a lot of time to fully understand that pile of definitions) Second, use less conditional compilation such as #if #ifdef in the program . Program segments that do not meet the conditions will not be compiled, and syntax errors will not be checked. When the conditional switch is turned on in some cases, a lot of errors may appear. In addition, the program is not conducive to reading. When reading the program, it is really painful to suddenly find out whether something is #define. Okay, here are my habits : 1. Define the output level of the I/O port. In case the I/O level drive is reversed in the future, just change it. //I/O Port Stutes enum {OFF = 0, ON = 1}; //Led Stutes enum {LED_ON = 0, LED_OFF = 1}; eg. LED = LED _ON; //Low level drives the LED to light up. 2. The I/O port must be defined to interface with any other IC. You cannot use the I/O port directly. (Convenient for use in other applications in the future) For example, 93C46: #define CS93C46 P1_0 //Chip select signal #define SK93C46 P1_1 //Clock signal #define DI93C46 P1_2 //Data input, relative to 93C46 (i.e. 93C46 data input) #define DO93C46 P1_3 //Data output For example, character LCD: #define LCDPORT P0 //lcd data port #define RS P1_7 //Data instruction selection. Low--ins; High--data #define RW P1_6 //Read and write selection. Low--w; High--r #define EN P1_5 //Enable. High to low--enable. 3. The keyboard port needs to define the input port and mask word For example: #define KEYMASK 0x0f #define KEYPORT P2 4. The serial port is interrupt-driven and must have Buf. 5. Write two programs for keystrokes and serial port, one is non-blocking and the other is blocking (no return if no key is pressed or no data is received). 6. Define the crystal frequency #define FOSC 11059200ul //Frequency 7. Define the baud rate #define BAUD 4800 //Baud rate 4800 #define SMODX 0 //Whether to multiply 0-NO or 1-YES #define T1H_VAL (256 - (UINT8)(FOSC * (1<
Reference address:Some habits of microcontroller programming
For example:
enum {OFF = 0, ON = 1}; is much better
than
#define OFF 0
#define ON 1. The two ways of writing have the same compilation effect, but the feeling is different for the programmer and the programmer. When using enum to define, you can think that OFF and ON are a set and are related (and can reduce the definition of #define in the program). But when using #define to define, you can completely think that OFF and ON are irrelevant. Especially when there are too many #define definitions in the program, it is a pain to find and understand its meaning. (For example: uc/os, seeing its large number of #defines is a headache, and it takes a lot of time to fully understand that pile of definitions) Second, use less conditional compilation such as #if #ifdef in the program . Program segments that do not meet the conditions will not be compiled, and syntax errors will not be checked. When the conditional switch is turned on in some cases, a lot of errors may appear. In addition, the program is not conducive to reading. When reading the program, it is really painful to suddenly find out whether something is #define. Okay, here are my habits : 1. Define the output level of the I/O port. In case the I/O level drive is reversed in the future, just change it. //I/O Port Stutes enum {OFF = 0, ON = 1}; //Led Stutes enum {LED_ON = 0, LED_OFF = 1}; eg. LED = LED _ON; //Low level drives the LED to light up. 2. The I/O port must be defined to interface with any other IC. You cannot use the I/O port directly. (Convenient for use in other applications in the future) For example, 93C46: #define CS93C46 P1_0 //Chip select signal #define SK93C46 P1_1 //Clock signal #define DI93C46 P1_2 //Data input, relative to 93C46 (i.e. 93C46 data input) #define DO93C46 P1_3 //Data output For example, character LCD: #define LCDPORT P0 //lcd data port #define RS P1_7 //Data instruction selection. Low--ins; High--data #define RW P1_6 //Read and write selection. Low--w; High--r #define EN P1_5 //Enable. High to low--enable. 3. The keyboard port needs to define the input port and mask word For example: #define KEYMASK 0x0f #define KEYPORT P2 4. The serial port is interrupt-driven and must have Buf. 5. Write two programs for keystrokes and serial port, one is non-blocking and the other is blocking (no return if no key is pressed or no data is received). 6. Define the crystal frequency #define FOSC 11059200ul //Frequency 7. Define the baud rate #define BAUD 4800 //Baud rate 4800 #define SMODX 0 //Whether to multiply 0-NO or 1-YES #define T1H_VAL (256 - (UINT8)(FOSC * (1<
Previous article:Convert digital string into digital number in MCU
Next article:Problems with while(1) in C language for microcontrollers
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
Guess you like
- Programmable Power Supply Technology and Application Guide (6) - Reducing Noise from Power Supply to DUT
- 【GD32L233C-START Review】- Summary of the initial review
- Design of heterogeneous pads in AD9
- Zigbee based on CC2430 enables terminal devices to access the network based on a specific PAN ID
- Some posts related to Qinheng EE are sorted out, and there are also school videos in the download center
- How can we check whether the heat dissipation area of the control board is sufficient?
- Correcting ICOM IC-725 frequency deviation without frequency meter
- Freshman asks for a simple wind tunnel control program
- Analysis of the working principles of seven triode collector DC circuits 1
- A very good introduction to the ble Bluetooth protocol stack