Interpretation of the simple control book of the register in the single chip microcomputer

This article is suitable for the microcontroller that just got started.

In the electrical computer, the single-chip microcomputer is a very simple device. When viewed from the outside, it is a chip with many feet. Internally, it is a bunch of registers. Different microcontrollers, the external performance is that the shape and pin count and pin names may be different, internal, that is, the register name is not the same.

What we need to do is to write a program to control the registers inside the microcontroller, and then complete the various electronic products through the pins.

Therefore, when you look at a program written by someone else, you will often see a statement that assigns a value to the microcontroller register. The following is the simplest way to control the 51 MCU pin high and low level. (Do not always have the idea that 51 single-chip microcomputers have been eliminated, 51 single-chip microcomputers are still the largest single-chip microcomputers, and every major company has a new 51 single-chip microcomputer introduced every year, the function is getting stronger and stronger, to make products, to choose the most The right microcontroller, not always want to use the microcontroller.)

For example, the P1 port of the MCU has 8 pins, which are respectively P1.0~P1.7. If you want to let the 8 pins of the P1 port output high level, it is written like this: P1=0XFF; if you want P1 port The 8 pins all output low level, so write: P1=0X00; if only P1.0 output is high, other pins output low level, so write: P1=0X01..... .

How did these values ​​come from?

Calculation method:

A number, preceded by 0X, indicates that the following number is a hexadecimal number. So first, we need to know the conversion between decimal and hexadecimal.

Hexadecimal: 0 1 2 3 4 5 6 7 8 9 ABCDEF Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The above hexadecimal and decimal are the corresponding relations, for example, B corresponds to 11.

A microcontroller, like a computer, uses "bits" as the smallest microcontroller. For example, P1 is an 8-bit register. The eight bit names of the P1 register are: P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0

The number at the beginning of 0X above is calculated by taking 4 "bits" as a unit, and adding the calculation with 8421 yards. It is very confusing here. Just give an example to understand.

You can make a form like this:

As seen from the above table, the "bits" of the registers are arranged from high to low, and then grouped by 4, and the corresponding 8421 code is as shown in the above figure.

Writing a value to a register actually writes 0 or 1 to each of its bits. For example, to make which pin is high, write 1 to the corresponding bit; to make which pin is low, Write 0 to the corresponding bit. Then add 8421 corresponding to 1 and group it in 4 bits.

For example, we want each pin to output a high level, that is, the value of each bit is 1, which corresponds to the upper four bits of the register, 8+4+2+1=15, and 15 corresponds to the hexadecimal The F, then look at the lower four bits of the register, 8 + 4 + 2 + 1 = 15, or corresponding to F, so the final value is 0xFF.

Look again, if you let each pin output low level, the value of each bit is 1. As mentioned before, only the corresponding bit is 1, only add their 8421, now it is 0, so both No need to add, the result is 0x00.

Look again, if only the P1.0 pin is high and the other pins output low level, then only the bit corresponding to P1.0 is 1, and the other bits are 0, so only P1.0 is required. The above 1 code, so the result is 0x01.

Look again, if you let P1.0, P1.1, P1.7, P1.5 output high level, other pins output low level, then the corresponding values ​​are as shown below:

We look at the upper four bits of the register, take the corresponding code 8+2=10, 10 corresponds to the hexadecimal A, then look at the lower four bits of the register, and take the corresponding code above 2+1=3,3 The corresponding hexadecimal is still 3, so the result is 0XA3. This is, if you write P1=0XA3, you can make the pin high, and the low is low.

Whether it is the 8-bit register of 51 or the 32-bit register of ARM, each bit in the register has a certain intention. These intentions can be found in the user manual of the MCU. You only need to give the corresponding bit according to your requirements. Write 1 or 0, and then use the above method to get the result, you can operate the microcontroller's registers.

Have you learned it?

HCOW IMonster 6000puffs

The HCOW MONSTER Disposable Vape Devices are designed with simple vaping in mind.It Featuring 12ml of E-Liquid,has a nicotine strength of 5% nicotine salt, offering a smooth inhale and satisfying vape.HCOW MONSTER Disposable Vape internal 500mAh battery,approximately 600 puffs of vaping enjoyment.
HCOW IMONSTER features:
Puffs:6000Puffs
Battery Capacity:1000mAh
E-liquid Volume:12ml
Nicotine Content:5% nicotine salt
HCOW IMONSTER comes with:

1*HCOW IMONSTER Disposable Vape

48

Hcow Imonster 6000Puffs,6000 Puffs Vape ,15Ml Of E-Liquid ,Electronic Cigarette Vape Puff Bar

Shenzhen Kester Technology Co., Ltd , https://www.kesterpuff.com