Summary of Key Points in C51 Microcontroller Programming
1. Header File: #include (I'm using the STC 89C54RD+)
2. Definition of Special Function Registers: sbit LED = P1^0; // This defines bit 0 of port P1 as the LED.
Note: In C51, "P1^0" is used instead of "P1.0" as in A51. Port P1 consists of 8 bits, numbered from 0 to 7.
Note 2: The keyword 'sbit' is used to define a bit variable from the SFR (Special Function Register). In this example, LED is treated as a global variable.
Note 3: The following syntax is incorrect:
sbit code table[] = {P1^0, P1^1, P1^2, P1^3}; // Trying to access different pins via table[i] leads to an error.
sbit table[] = {P1^0, P1^1, P1^2, P1^3}; // Even if you try using standard C array syntax, it's still invalid.
3. Main Function: void main(void) { ... }
4. Representation of Values:
P1 = 11111111; // Binary value
P1 = 0xff or P1 = 0xFF; // Hexadecimal value, starts with 0x, case-insensitive
P1 = 255; // Decimal value
5. For decimal values, use 'unsigned char i' which ranges from 0 to 255, making it ideal for loop variables.
6. Bitwise Shift Operations:
P1 <<= 2; // Equivalent to P1 = P1 << 2; shifts left by 2 bits.
P1 >>= 3; // Equivalent to P1 = P1 >> 3; shifts right by 3 bits, effectively dividing by 8.
Note: By default, shifts are logical, meaning empty spaces are filled with zeros.
7. Bitwise AND and OR:
P1 = P1 & 0x01; // Sets all bits except the first to zero.
P1 = P1 | 0x01; // Sets the first bit to one, keeping others unchanged.
8. ROM Table (Constant Array):
unsigned char code table[] = {0xff, 0xff, 0xff, 0xff};
Usage: P1 = table[i];
Note: 'table[]' is defined as a global variable. In this example, 'i' ranges from 0 to 3.
Note 2: Constants declared with 'code' are stored in the ROM area, saving RAM space.
9. When working with digital tubes, if you're using temp[i] to represent a character and want to display a decimal point, you can use temp[i] | 0x80 to add the dot via a bitwise OR operation.
10. Keil C51 Compilation Note: It is not case-sensitive! Today’s program may fail due to a typo where the array name and variable name are the same but differ in case. In standard C, this would be an error, but Keil is not case-sensitive. Some versions may treat external variables differently, while internal static variables are case-sensitive. At least in Keil uVision2, this behavior holds true. It's worth verifying on other versions.
11. There is no 'unsigned float x' in C51. The 'unsigned' keyword cannot be used with the 'float' data type.
12. After compiling with Keil C51, the main function does not stop after execution—it loops back. How to solve this?
Conclusion 1: If there is no 'while(1)' loop in the main function, the program will restart from the beginning.
Conclusion 2: If the main function includes an infinite 'while(1)' loop, the program will run continuously within that loop and won't restart from the top.
This seems to be a known issue with Keil. Some users have observed that the generated assembly code ends with an 'LJMP main', causing the program to jump back to the start. Another theory suggests that the PC pointer overflows, pointing to the beginning of the program, resulting in a loop. Whether it's a compiler bug or a design choice, adding 'while(1)' at the end of your main function can prevent the unintended loop effect.
Refurbished Lenovo,Used Lenovo Thinkpad,Used Lenovo,Lenovo Refurbished Laptops
Guangzhou Panda Electronic Technology Co., LTD , https://www.panda-3c.com