Learn 51 MCU from me (I): MCU minimum system composition and I/O output control

Publisher:EnchantedDreamsLatest update time:2013-02-07 Source: dzsc Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  Single-chip microcomputer is a technology with strong practicality. Many beginners are often confused when learning single-chip microcomputer technology development and don't know where to start. To this end, the author combined his many years of experience in using single-chip microcomputers to specially design the Study-c machine and hardware kit required for single-chip microcomputer development, and combined with the kit, carefully compiled a series of tutorials on single-chip microcomputers from entry to mastery. By explaining the principles of single-chip microcomputers, circuit design, application development software tools, and writing experimental examples, readers can fully contact single-chip microcomputer technology. The tutorial is arranged from the shallow to the deep, step by step, and the content strives to be complete, practical, and interesting, so that readers can gradually improve the comprehensive design level of single-chip microcomputer software and hardware in a relaxed and enjoyable learning process.

  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

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

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

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

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 5 Program flow chart

Figure 6 Hardware circuit board diagram

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

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

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.

Reference address:Learn 51 MCU from me (I): MCU minimum system composition and I/O output control

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号