What is the purpose of each bit in the register?

Publisher:快乐旅人Latest update time:2020-09-21 Source: elecfansKeywords:register Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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?

Keywords:register Reference address:What is the purpose of each bit in the register?

Previous article:Detailed explanation of the running process of the microcontroller program
Next article:Various versions of ST-LINK emulator

Latest Microcontroller Articles
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号