This article is suitable for those who are just getting started with microcontrollers.
In the field of electronics, a single-chip microcomputer is a very simple device. From the outside, it is just a chip with many pins. Inside, it is a bunch of registers. Different single-chip microcomputers may have different external shapes, pin numbers, and pin names. Internally, the register names are different.
What we need to do is to write programs to control the registers in the microcontroller, and then display them through pins to complete various electronic products.
Therefore, when you read the programs written by others, you will often see the statements that assign values to the registers of the microcontroller. The following is an explanation of the simplest control of the high and low levels of the pins of the 51 microcontroller. (Don't always think that the 51 microcontroller has been eliminated. The 51 microcontroller is still the microcontroller with the largest shipment volume, and major companies launch new 51 microcontrollers every year, with more and more powerful functions. When making products, you should choose the most suitable microcontroller, instead of always thinking of using the best microcontroller.)
For example, the microcontroller P1 port has 8 pins, namely P1.0~P1.7. If you want all 8 pins of P1 port to output high level, you write it like this: P1=0XFF; if you want all 8 pins of P1 port to output low level, you write it like this: P1=0X00; if you only want P1.0 to output high level and other pins to output low level, you write it like this: P1=0X01......
How do these values come from?
Calculation method:
A number that starts with 0X indicates that the number behind it is a hexadecimal number. So first of all, we need to know how to convert between decimal and hexadecimal.
Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The above hexadecimal and decimal systems are in correspondence, for example, B corresponds to 11.
Like computers, microcontrollers use "bits" as the smallest unit. For example, P1 is an 8-bit register. The 8 bits of the P1 register are: P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
The numbers starting with 0X above are calculated in units of 4 "bits" and added up to 8421. This may sound confusing, but an example will make it clear.
You could make a table like this:
As can be seen from the table above, the "bits" of the register are arranged from high to low, and then in groups of 4. The corresponding 8421 code is shown in the figure above.
Writing a value to a register is actually writing 0 or 1 to each bit. For example, if you want to make a pin high, write 1 to the corresponding bit; if you want to make a pin low, write 0 to the corresponding bit. Then add up the corresponding 8421 bits that are 1, in groups of 4 bits.
For example, if we want each pin to output a high level, that is, the value of each bit is 1, then the corresponding upper four bits of the register are 8+4+2+1=15, 15 corresponds to hexadecimal F, and then look at the lower four bits of the register, 8+4+2+1=15, which still corresponds to F, so the final value is 0xFF.
Let's look at it again. If each pin outputs a low level, the value of each bit is 1. As mentioned before, only when the corresponding bit is 1, their 8421 are added together. Now they are all 0, so there is no need to add them. The result is 0x00.
Let's look at it again. If only the P1.0 pin is high level and other pins output low level, then only the bit corresponding to P1.0 is 1, and the other bits are 0. Therefore, we only need to take the 1 code on P1.0, so the result is 0x01.
Let's look at it again. If P1.0, P1.1, P1.7, and P1.5 are set to output high level and other pins are set to output low level, the corresponding values are shown in the figure below:
We look at the upper four bits of the register, take the code corresponding to 1 8+2=10, 10 corresponds to hexadecimal A, and then look at the lower four bits of the register, take the code corresponding to 1 2+1=3, the hexadecimal corresponding to 3 is still 3, so the result is 0XA3. This means that if you write P1=0XA3, the pin will be high when it should be high, and low when it should be low.
No matter it is the 8-bit register of 51 or the 32-bit register of ARM, each bit in the register has a certain purpose, which can be found in the user manual of the microcontroller. You only need to write 1 or 0 to the corresponding bit according to your requirements, and then use the above method to get the result, and you can operate the register of the microcontroller.
Have you learned it?
Previous article:Detailed explanation of the running process of the microcontroller program
Next article:Various versions of ST-LINK emulator
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- 【Smart water cup holder】06-Work submission
- Here the role of the voltage regulator + diode combination
- Bluetooth bracelet master control selection
- 【XMC4800 Relax EtherCAT Kit Review】+ Getting Started with DAVE, PWM Module Application
- Explain the circuit diagram, thanks
- STM32 branch can support flash chips larger than 16MB
- RSL10-SENSE-DB-GEVK Environmental Monitoring Node Design
- AT32F425-Evaluation Report-CANOpen Porting 05
- RSIC-V IDE MRS User Notes (V): Code Erasure Problem Before Debugging
- There are indeed many people who ask for money, and the most annoying ones are the brainless ones.