Summary of c51 microcontroller programming points

1, the header file: #include (I use the STC 89C54RD+)

2. Predefined: sbit LED = P1^0// Define the 0 bit of P1 port as LED

Note: "P1^0" is written differently from A51 (A51 is P1.0), P1 is a group of ports, port number range 0~7

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

Note 3: The following is wrong:

Sbit code table[ ] = {P1^0, P1^1, P1^2, P1^3};// I want to specify different pins with table[i], but this will give an error.

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

3, the main function is written: void main (void)

4, the representation of the value:

P1 = 1111 1111// Binary

P1 = 0xff or P1 = 0xFF // hexadecimal, starting with 0x, and the value is not case sensitive

P1 = 255// decimal

5, when defining a decimal value, you can use unsigned char i, so the range of i is 0~255, which is better as a loop variable.

6, left and right shift:

P1 <<= 2 is equivalent to P1 = P1 << 2// P1 is shifted 2 bits to the left and 1 is shifted to the left by 2

P1 >>= 3 is equivalent to P1 = P1 >> 3// P1 shifts 3 bits to the right and shifts one bit to the right by dividing by 2

Note: The left and right shifts default to "logical shift", that is, whether the left or right shift, the gap is filled with 0

7, by bit and or:

P1 = P1 & 0x01

P1 = P1 | 0x01

8, define the ROM table (that is, the data is an array of constants):

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

Use: P1 = table[i]

Note: table[ ] is defined as "global variable". In the above example, the range of i is 0~3.

Note 2: The constant defined by code is stored in the “code area”, ie the ROM area, which can save RAM space.

9. When using digital tube programming, if you are using temp[i] to represent a certain display character and suddenly want to display the decimal point, you can use temp[i] | 0x80, and add the decimal point by the OR operation...

10. If you compile with Keil C51, remember one thing: it is not case sensitive!!! The slot, today the program is wrong, because an array name is exactly the same as a variable name, but the case is not the same. , Standard C, how do I remember this way... Internet search, lying, Keil C51 is not case sensitive, exactly "connected when not case sensitive", more accurately "variables with external connections" Case, internal connection static is case sensitive"... At least Keil uVision2 is like this, don't know if other versions are not, to be verified...

11, no unsigned float x !float type variable has never been added before the unsigned syntax!

12, Keil compiled program, the main function will not stop after execution, will cycle the main function, what solution?

Conclusion 1: If there is no infinite loop of while(1) in the main program, the program will start from the beginning again.

Conclusion 2: If the main program has an infinite loop of while(1), the program will continue to run in this infinite loop until the end, and there will be no further execution from the beginning.

This should be a bug in the Keil compiler. Some netizens have done experiments, indicating that the assembly code generated in the later stage of Keil compilation has an LJMP main at the end, which means jumping to the main function for repeated execution... There is also a saying that the PC pointer overflows. The address after the overflow points to the beginning, causing the effect to continue... (The blogger thinks it is still Keil's problem. If the Keil compiler does not generate the LJMP main statement, it will not produce a loop effect...)

Anyway, adding while(1) at the end of the program can solve the bug of looping the main function...

LED Display

Led Display,Led Screen,Led Display Screen,led monitor

Guangzhou Chengwen Photoelectric Technology co.,ltd , https://www.cwleddisplay.com