This article summarizes some common problems in the application of 51 microcontrollers. These are all things I have actually encountered. Due to the limited length of the article, these problems are far from enough to express the common problems of microcontrollers. I hope it will be helpful to beginners. Please give me some pointers if there are any imperfections in the article. Thanks!
1: How does the C51 compiler distinguish between bit addresses and byte addresses?
It is implemented by predefinition, for example: sfr P0 = 0x80; sbit P0_0 = 0x80; the former declares that the P0 port address is located at 0x80, and the latter explains the bit0 of the P0 port, that is, P0.0 is located at 0x80 in the bit address space. These two 0x80s have completely different meanings and are distinguished by the keywords sfr and sbit. In this way, when the program is compiled, the compiler will compile it into the corresponding assembly language. For example:
C51 statement: P0 = 1;
P0 is declared as sfr, so it is compiled into: mov 80h, 01h, which will send the 0x01 data to the 0x80 unit. Since the 0x80 unit physically corresponds to the P0 port, the P0.0 pin will output a high level (actually in a high impedance state , unique to P0 port), other pins 1-7 output low level.
C51 statement: P0_0 = 1;
P0_0 is declared as sbit, so it is compiled into: setb 80h, which will set the value of the bit at address 0x80 in the bit address space to 1. This bit is exactly bit0 of the P0 port. After execution, P0.0 will output a high-impedance state. And P0.1-.7 will not change.
2: Why does C51 need nested assembly?
A significant advantage of the 51 microcontroller is that the instruction execution time is fixed, so it can adapt to situations with strict timing requirements. For example, the reading and writing of CPU cards that comply with the ISO7816 protocol have strict timing requirements. In fact, it is a synchronous half-duplex serial port made with io pins. Programs that support CPU cards are generally relatively large and need to be organized using C51. However, due to the uncertainty of C compilation, the underlying program must be encapsulated into an assembly language module and embedded into the project. This brings up several issues: how to declare functions, how to pass parameters, etc. Due to space limitations, we cannot go into detail. Here's an example:
The assembler saves a separate file and adds it to the project. The function is as follows:
_proc_a:
mov a, r7
inc a
mov r7, a
ret
Declared in the .h file in c language: extern unsigned char proc_a(unsigned char val);
When called, the shape is: retvalue = proc_a(0x11);
illustrate:
a: If the assembler has parameters, you need to add an underscore before the assembler. There is no need to add it where it is declared (this is required by the Weifu compiler).
b: The first parameter in the formal parameter of the function is passed with R7, and the function return value is returned with R7. This is the general specification of C51. Other parameters have corresponding regulations. The function can return a bit, using the c bit of psw. c: The execution sequence of the above statement is to give 0x11 to R7, then jump to the subroutine, and the subroutine adds 1 to it and sends it back.
d: When the function jumps to the assembler, the R0-R7, A, B, PSW, DPTR and other registers in this area can be used by the subroutine. There is no need to consider whether these conventional resources need to be restored after the call. In the above example, the value of A is used by the function, and the programmer does not need to restore the value before the call.
3: The special features of the P0 port of the 51 microcontroller
Many novices have encountered this problem. It is actually very simple. It involves how the IO pins of the chip are made. This is very important for hardware engineers. TTL’s io lame model:
Ports P1, P2, and P3 can all be understood as shown on the left. Note that there is a resistor under vcc, so it can be understood as: the pin has a weak ability to output 1. There is no resistance on the ground side, which can be understood as the pin has a strong ability to sink current. The P0 port can be understood as the picture on the right. This is the open collector output, also called OC output. It can be seen that when CTR=1, the transistor is turned on and the pin is grounded; when CTR=0, the transistor is turned off and the pin is floating, also called three-state. The purpose of this port is to consider that the P0 port is responsible for reading and writing data and address multiplexing. This relationship requires a careful understanding of the CPU timing diagram. Therefore, the P0 port must add a suitable pull-up resistor and never add a pull-down resistor. The choice of pull-up resistor depends on the external load.
4: How to input and output P1-3 port
This can be seen from the left picture in the previous section. When doing output, ctr=1 will output a strong signal 0, and ctr=0 will output a weak signal 1. When the io pin is used as input, ctr=0 should be set, so that the transistor is turned off. If the external signal is 1, the pull-up resistor strengthens the 1, and the microcontroller will read 1. When the external signal is 0, note that the pull-up effect of the pull-up resistor must be completely offset to get 0 on the pin.
Therefore, for the program, setting the io pin to 1 is in the receiving state, and of course it is also in the output 1 state. The program sets the io port to 1. Whether the read signal is 1 depends on the external circuit. If the external circuit does not "eat" the current of the pull-up resistor, it will read 1. On the contrary, although the program sets the io pin to 1, But what is read is 0.
Therefore, if you use the high level of the io pin to drive an external circuit, be careful that the external circuit "eats" this 1 and cannot output 1. When used as an input, the peripheral with a 0 level must be capable enough to pull the io pin low. Therefore, when using the IO pin to directly light up the LED, it is best to use reverse logic, that is, output 0 to make the LED light up. This ensures driving capability. That is, the io pin is connected to the negative terminal of the LED, and the positive terminal of the LED is connected to the VCC through a resistor.
Therefore, when the io pin outputs 1, it does not matter if the external circuit forcibly connects it to ground. However, when the io pin outputs 0, if the external circuit forcibly connects the power supply, the io pin will be damaged. Therefore, after the program is powered on, all IO ports are generally written as 1: MOV P0, 0FFH.
Port P3 pins are multiplexed and all pins must be in the output 1 state. For example, if the RXD pin outputs 0, it will not be able to read any data. In the early days, the author debugged for a whole day and found out that the reason why the serial port could not receive data was because RXD was not set to 1, and all the time was wasted on the periphery. At that time, Very ashamed.
5: About crystal oscillator
The crystal oscillator of the microcontroller can be simplified to an inverter internally. When the crystal oscillator input pin XI has just passed the threshold voltage and is considered to be 1, the output pin XO outputs 1. This goes round and round.
Therefore, when you use an oscilloscope to observe the normally working crystal oscillator input pin XI, you will get an approximate horizontal line that is neither high nor low. XO is a sine wave with a large amplitude. When measuring the crystal oscillator input pin XI, the oscilloscope test pen must be set at X10, otherwise the test pen will stop the crystal oscillator.
Therefore, when wiring, the crystal oscillator input pin XI should be as close to the crystal oscillator as possible, while the XO pin can be slightly further away. At the same time, XO has certain driving capabilities, and some chips can use it to drive other sequential circuits (this is not recommended because the system reliability is reduced).
After writing this, I realized that there are too many problems with the 51 microcontroller. This article is simply a drop in the ocean.
Previous article:Pinout diagram of MSC-51 microcontroller
Next article:Introduction to 51 microcontroller and introduction to the minimum system module
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Apply for ADI smoke detection module for free, design smoke detectors more reasonably, and save lives more effectively.
- ROHM R Classroom: Power supply design technical information is continuously updated. Click to download
- Is the era of Wi-Fi 6 really coming? Are the original wireless routers going to be eliminated?
- CB140 Hardware Introduction
- ADI's LT8645SIV#PBF chip uses the EN pin to control power on and off, causing it to burn out. Help~
- Problems with STM32F103C8T6 shutdown mode
- New typec interface M2 solid state drive box board 3 yuan a piece 10 pieces free shipping
- Three simple steps to operate the msp430 serial port interrupt
- 【Ufan Learning】How small is the Ufan learning board? ?
- [MicroPython] STM32 branch fixes a CDC problem when USB is reconnected