【51 MCU】Study Summary

Publisher:TechGuru123Latest update time:2015-08-11 Source: dzsc Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Ctrl+S saves the current file, as shown in the figure.
[Reprint] 【51 Single Chip Microcomputer】Study Summary-2014-9-22
Two files are opened, and you can see that one file has an asterisk and a red cross. The red cross indicates that it is the currently opened file, that is, the file being edited. When Ctrl+S is pressed, only the current file is saved. The other file is not saved. When your computer unexpectedly loses power or crashes, if you press Ctrl+S before that, you can recover some losses, but the information of the other file may be lost. Therefore, it is recommended to press F7 (compile and save all files), so that all files can be saved once while compiling. Therefore, when you are really doing a project, you must learn to press F7 regularly, not that you must press F7 after the program is fully written. 2. By default, the
header file and the source file should be placed in the same file directory. If the header file is placed in a separate folder such as a folder named include, it cannot be read at this time. You need to make some settings to read it out. As shown in the figure:
[Reprint] 【51 Single Chip Microcomputer】Study Summary-2014-9-22
In the Include Paths option in the C51 option, you must set the path of the header file before you can open the header file and compile the header file!
3. For variables that do not change, such as the array displayed by the digital tube, it should be placed in ROM with the prefix code to avoid occupying RAM space.
4. The content of the macro definition is all in uppercase letters. When there are two words, use _ to separate them, such as #define LED_SEG P1_1; lowercase letters are used only in special cases.
5. The definition of variable names is generally i, Led, DispBuff, that is, when a single letter is lowercase, when multiple letters are a word, the first letter is uppercase, and when multiple words are written together, the first letter of each word is uppercase.
6. The definition of function names is generally delay, write_byte, that is, when a single word is all lowercase, when multiple words are also lowercase, but the words are separated by _.
7. Don't worry about how many lines should be wrapped and how many spaces should be, as long as you know where to wrap and where to change the space, the scale of things like these can only be understood when you really practice them. Of course, there is no absolute standard. Even very outstanding engineers find it difficult to achieve accurate line breaks. It is OK as long as their programs look neat and readable as a whole. You should know how to put your energy in the right place.
8. Don't be too rigid and follow the rules and regulations, but I believe that I need to learn from Mr. Song, and his program is of great value.
9. Interrupt function writing
[Reprint] 【51 Single Chip Microcomputer】Study Summary-2014-9-22
10. In the function, whether it is the called function or the main function, you should know how to initialize some variables. This is a good habit. Don't think it is unnecessary or cumbersome. This is not cumbersome, but rigorous. You should have a rigorous attitude when writing programs.
11. The standard writing of the main function is:
int main(void)
{
...
return 0;
}
12. When the timer overflows and enters the interrupt, TF0 or TF1 will be cleared to 0 by hardware (automatically) when the interrupt is executed, and the counting will start again. However, when the serial port sends or receives the flag position 1 and causes an interrupt, TI or RI needs to be cleared to 0 by software (manually) during the execution of the interrupt.
13. Regarding the initialization of TH1 and TL1 (equal to TH1) in UART communication timer 1 mode 2. In actual calculation, TH1 = 256 - 11059200/(12*32*9600); TH1 = 0xFD can be calculated; But when you write TH1 = 256 - 11059200/(12*32*9600); into the compiler to let the compiler complete the calculation, then it is wrong to write it like this. In Keil, the default integer constant is uint16 type, with a value of 0~65535. So it should be written as TH1 = 256 – 11059200UL/(12*32*9600UL); It should be noted that Keil does not report this type of error!
14. In serial communication, such as:
[Reprint] 【51 Single Chip Microcomputer】Study Summary-2014-9-22
the serial debugging assistant generally selects "character format display". If you want to display numbers, write the statement UART_send_byte('8'). Note that when displaying numbers, only one digit can be displayed, that is, 0~9; if you want to display letters, you can write the statement UART_send_byte('A'). Be careful not to lose the '', especially when writing numbers. If you lose a letter, an error will be reported. If you lose a number, no error will be reported. At this time, the corresponding ASII code of the number is displayed.
15. If uint8 temp = 'A'; then temp = 'A' + 1, temp = 'B', that is, the addition of characters and numbers is the addition of the corresponding ASII code.
16. In the serial port debugging assistant, you don't need to bring '' when sending characters because the software has already brought it by default.
17. There are two ways to assign values ​​to each element of the array, such as uint8 table [11] = "how are you"; the other is table[0] = 'h'; table[1] = 'o';... The first assignment can only be used when defining data types, as shown above.
After the data type is defined, you cannot change the elements in the array through table [11] = "how are you";. Doing so will cause a warning in Keil! But no error is reported, which is actually incorrect. Then the elements of the array can only be changed by assigning values ​​to the elements one by one.
18. UART communication receives data from the low bit, while I2C communication receives data from the high bit.
19. About bit type functions. For example,
bit i2c_read_buf(uint8 *buf, uint8 addr, uint8 len)
{
while (len--)
{
if (i2c_readbyte(addr++, buf++))
return 1;
}
return 0;
}
In the main function, it can be directly written as bit i2c_read_buf(&a, b, c); or it can be written as y = bit i2c_read_buf(&a, b, c), giving the return value of the function to y.
20. uint8 table[] = “I”; and uint8 table[]={'I'}; are equivalent when used, but the former is a string, so it has one more byte than the latter.
21. The addresses allocated to the structure are continuous. For example:
[Reprint] 【51 Single Chip Microcomputer】Study Summary-2014-9-22
22.Regarding the structure pointer problem. Let's first look at the following programs and draw conclusions through analysis of the results. The program is as follows:
[Reprint] 【51 Single Chip Microcomputer】Study Summary-2014-9-22
(1) The result is: 44.
Does it mean that the structure pointer p points to the entire int a?
(It can point to the next byte address one by one from the first byte address until the fourth one)
(2) Then change tmp={44} to tmp={127};
the result is: 127
is still correct
(3) Continue to change tem={128};
the result is: -128
is obviously not what we want. If p points to the entire int a, then the result should be 128. Obviously, *p only points to the first address of int a. It will not automatically point to the next address and continue to read the remaining 3 bytes. Only when the remaining three bytes are read can a complete INT type data be read. The above is when the pointer is of char type. (4) Continuing from the above, continue to change char *p to int *p.
The result is 128. It
is normal. Why? Because a char pointer can only hold one address at a time (one byte per address),
and int *p can hold four addresses at a time, so at this time p can point to four addresses of four int a at a time
23. The operation order of *p+1 is first *p and then +1, do not mistake it for *(p+1).
24. If there is an array int table[]={111,323,3423,3322}; pointer char *p; and p = &table;
then p points to an address (the first address of table), and how many addresses it can point to is determined by the type of pointer. Now the pointer type is char and char occupies one byte and can only hold one address, so p can only point to one address at a time, so *p != 111, because *p only reads the data of the first address of 111, and the data of three addresses are not read. Note that at this time p+1 does not point to the first address of the element table[1]. Instead, it points to the second address of the first element. You can make some improvements to read 4 addresses at a time, that is, let the pointer point to 4 addresses of int type data at a time.
Change char *p to int *p, so that it can point to four addresses at a time. Then *p = 111, *(p+1) = 232; Note that the brackets must be included, otherwise the result is 112.
It can be concluded that p does not point to one address, and the number of addresses it points to is determined by the data type of p. p + 1 does not represent the next address. For example, int *p, then p+1 represents the next 4 consecutive addresses. It can be understood that p+1 points to 4 consecutive addresses. After the *(p+1) operation, the data of the 4 addresses are read out continuously to synthesize a complete int type of data.
25. About the length of the array. For example, uint8 table[]={'a'}; and uint8 table[]=”a”; The latter is initialized in string form. It should be noted that if there is uint8 *p=&table. For both, *p='a'; but for *(p+1), the two situations are different. The latter has *(p+1)= ''; while the former does not have this situation. In actual experiments, this situation only occurs after pushing a few digits.
26. Regarding the conversion between binary (BCD code) and hexadecimal, 0xFF—0b1111 1111, 0xFE—0b1111 1110.
Write the binary number corresponding to the first digit or letter respectively. As for the conversion from binary to hexadecimal, it is the same, just the other way around. Anyway, remember that 4 binary digits correspond to one hexadecimal digit.
27. Character type data (uint8) exists in the form of ASCII code or decimal number when operating with integers. If the uint8 type variable contains letters or 2 BCD codes (8421), 8-bit binary numbers, when operating with integers, it can be seen that it is an ASCII code or decimal number operation.
28. Don't mistake if statements for loop statements. When executing an if statement, it first determines whether the statements in the brackets are true. If true, it executes the contents in {} (the contents of the nested if statement). If true, it executes the contents in {}. After execution, it does not make another if judgment, but executes the next statement, even if the judgment is still true. The main thing is to explain that if is not a loop statement. It only judges once. Regardless of whether it is true or not, it directly executes the next statement after executing the required content.
29. Regarding the use of the address symbol &, the variable originally represents the address, so & cannot be added, which will result in an error. A special example is the array name a, which itself represents the first address of the first element. But the address symbol can be used without an error. After adding the address symbol, the meaning of &a has changed and now represents the first address of the array. The value seems to be the same as a, but the meaning is different.
30. *p+1 is equivalent to (*p)+1, because * has a higher priority than +; *p++ is equivalent to *(p++), because ++ has a higher priority than *;
&a+1 is equivalent to (&a)+1. &(a+1) cannot appear, the system does not agree, and an error will be reported!
31. For the array int a[4]={1,2,3,4}; a+1 represents the first address of the second element (the address of the first byte). And (int)a+1 represents the address of the second byte of the first element.
32. About the naming of structure variables. As shown in the figure,
[Reprint] 【51 Single Chip Microcomputer】Study Summary-2014-9-22
this naming method will report an error under KEIL, but it is fine under VC++. When you change the code to other letters (do not use the same letters as the original code), you will find that there is no problem. It can be seen that the problem is not the uppercase and lowercase letters of the naming. The reason is that KEIL does not agree to name the structure with lowercase letters (here it means all lowercase). When you change one of the letters to uppercase, there is no problem. For example, change the last line to CODE Code and make the first letter of the variable name capitalized.
33. When the microcontroller is communicating, be sure to turn off all interrupts (EA=0) first, and then turn them on (EA=1) after the communication is completed. Since communication usually requires timing, once an interrupt is triggered, the timing will be disrupted, causing data transmission failure or abnormality! Note that timing is very important in communication!
34.During the communication process, you need to pay attention to the following. For example, when transmitting a byte of data, you need to transmit it one bit at a time until 8 bits are enough to form a byte. Should you start transmitting from the low bit or the high bit? Should you start transmitting from the low bit or the high bit? If these cannot be handled properly, it will directly affect the result. In other words, the sender and the receiver must be consistent. There are two ways. One is to start sending from the low bit (sender) and then start receiving from the low bit (receiver). The other is to start sending from the high bit (sender) and then start receiving from the high bit (receiver). There is also a byte receiving and sending direction that must be consistent. Only when these are guaranteed can the information be accurately transmitted. For example: In UART communication, if you want to use the microcontroller IO port to simulate UART communication, you need to pay attention to these. Generally, the unified standard of UART communication is adopted, and transmission starts from the low bit. In this way, it can be easily connected with the standard UART device without further processing. When both communication devices are connected to the standard UART interface, then there is no need to consider this. They will process it themselves during the transmission process, and we don’t need to consider it. Just get the byte order right. They will complete the processing of the bits themselves.
35. The overflow of timer 1 when working in mode 1 means that when TH1=255 (0xFF) and TL1=0, TL1 reaches 255 and then +1, it overflows. At this time, the overflow flag TF1=1, which needs to be cleared to allow the timer to continue working. Otherwise, it will not time, but wait, waiting for TF1=0, and then start timing. It is necessary to find out when it will overflow!
In addition, each increase of TL1 by 1 means that a machine cycle has been timed. In a 12T/11.0592MHz microcontroller, the machine cycle is 12/11059200 s, which is about 1us. That is to say, each increase of TL1 by 1 means that 1us has been timed. And TH1 plus 1 means that 256 machine cycles have been experienced. The +1 mentioned here will not cause the timer to overflow. Only when TH1 reaches 255, it will overflow after the next 256 machine cycles!
Reference address:【51 MCU】Study Summary

Previous article:How to use STC89C52 timer
Next article:NRF24L01 sending program (the microcontroller is STC89C52)

Latest Microcontroller Articles
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号