Some habits of microcontroller programming

Publisher:VelvetSoulLatest update time:2016-09-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:Convert digital string into digital number in MCU
Next article:Problems with while(1) in C language for microcontrollers

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号