Summary of C51 Microcontroller Programming Points

Summary of C51 Microcontroller Programming Points

1. Header file: #include (I am using STC 89C54RD+)

2. Pre-definition: sbit LED = P1^0 // Define bit 0 of port P1 as LED

Note: The notation “P1^0” is different from A51 (A51 uses P1.0). P1 is a group of ports, and the port number ranges from 0 to 7.

Note 2: sbit is used to define bit variables of SFR (Special Function Register). In the above example, LED is defined as a “global variable”.

Note 3: The following notation is incorrect:

sbit code table[] = {P1^0, P1^1, P1^2, P1^3}; // Trying to use table[i] to specify different pins, but this will cause an error.

sbit table[] = {P1^0, P1^1, P1^2, P1^3}; // Considering that the above may be a misuse of the code keyword, using standard C array notation, but this is also wrong.

3. Main function notation: void main(void)

4. Representation of values:

P1 = 1111 1111 // Binary

P1 = 0xff or P1 = 0xFF // Hexadecimal, starts with 0x, and the value is case insensitive.

P1 = 255 // Decimal

5. When defining decimal values, you can use unsigned char i, which makes i’s range from 0 to 255, making it easier to use as a loop variable.

6. Left and right shifting:

P1 <<= 2 is equivalent to P1 = P1 << 2 // Left shift P1 by 2 bits, left shifting by one bit is equivalent to multiplying by 2.

P1 >>= 3 is equivalent to P1 = P1 >> 3 // Right shift P1 by 3 bits, right shifting by one bit is equivalent to dividing by 2.

Note: Left and right shifts are treated as “logical shifts”, meaning that regardless of shifting left or right, the empty bits are filled with 0.

7. Bitwise AND and OR:

P1 = P1 & 0x01

P1 = P1 | 0x01

8. Define ROM table (which is an array of constants):

unsigned char code table[] = {0xff, 0xff, 0xff, 0xff};

Usage: P1 = table[i]

Note: table[] is defined as a “global variable”, in the above example, i’s range is from 0 to 3.

Note 2: Constants defined by code are stored in the “code area”, that is, the ROM area, which can save RAM space.

9. When programming with a digital tube, if you are using temp[i] to represent a display character, and suddenly want to display a decimal point, you can use temp[i] | 0x80 to add the decimal point through the “or” operation…

10. If you are compiling with Keil C51, remember one thing: it is case insensitive!!! OMG, today while debugging the program, it was just because an array name and a variable name were exactly the same, only differing in case. I thought standard C allowed this… After checking online, OMG, Keil C51 is case insensitive, to be precise, it is “case insensitive during linking”, more accurately, “externally linked variables are case sensitive, while internally linked static ones are case sensitive”… At least Keil uVision2 is like this; I don’t know if other versions are the same, yet to be verified…

11. There is no unsigned float x! There has never been a syntax to add unsigned before float type variables!

12. In Keil compiled programs, the main function does not stop after execution; it will loop to execute the main function again. Why?

Conclusion 1: If the main program does not have a while(1) infinite loop, the program will restart from the beginning after reaching the end.

Conclusion 2: If the main program has a while(1) infinite loop, the program will run in this infinite loop without restarting from the beginning.

This should be considered a bug in the Keil compiler. Some netizens have conducted experiments and reported that the assembly code generated by Keil at the end contains a line LJMP main, meaning it jumps back to the main function to execute repeatedly… Another explanation is that the PC pointer overflows, and the overflowed address points to the beginning, causing the effect of continued execution… (The author thinks this is still a problem with Keil; if the Keil compiler does not generate such a statement as LJMP main, there would not be a loop effect…)

In any case, adding while(1) at the end of the program can solve the bug of looping executing the main function…

Summary of C51 Microcontroller Programming Points

Summary of C51 Microcontroller Programming Points

In order to help everyone learn better, Changxue Electronics has specially added a WeChat public account focusing on microcontrollers and EDA, pushing related knowledge every day, hoping to assist your learning!

Summary of C51 Microcontroller Programming Points

Summary of C51 Microcontroller Programming Points

Leave a Comment