1. What can you do with a single-chip microcomputer? Single
-chip microcomputers are widely used. You can see single-chip microcomputers everywhere, such as remote-controlled cars, washing machines, refrigerators, color TVs, and air conditioners. After learning single-chip microcomputers, you can play small productions that you like, or you can use single-chip microcomputers to design some small products. The most important thing is that single-chip microcomputers will bring you into the field of electronic design, fully demonstrate your abilities, and of course, bring you good returns.
2. Which single-chip microcomputer
should I learn? There are many types of single-chip microcomputers, the most common ones are 51, PIC, and AVR series, and each series has many models. So, as beginners, which single-chip microcomputer should we learn?
In fact, single-chip microcomputers have similarities. As long as you study hard and truly master a single-chip microcomputer, it is easy to learn other models, and the latter is directly used. As a classic single-chip microcomputer, the 51 series has very rich information, so it should be a good choice to start with 51.
3. Should I learn assembly or C51?
This is probably the most controversial issue. Some people insist on using assembly language to learn MCU software, while others insist on choosing high-level languages. This is mainly because when C51 was first introduced, the internal resources of the MCU were relatively precious, and the compiler efficiency of C51 itself was not high enough, which led many people to choose to use assembly language to write code. So what is the situation now? The compiler efficiency is high, and the internal resources of the MCU are also rich. Using C to write MCU software will not encounter the same problems as before.
If there are still people who insist on using assembly language to write the entire MCU software, then this person must not know C. Just look at the companies outside and you will know that there are several companies that do not use C. After all, C is easy to write, readable, and portable.
But knowing C alone is not enough. You also have to understand assembly language. In fact, it is not to understand assembly instructions, but to understand the architecture of the MCU. So I think it is recommended to use C to write code when learning MCU, but you should also look at other people's assembly code more often. The minimum requirement is: to be able to use C to write code proficiently and to understand assembly code.
4: How does the C51 compiler distinguish between bit addresses and byte addresses? It
is implemented by pre-definition, for example: sfrP0=0x80;sbitP0_0=0x80; the former declares that the address of the P0 port is at 0x80, and the latter indicates that 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, which 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 accordingly. For example:
C51 statement: P0=1;
P0 is declared as sfr, so it is compiled into: mov80h, 01h, which will send 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 it is in a high impedance state, which is unique to the P0 port), and the other .1-.7 pins will output a low level.
C51 statement: P0_0=1;
P0_0 is declared as sbit, so it is compiled into: setb80h, which will set the value of the bit at address 0x80 in the bit address space to 1. This bit is exactly bit0 of port P0. After execution, P0.0 will output high impedance. P0.1-.7 will not change.
5: 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 occasions with strict timing requirements. For example, the reading and writing of the CPU card that complies with the ISO7816 protocol has strict timing requirements. In fact, it is a synchronous half-duplex serial port made with io pins. Programs that support CPU cards are generally 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 in the project. This brings several problems: how to declare functions, how to pass parameters, etc. Due to space limitations, it cannot be explained in detail. The following example:
the assembly program is saved in a separate file and added to the project. The function is as follows:
_proc_a:
mova, r7
inca
movr7, a
ret.
It is declared in the .h file in C language: externunsignedcharproc_a (unsignedcharval);
when called, it is as follows: retvalue = proc_a (0x11);
Description:
a: If the assembly program has parameters, you need to add an extra underscore before the assembly program. It is not necessary to add it where it is declared (this is required by the Weifu compiler).
b: The first parameter of the function's formal parameters is passed with R7, and the function return value is returned with R7. This is a general specification of C51. Other parameters have corresponding regulations. The function can return a bit, which is returned using the c bit of psw.
c: The execution order of the above statements is to give 0x11 to R7, then jump to the subroutine, and the subroutine will add 1 to it and send it back.
d: When the function jumps to the assembly program, the registers R0-R7, A, B, PSW, DPTR and other registers in this area can be used by the subroutine, and there is no need to consider whether to restore these conventional resources 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.
6: The special features of the P0 port of the 51 single-chip microcomputer
Many novices encounter this problem. In fact, it is very simple. This involves how the io pins of the chip are made. This is very important for hardware engineers. TTL io pin model:
P1, P2, and P3 ports can be understood as the left picture. 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 resistor on the ground side, which can be understood as the pin has a strong ability to absorb current. And P0 port can be understood as the right picture. 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 tri-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 careful understanding of the CPU timing diagram. Therefore, the P0 port should add a suitable pull-up resistor, and never add a pull-down resistor. The selection of the pull-up resistor depends on the external load.
7: How to input and output the P1-3 port
As can be seen from the left figure in the previous section. When outputting, ctr=1 outputs a strong signal 0, and ctr=0 outputs a weak signal 1. When the io pin is input, ctr=0 should be set so that the transistor is cut off. If the external signal is 1, the pull-up resistor strengthens this 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 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 up" the current of the pull-up resistor, the reading will be 1. On the contrary, although the program sets the io pin to 1, the reading will be 0.
Therefore, if you use the high level of the io pin to drive the external circuit, be careful that the external circuit "eats up" this 1 and thus cannot output 1. When used as an input, the peripheral with a 0 level must be capable enough to pull the io pin down. Therefore, when using the io pin to directly light up the LED, it is best to use inverse logic, that is, output 0 to make the LED light up. This can ensure the driving ability. That is, the io pin is connected to the negative end of the LED, and the positive end of the LED is connected to vcc through a resistor.
Therefore, when the io pin outputs 1, it does not matter if the external circuit forcibly connects it to the ground. However, when the io pin outputs 0, the external circuit forcibly connects the power supply and the io pin will be damaged. Therefore, after the program is powered on, all io ports are generally written as 1: MOVP0, 0FFH. The
P3 port 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. The author once debugged for a whole day before finding that the serial port could not receive data because RXD was not set to 1. Time was wasted on the periphery, which was very embarrassing at the time.
8: About crystal oscillator
The crystal oscillator of the microcontroller can be simplified into an inverter internally. When the crystal oscillator input pin XI just passes the threshold voltage and is considered to be 1, the output pin XO outputs 0. This 0 will drive the crystal oscillator to make the XI voltage drop. When it drops to the threshold voltage and is considered to be 0, the output pin XO outputs 1. This cycle repeats.
Therefore, when using an oscilloscope to observe the normal working crystal oscillator input pin XI, a horizontal line is obtained that is neither high nor low. XO is a sine wave with a large amplitude. When measuring the crystal input pin XI, the oscilloscope probe should be set to the X10 position, otherwise, the probe can stop the crystal.
Therefore, when wiring, the crystal input pin XI should be as close to the crystal as possible, while the XO pin can be slightly farther away. At the same time, XO has a certain driving capability, and some chips can use it to drive other timing circuits (this is not recommended because the system reliability is reduced).
Reference address:Common problems in microcontroller learning
-chip microcomputers are widely used. You can see single-chip microcomputers everywhere, such as remote-controlled cars, washing machines, refrigerators, color TVs, and air conditioners. After learning single-chip microcomputers, you can play small productions that you like, or you can use single-chip microcomputers to design some small products. The most important thing is that single-chip microcomputers will bring you into the field of electronic design, fully demonstrate your abilities, and of course, bring you good returns.
2. Which single-chip microcomputer
should I learn? There are many types of single-chip microcomputers, the most common ones are 51, PIC, and AVR series, and each series has many models. So, as beginners, which single-chip microcomputer should we learn?
In fact, single-chip microcomputers have similarities. As long as you study hard and truly master a single-chip microcomputer, it is easy to learn other models, and the latter is directly used. As a classic single-chip microcomputer, the 51 series has very rich information, so it should be a good choice to start with 51.
3. Should I learn assembly or C51?
This is probably the most controversial issue. Some people insist on using assembly language to learn MCU software, while others insist on choosing high-level languages. This is mainly because when C51 was first introduced, the internal resources of the MCU were relatively precious, and the compiler efficiency of C51 itself was not high enough, which led many people to choose to use assembly language to write code. So what is the situation now? The compiler efficiency is high, and the internal resources of the MCU are also rich. Using C to write MCU software will not encounter the same problems as before.
If there are still people who insist on using assembly language to write the entire MCU software, then this person must not know C. Just look at the companies outside and you will know that there are several companies that do not use C. After all, C is easy to write, readable, and portable.
But knowing C alone is not enough. You also have to understand assembly language. In fact, it is not to understand assembly instructions, but to understand the architecture of the MCU. So I think it is recommended to use C to write code when learning MCU, but you should also look at other people's assembly code more often. The minimum requirement is: to be able to use C to write code proficiently and to understand assembly code.
4: How does the C51 compiler distinguish between bit addresses and byte addresses? It
is implemented by pre-definition, for example: sfrP0=0x80;sbitP0_0=0x80; the former declares that the address of the P0 port is at 0x80, and the latter indicates that 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, which 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 accordingly. For example:
C51 statement: P0=1;
P0 is declared as sfr, so it is compiled into: mov80h, 01h, which will send 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 it is in a high impedance state, which is unique to the P0 port), and the other .1-.7 pins will output a low level.
C51 statement: P0_0=1;
P0_0 is declared as sbit, so it is compiled into: setb80h, which will set the value of the bit at address 0x80 in the bit address space to 1. This bit is exactly bit0 of port P0. After execution, P0.0 will output high impedance. P0.1-.7 will not change.
5: 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 occasions with strict timing requirements. For example, the reading and writing of the CPU card that complies with the ISO7816 protocol has strict timing requirements. In fact, it is a synchronous half-duplex serial port made with io pins. Programs that support CPU cards are generally 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 in the project. This brings several problems: how to declare functions, how to pass parameters, etc. Due to space limitations, it cannot be explained in detail. The following example:
the assembly program is saved in a separate file and added to the project. The function is as follows:
_proc_a:
mova, r7
inca
movr7, a
ret.
It is declared in the .h file in C language: externunsignedcharproc_a (unsignedcharval);
when called, it is as follows: retvalue = proc_a (0x11);
Description:
a: If the assembly program has parameters, you need to add an extra underscore before the assembly program. It is not necessary to add it where it is declared (this is required by the Weifu compiler).
b: The first parameter of the function's formal parameters is passed with R7, and the function return value is returned with R7. This is a general specification of C51. Other parameters have corresponding regulations. The function can return a bit, which is returned using the c bit of psw.
c: The execution order of the above statements is to give 0x11 to R7, then jump to the subroutine, and the subroutine will add 1 to it and send it back.
d: When the function jumps to the assembly program, the registers R0-R7, A, B, PSW, DPTR and other registers in this area can be used by the subroutine, and there is no need to consider whether to restore these conventional resources 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.
6: The special features of the P0 port of the 51 single-chip microcomputer
Many novices encounter this problem. In fact, it is very simple. This involves how the io pins of the chip are made. This is very important for hardware engineers. TTL io pin model:
P1, P2, and P3 ports can be understood as the left picture. 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 resistor on the ground side, which can be understood as the pin has a strong ability to absorb current. And P0 port can be understood as the right picture. 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 tri-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 careful understanding of the CPU timing diagram. Therefore, the P0 port should add a suitable pull-up resistor, and never add a pull-down resistor. The selection of the pull-up resistor depends on the external load.
7: How to input and output the P1-3 port
As can be seen from the left figure in the previous section. When outputting, ctr=1 outputs a strong signal 0, and ctr=0 outputs a weak signal 1. When the io pin is input, ctr=0 should be set so that the transistor is cut off. If the external signal is 1, the pull-up resistor strengthens this 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 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 up" the current of the pull-up resistor, the reading will be 1. On the contrary, although the program sets the io pin to 1, the reading will be 0.
Therefore, if you use the high level of the io pin to drive the external circuit, be careful that the external circuit "eats up" this 1 and thus cannot output 1. When used as an input, the peripheral with a 0 level must be capable enough to pull the io pin down. Therefore, when using the io pin to directly light up the LED, it is best to use inverse logic, that is, output 0 to make the LED light up. This can ensure the driving ability. That is, the io pin is connected to the negative end of the LED, and the positive end of the LED is connected to vcc through a resistor.
Therefore, when the io pin outputs 1, it does not matter if the external circuit forcibly connects it to the ground. However, when the io pin outputs 0, the external circuit forcibly connects the power supply and the io pin will be damaged. Therefore, after the program is powered on, all io ports are generally written as 1: MOVP0, 0FFH. The
P3 port 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. The author once debugged for a whole day before finding that the serial port could not receive data because RXD was not set to 1. Time was wasted on the periphery, which was very embarrassing at the time.
8: About crystal oscillator
The crystal oscillator of the microcontroller can be simplified into an inverter internally. When the crystal oscillator input pin XI just passes the threshold voltage and is considered to be 1, the output pin XO outputs 0. This 0 will drive the crystal oscillator to make the XI voltage drop. When it drops to the threshold voltage and is considered to be 0, the output pin XO outputs 1. This cycle repeats.
Therefore, when using an oscilloscope to observe the normal working crystal oscillator input pin XI, a horizontal line is obtained that is neither high nor low. XO is a sine wave with a large amplitude. When measuring the crystal input pin XI, the oscilloscope probe should be set to the X10 position, otherwise, the probe can stop the crystal.
Therefore, when wiring, the crystal input pin XI should be as close to the crystal as possible, while the XO pin can be slightly farther away. At the same time, XO has a certain driving capability, and some chips can use it to drive other timing circuits (this is not recommended because the system reliability is reduced).
Previous article:Common brands of microcontrollers
Next article:MCU internal reset circuit diagram
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- The electronic competition is coming again, come and vote, will you participate?
- 【FAQ】 TPS63020: Phase margin improvement method
- KiCad requires the use of the new kicad.org domain
- 【GD32307E-START】+ Dual-color OLED screen driver display
- [Automatic clock-in walking timing system based on face recognition] Maixbit K210 serial communication protocol debugging
- CAN bus receives error information? Add CRC check in the data?
- Working principle, purpose and use of flexible waveguide
- Motor Control Basics - Timer Encoder Mode Usage and Speed Calculation
- Thank you-Chengdu
- [Xianji HPM6750 Review 7] A video player that reads RGB data files from an SD card