I. Summary
This lecture mainly introduces the realization of the minimum system of the 51 series single-chip microcomputer and realizes the output control of the single-chip microcomputer IO port by writing programs. Taking the example of lighting up the externally connected LED (light-emitting diode), briefly introduces the principle of single-chip microcomputer, the composition of the minimum system, and describes the use of the compilation software Keil and downloading Hex files to burn single-chip microcomputer through simple C51 program design.
2. Principle Introduction
Before understanding the principle, let us first think about a question, what is a single-chip microcomputer, and what is the use of a single-chip microcomputer? This is an interesting question, because no one can give a concept that is recognized by everyone, so what is a single-chip microcomputer? Generally speaking, a single-chip microcomputer, also known as a single-chip microcontroller, is a chip that integrates the basic functional components required by a computer, such as CPU (central processing unit), RAM (data memory), ROM (program memory), timer/counter and I/O (input/output) interface with multiple functions, so that complex calculations, logical control, communication and other functions can be completed. Here, we don't need to find a clear concept to analyze what a single-chip microcomputer is, especially when using C language to write programs, we don't need to understand too much about the internal structure and operating principle of the single-chip microcomputer. From the perspective of application, by starting with simple programs, slowly familiarize yourself with and gradually master the single-chip microcomputer.
After a brief understanding of what a single-chip microcomputer is, we will then build the minimum system of a single-chip microcomputer. The minimum system of a single-chip microcomputer is the necessary components for the single-chip microcomputer to work properly and exert its functions. It can also be understood as a system that can work with a single-chip microcomputer composed of the least components. For the 51 series single-chip microcomputer, the minimum system should generally include: single-chip microcomputer, clock circuit, reset circuit, input/output devices, etc. (see Figure 1).
Figure 1 MCU minimum system block diagram
3. Circuit Detail
Based on the above content, the minimum system of 51 series microcontroller is designed as shown in Figure 2.
Figure 2 51 series single-chip microcomputer minimum system
The following is a detailed description of the various circuits of the single-chip microcomputer minimum system shown in Figure 2.
1. Clock circuit
Before designing the clock circuit, let us first understand the clock pins on the 51 microcontroller:
XTAL1 (pin 19): Input terminal of the internal oscillation circuit of the chip.
XTAL2 (pin 18): output terminal of the internal oscillation circuit of the chip.
XTAL1 and XTAL2 are independent input and output inverting amplifiers. They can be configured as on-chip oscillators using quartz crystals, or the device can be driven directly by an external clock. Figure 2 uses the internal clock mode, that is, the internal oscillator circuit is used to connect timing components (a quartz crystal and two capacitors) to the pins of XTAL1 and XTAL2, and the internal oscillator can generate self-excited oscillation. Generally speaking, the crystal oscillator can be selected between 1.2 and 12MHz, and can even reach 24MHz or higher, but the higher the frequency, the greater the power consumption. The 11.0592M quartz crystal oscillator used in this experimental kit. The size of the two capacitors connected in parallel with the crystal oscillator has a slight effect on the oscillation frequency and can play a role in fine-tuning the frequency. When using a quartz crystal oscillator, the capacitor can be selected between 20 and 40pF (30pF is used in this experimental kit); when using a ceramic resonant device, the capacitor should be appropriately increased, between 30 and 50pF. Usually, a 33pF ceramic capacitor is sufficient.
It is also worth mentioning that if the reader designs the printed circuit board (PCB) of the microcontroller system, the crystal and capacitor should be as close to the microcontroller chip as possible to reduce the parasitic capacitance of the lead wire and ensure the reliable operation of the oscillator. The method to detect whether the crystal oscillator is oscillating is to use an oscilloscope to observe the very beautiful sine wave output by XTAL2, or to use a multimeter to measure (set the gear to the DC gear, and the effective value is measured at this time) the voltage between XTAL2 and the ground, and you can see a voltage of about 2V. [page]
2. Reset circuit
In a single-chip microcomputer system, the reset circuit is very critical. When the program runs away (abnormally) or freezes (stops running), a reset is required.
When the reset pin RST (pin 9) of the MCS-5l series microcontroller is at a high level for more than 2 machine cycles, the microcontroller will perform a reset operation. If RST continues to be at a high level, the microcontroller is in a cyclic reset state.
There are usually two basic forms of reset operation: automatic reset at power-on and switch reset. The reset circuit shown in Figure 2 includes these two reset methods. At the moment of power-on, the voltage across the capacitor cannot change suddenly. At this time, the negative pole of the capacitor is connected to RESET, and the voltage is all applied to the resistor. The input of RESET is high, and the chip is reset. Then the +5V power supply charges the capacitor, and the voltage on the resistor gradually decreases, and finally equals to 0, and the chip works normally. The reset button is connected in parallel to both ends of the capacitor. When the reset button is not pressed, the circuit realizes power-on reset. After the chip works normally, the RST pin is made high by pressing the button to achieve the effect of manual reset. Generally speaking, as long as the RST pin maintains a high level for more than 10ms, the microcontroller can be effectively reset. The reset resistor and capacitor shown in the figure are classic values. In actual production, they can be replaced by resistors and capacitors of the same order of magnitude. Readers can also calculate the RC charging time by themselves or measure it in the working environment to ensure the reliability of the reset circuit of the microcontroller.
3. Function and connection of EA/VPP (pin 31)
EA/VPP (pin 31) of 51 MCU is the selection pin of internal and external program memory. When EA is kept at high level, the MCU accesses the internal program memory; when EA is kept at low level, it only accesses the external memory regardless of whether there is internal program memory.
For most of today's single-chip microcomputers, the capacity of their internal program memory (usually flash) is very large, so there is basically no need for external program memory, and the internal memory can be used directly.
In this experimental kit, the EA pin is connected to VCC, and only the internal program memory is used. This must be noted, as many beginners often leave the EA pin floating, which results in abnormal program execution.
4. Connect an external pull-up resistor to the P0 port
The P0 port of the 51 MCU is an open-drain output, and there is no internal pull-up resistor (see Figure 3). Therefore, when used as a normal I/O to output data, since V2 is cut off, the output stage is an open-drain circuit. To make the "1" signal (i.e. high level) output normally, an external pull-up resistor must be connected.
Figure 3 1-bit structure of port P0
In addition, to avoid reading data errors during input, an external pull-up resistor is also required. Here is a brief explanation of the reason: In the input state, the signals read from the latch and the pin are generally consistent, but there are exceptions. For example, when a low level is output from the internal bus, the latch Q = 0, Q = 1, the field effect tube V1 is turned on, and the port line is in a low level state. At this time, no matter whether the external signal on the port line is low or high, the signal read from the pin to the microcontroller is low, so the signal on the port pin cannot be read correctly. For another example, when a high level is output from the internal bus, the latch Q = 1, Q = 0, and the field effect tube V1 is turned off. If the external pin signal is low, the signal read from the pin is different from the signal read from the latch. Therefore, when the P0 port is used as a general I/O interface input, "1" should be written to the P0 port before inputting data. At this time, the Q end of the latch is "0", so that the two field effect transistors V1 and V2 of the output stage are cut off, and the pin is in a floating state, which can be used as a high-impedance input.
In summary: In order to enable the P0 port to drive the NMOS circuit when outputting and avoid reading data errors when inputting, an external pull-up resistor is required. In this experimental kit, an external 10K resistor is used. In addition, in the input operation of the 51 microcontroller to ports P0-P3, in order to avoid reading errors, "1" should be written to the latch in the circuit first to turn off the field effect transistor to avoid interference with the pin reading when the latch is in the "0" state.
5. LED drive circuit
Careful readers may have discovered that in the minimum system, the light-emitting diode (LED) is connected by connecting the power supply to the anode of the diode and then to the I/O port of the microcontroller through a 1K resistor (see connection 1 in Figure 4). Why is it connected like this? First of all, we need to know the working conditions of the LED. Different LEDs have different rated voltages and rated currents. Generally speaking, the working voltage of red or green LEDs is 1.7V~2.4V, the working voltage of blue or white LEDs is 2.7~4.2V, and the working current of 3mm diameter LEDs is 2mA~10mA. Here, a red 3mm LED is used. Secondly, when the I/O port of a 51 microcontroller (such as the STC89C52 microcontroller used in this experimental board) is used as an output port, the ability to pull current (output current to the outside) is at the μA level, which is not enough to light up a light-emitting diode. The current injection (input current to the inside) method can be as high as 20mA, so the current injection method is used to drive the light-emitting diode. Of course, some enhanced MCUs today use current pull output (connection method 2), as long as the output current capability of the MCU is strong enough. In addition, the resistor in Figure 4 is 1K, which is to limit the current and limit the working current of the light-emitting diode to 2mA~10mA.
Figure 4 LED connection
4. Programming
There are two choices for microcontroller programming languages: C and assembly. This series of tutorials uses C to write programs. Here, we will make a simple comparison between C and assembly when developing microcontrollers. Assembly language is hardware-oriented and requires familiarity with hardware features such as registers. It has high execution efficiency, but poor readability and portability. Programs between different microcontrollers cannot be universal. For example, if you have learned the assembly instructions of 51 microcontrollers, you cannot use them on AVR microcontrollers. C language is process-oriented, with good readability and portability, but lower efficiency than assembly. For those who are new to microcontrollers, learning these two languages is the same, but in terms of future development efficiency, the advantages of C language are reflected. It can be ported with almost no changes, which greatly improves the development speed.
C language source program to control the flashing of light-emitting diode D1:
1. Detailed description of the procedure
(1) Header file inclusion. The P0_0 called by the program next is a register address defined in the header file. Before operating the register inside the microcontroller, its source should be stated. Interested readers can look at the contents of the AT89X52.h file.
(2) Macro definition of led makes it easier to understand and modify the program. Name the P0_0 port as led, so that led can be used instead of P0_0 port to perform operations in the program.
(3) Delay function declaration. A function must be declared before it is called. Since the function definition is placed after the main function, the delay function is declared before the main function.
(4) Main function entry. The main function does not pass parameters or return values.
(5) Infinite loop.
(6) Output high level, LED does not light up.
(7) Delay for a period of time so that the human eye can see it directly.
(8) Output low level, LED lights up.
(9) Delay for a period of time.
(10) Definition of delay function.
(11) for statement loop delay.
2. Program flow chart and experimental phenomena
The program flow is shown in Figure 5. After being compiled by the compiler software (keil), the microcontroller burning file is generated, and then it can be downloaded to the microcontroller for operation. The hardware circuit board is shown in Figure 6. The STC89C52RC is used on this experimental board, and the program can be burned through the onboard USB to serial port. Therefore, the USB cable (in this experimental kit) is connected to the computer and the experimental board. The power supply can be taken from the USB or from an external power supply. Cold start, that is, click download first, and then power on. After downloading the program to the microcontroller and running it, you can see that the LED light (D1) connected to the P0_0 port on the experimental board flashes on and off.
[page]
Figure 5 Program flow chart
Figure 6 Hardware circuit board diagram
V. Conclusion
This lecture mainly introduces the design of the 51 MCU minimum system and the writing of the first simple program. Through this experiment, you can master the development process of the MCU and get started quickly. Several issues should be noted in this lecture:
1. This lecture uses C language to write programs because C language has strong readability and portability. If readers have not learned C language, they should understand and master the corresponding C language knowledge. C language is easy to learn and use, and I believe you will be proficient in it soon.
2. The program compiler software used is Keil. Due to space limitations, it is not described here. If readers have any questions, they can go to the microcontroller section of the forum of this magazine. The author has made a detailed Keil introductory tutorial. We use Keil to compile the program and finally generate the Hex code file for burning the microcontroller.
The Keil software interface is shown in Figure 7. The blank area in the middle is the code area, the left side is the project list, and the bottom is the message window.
Figure 7 Keil software development interface
3. The STC microcontroller used in this experiment board downloads the program through the serial port. Its host computer software interface is shown in Figure 8 (V3.1 version is recommended, and the latest version can be downloaded from the STC homepage: http://www.mcu-memory.com/). The burning operation is very simple. Click the "Open File" button to browse and find the generated Hex file to be burned, then power off the microcontroller, click the "Download" button, power on the microcontroller, and the program can be downloaded to the microcontroller.
Figure 8 STC burning software interface
4. Product Components
If readers understand and practice according to the content of this lecture, they can be said to have entered the microcontroller field. The next lecture will go further and introduce the internal timer and interrupt system of the microcontroller. Please stay tuned.
Previous article:Learn 51 MCU with me (Part 2): MCU internal timer/counter and interrupt system
Next article:Application of Single Chip Microcomputer in Infrared Remote Control Technology
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- 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
- [Summary] EEWorld invites you to play disassembly (first issue)
- [Mil MYC-JX8MPQ Review] + Power on and run OpenGL DEMO
- Purgatory Legend-FIFO Battle
- How bias circuit works!!!
- The best articles of Bluetooth in the past 10 years
- Program to replace Matlab results with EXCEL formulas
- C2000 ±0.1° Accurate Discrete Resolver Front-End Reference Design
- EEWORLD University Hall----Live Replay: ADI MEMS Sensors Open a New Era of Conditional State Monitoring
- Unboxing ESP32-S2 and S3
- Easily solve design challenges with MSP430 MCUs