51 Single-chip microcomputer for beginners 1-51 Introduction to single-chip microcomputer

   At present, there are many types of single-chip microcomputers, and they occupy an important position in the field of automatic control with their superior control performance. Nowadays, single-chip microcomputers are everywhere. For example, our refrigerators, air conditioners, and washing machines are all controlled by single-chip microcomputers internally. Of course, the single-chip microcomputers they use are customized, not the single-chip microcomputers we use when we study. In addition, different brands of MCU chips will use different cores, such as INTEL's MCS-51 core (representative chip: AT89 series, domestic STC series, etc.), ARM's high-performance "Cortex-M3" core (representative chip: STM32 series), etc. Because of the difference in the kernel, they will also be different in use.

For beginners, the STC89C52 in the 51 single-chip microcomputer is a very good choice, because it is cheap (the price of a single chip is around 8RMB), and it is relatively easy to learn. Next, we will take this chip as an example to introduce the 51 single-chip microcomputer.

First, a brief introduction to this chip:

1. STC89C52 is a low-power, high-performance CMOS 8-bit microcontroller produced by STC Company (Shenzhen Hongjing Technology Co., Ltd., China), with 8K bytes of system programmable Flash memory (8K is not small for a microcontroller, of course Can not be compared with our mobile phone), using the MCS-51 core, the instruction code is fully compatible with the traditional 8051.

2. The working voltage range is very wide (only the 5V version is introduced here), and it can work between 3.3V~5.5V, but the higher the voltage, the greater the working current. Generally speaking, if you use a single-chip microcomputer to light up a LED lights, using 5.5V power supply, the brightness of the light is brighter than that of 3.3V power supply, and the power consumption will be more.

3. General I/O port (4x8=32), after reset: P1/P2/P3 is quasi-bidirectional port /weak pull-up, P0 port is open-drain output , when used as I/O port, you need to add pull resistor. (Unless an I/0 port is set to zero, it will output a high level)

4. Working frequency range: 0~40MHz, equivalent to 0~80MHz of ordinary 8051, the actual working frequency can reach 48MHz, and its working frequency depends on the frequency of the crystal oscillator. We usually use 12MHz and 11.0592MHz.

5. ISP (programmable in system)/IAP (programmable in application), can directly download user programs through serial ports (RxD/P3.0, TxD/P3.1). In the learning process, we only need to use ISP, that is, after making the circuit board, we only need to lead out RxD/P3.0, TxD/P3.1 through the pin header, and we can burn the program through USB-TTL To the single-chip microcomputer, there is no need to take the single-chip microcomputer chip out of the system board.

6. Three 16-bit timer /counters in total. Namely timer T0, T1, T2

Other parameters will not be introduced in detail, and their information can be consulted online.

First of all, you need to understand the pin diagram of this chip in order to connect various components according to the pins. (Temporarily only mark the commonly used functions)

Let's explore each module in detail:

 1, I/O port

The I/O port is the input and output port, which is the port used by the microcontroller to output or input signals. STC89C52 has four groups (P0, P1, P2, P3), 8 in each group (each group of I/O ports can output 8-bit binary numbers at the same time. The data width of the CPU calculation is also exactly 8 bits, so the chip is 8 bit controller), a total of 32 I/O ports.

The default state of all I/O ports of the microcontroller is high level, unless it is set to 0 in the program, such a setting can make the microcontroller run more stably.

Usually, the I/O port is connected with a pull-up resistor, the purpose of which is to make the MCU run stably, and the second is to improve the driving ability of the MCU, that is, to allow the MCU to control a larger load. We usually use 9P pull-up resistors with a resistance of 10K (the size can be 1~10K, small resistance can improve the driving ability, and large resistance can reduce power consumption). It has 9 pins, one is the common end, and the other eight pins are connected to the I/O port. Its structure and wiring diagram are shown in the figure below. Note that in addition to the pull-up resistor, there is also a pull-down resistor. The common end of the pull-up resistor is connected to VCC, and the common end of the pull-down resistor is connected to GND.

2. Clock circuit

The clock here is not what we call a clock, but a signal (an ideal clock signal is a rectangular wave, or a pulse wave). Learning from digital electronic technology, we know that in logic circuits, we must rely on clock signals to work (such as flip-flops, each time a clock signal is received, it will act once), and the single-chip microcomputer can actually be regarded as an integrated logic circuit. So we need an external clock circuit to make the microcontroller work.

The core of the clock circuit is the crystal oscillator, which is an electronic component that can generate a stable oscillation frequency. Its basic parameter is the oscillation frequency, the unit is MHz, and its parameter determines the operating frequency of the microcontroller. Its value is generally engraved on the crystal oscillator component. Commonly used MCU crystal oscillators mainly include 12.000MHz and 11.0592MHz. When a timer is used in the program, the 11.0592MHz crystal oscillator can be used for more accurate timing.

In addition to the crystal oscillator, the clock circuit also has two ceramic capacitors (30pF). These two resistors can play a role in fine-tuning the frequency.

The crystal oscillator circuit has two ports XT1 and XT2. Connect these two ports to pin 18 (XTAL2) and pin 19 (XTAL1) of the microcontroller to provide clock signals for the microcontroller (these two ports have no order and can be connected at will) .

3. Reset circuit
The reset circuit is used to restart the single-chip microcomputer, initialize the single-chip microcomputer, and restart the execution of the program. When the single-chip microcomputer fails due to program problems (such as an uncontrollable infinite loop in the program), a reset signal can be sent to the 9th pin RET of the single-chip microcomputer through the reset circuit, and the single-chip microcomputer can reset itself. This reset signal is a high level for 2 consecutive machine cycles (24 clock cycles). That is, if the RES pin of the microcontroller is set to high level for two consecutive machine cycles, the microcontroller will automatically reset.

So the simplest reset circuit is shown in the figure above. Press the switch K, VCC is connected to the single-chip microcomputer RST, and after two machine cycles, the single-chip microcomputer is reset. (In actual use, you press the button and release it immediately. This time has exceeded two machine cycles, so you don’t need to pause and then release the reset button.) If you keep pressing the reset button, the microcontroller will not enter the working state , the MCU starts to work only when the reset button is released.

Generally speaking, after the single-chip microcomputer is powered on, it will automatically execute from the beginning of the program, and all registers are also initial values. There is no problem in executing the program immediately after power-on. But in some complex circuits, there are many external circuits of the single-chip microcomputer, sometimes there are capacitors and inductors, and they cannot enter the working state immediately after they are powered on. They have a "charging" process. An instruction is at the microsecond level, and it is easy for the microcontroller to misjudge and make unexpected errors. How to avoid this phenomenon?

Please imagine: if the single chip microcomputer does not enter the working state immediately after it is powered on, but stops for a while, waits for other components to be charged, and then enters the working state, then the unstable factors at the initial stage of power-on can be avoided, thereby avoiding misjudgment .

So how to achieve this pause? This depends on the power-on automatic reset of the reset circuit .

The above is a commonly used reset circuit for microcontrollers.

Its principle involves the KCL law in the circuit theory and the zero-state response of the first-order circuit, which can be skipped if you are not interested.

After power-on, the electrolytic capacitor C1 is charged, and its voltage changes as Uc=5(1-e^{-t/\tau }) [Equation 1],

The potential at RET is the voltage to ground. It can be seen that the voltage of resistor R9 is just equal to the potential of RET in value.

According to Kirchhoff's voltage law, the voltage of R9 = power supply voltage - capacitor C1 voltage, that is Ur=5-5\left ( 1-e^{-t/\tau } \right )=5e^{-t/\tau },

τ in the formula is a time constant, its value is τ=RC=1000X22/1000000=22ms, and its voltage change curve is shown in the figure (simplified drawing, for reference only):

It can be seen that for a long period of time (actually less than 20ms), Ur is kept at a relatively high potential, that is, RET is at a high level (the MCU with a voltage above 3.7V is considered a high level), during this time , enough for the MCU to avoid the instability at the initial stage of power-on. When the voltage of Ur is reduced to not enough for the single-chip microcomputer to judge as a high level, the reset fails, and the single-chip microcomputer works.

It can be seen that this reset circuit can also be manually reset. Press the switch S1, the capacitor C1 will discharge through R10, release S1, and the capacitor C1 will start charging again. The situation is the same as the power-on reset. RET receives a high level and the microcontroller resets. .
Therefore, the function of the reset circuit is to automatically reset when the power is turned on, avoiding the instability when powering on;

4. Other pins

Parallel serial port: The 10th and 11th pins are the parallel serial port of the single chip microcomputer, the function is to burn the program. Note that these two pins are multiplexed by P3.0 and P3.1. The burning program needs to use the burning tool USB-TTL.

The actual USB-TTL has many models, and some models have 10 pins, but the programming program only needs four (VCC, GND, RXD, TXD). So we need four DuPont lines. First, connect the VCC and GND on the conversion tool to the VCC and GND on the single-chip system board, then connect the RXD of the burning tool to the TXD of the single-chip, and the TXD of the burning tool to the RXD of the single-chip connect. Because one of the two pins is to send data and the other is to receive data, the conversion tool sends data (TXD), and the microcontroller naturally receives data (RXD), and vice versa. In addition, the programming program also needs to use the software STC-ISP, which is a programming software specially used for STC series microcontrollers.

Memory gating: The 31 pin of the single chip microcomputer is the memory gating EA, the internal storage space of the single chip microcomputer is very small, if you need to expand the space, you need to use this function. When EA is set at a high level, the MCU starts to execute the program from the internal memory; when EA is set at a low level, the MCU starts to execute the program from the external memory. For beginners, it is enough for us to use the internal memory, so we can directly connect EA to VCC.

Similarly, the 30th pin ALE is the address latch signal, which is also used when there is an external memory, and it can be suspended when not in use; the 29th pin PSEN is the external memory read strobe signal, which is also suspended. Needless to say, the other 40 feet and 20 feet are respectively connected to the positive pole and negative pole (ground) of the power supply.

The above is purely personal understanding, if there is any mistake, please point it out.

​​​​​​​​​​If you find it useful, you can give it a thumbs up~

Guess you like

Origin blog.csdn.net/qq_55203246/article/details/114392318