51 MCU tutorial: special function registers of MCU

Publisher:陈书记Latest update time:2012-06-04 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

We know that the microcontroller has ROM, RAM, and parallel I/O ports. So, apart from these things, what else is inside the microcontroller? How are these bits and pieces connected together? Let's make a complete functional analysis of the registers inside the microcontroller!

As we can see in the figure below, there is a CPU inside the 51 single-chip microcomputer for calculation and control, four parallel I/O ports, namely P0, P1, P2, and P3, ROM for storing programs, RAM for storing intermediate results, timer/counter, serial I/O port, interrupt system, and an internal clock circuit. There are so many things inside a 51 single-chip microcomputer.

Further analysis of the above diagram shows that to read and write parallel I/O ports, we only need to send data to the latch of the corresponding I/O port. So how to use the timer/counter, serial I/O port, etc.? In the microcontroller, there are some independent storage units used to control these devices, which are called special function registers (SFRs). In fact, we have already come across the special function register P1. What else are there? See Table 1 below.

We know that the microcontroller has ROM, RAM, and parallel I/O ports. So, apart from these things, what else is inside the microcontroller? How are these bits and pieces connected together? Let's make a complete functional analysis of the registers inside the microcontroller!

As we can see in the figure below, there is a CPU inside the 51 single-chip microcomputer for calculation and control, four parallel I/O ports, namely P0, P1, P2, and P3, ROM for storing programs, RAM for storing intermediate results, timer/counter, serial I/O port, interrupt system, and an internal clock circuit. There are so many things inside a 51 single-chip microcomputer.

42.gif

Further analysis of the above diagram shows that to read and write parallel I/O ports, we only need to send data to the latch of the corresponding I/O port. So how to use the timer/counter, serial I/O port, etc.? In the microcontroller, there are some independent storage units used to control these devices, which are called special function registers (SFRs). In fact, we have already come across the special function register P1. What else are there? See Table 1 below.

41.jpg
42.jpg
Table 1[page]

43.jpg

44.jpg

45.jpg

Next, we introduce several commonly used SFRs, see Figure 2.

ACC: Accumulator, often represented by A. You can't understand what it is from the name. It is a register, not a thing for adding. Why is it given such a name? Perhaps it is because one of the numbers must be in ACC when the arithmetic unit performs calculations. Its name is special and its identity is also special. Later we will learn about instructions and find that all calculation instructions are inseparable from it.

2. B: A register. It is used to store the multiplier or divisor when doing multiplication or division. It can be used as you wish when not doing multiplication or division.

3. PSW: Program Status Word. This is a very important thing. It contains many states of the CPU when it is working. Through this, we can understand the current state of the CPU and make corresponding processing. Please refer to Table 2 for its functions.

46.jpg Table 2

PSW is also called the flag register. It is very important to understand the principle of the microcontroller. It stores various related flags. Its structure and definition are as follows:

47.jpg

[page]

Below we introduce the uses of sfr one by one

(1) CY: Carry flag. It is used to indicate whether Acc.7 has carried to a higher bit. The arithmetic unit in 8051 is an 8-bit arithmetic unit. As we know, an 8-bit arithmetic unit can only represent 0-255. If we add two numbers, the sum of two numbers may exceed 255, so the highest bit will be lost, causing an operation error. What should we do? The highest bit is carried here. This way, everything is fine.

Example: 78H+97H (01111000+10010111)

(2) AC: Auxiliary carry flag, also called half carry flag. It is used to indicate whether Acc.3 carries to Acc.4.

Date: 57H+3AH(01010111+00111010)

(3) F0: User flag. We (programmers) decide when to use it and when not to use it.

(4) RS1, RS0: Working register bank selection bits. We already know this.

RS1, RS0 = 00 —— Area 0 (00H~07H)

RS1, RS0 = 01 —— Area 1 (08H~0FH)

RS1, RS0 = 10 —— Area 2 (10H~17H)

RS1, RS0 = 11 —— Zone 3 (18H~1FH)

(5) 0V: Overflow flag. Indicates that Acc overflows in signed arithmetic operations. We will discuss overflow later.

(6) P: Parity bit: It is used to indicate the parity of the number of binary digits "1" in the ALU operation result. If it is an odd number, P = 1, otherwise it is 0.

Example: The result of a certain operation is 78H (01111000). Obviously, the number of 1s is an even number, so P=0.

4. DPTR (DPH, DPL): Data pointer, which can be used to access any unit in the external data memory. If not used, it can also be used as a general register. It is up to us to decide how to use it. 16 bits, composed of two 8-bit registers DPH and DPL. Mainly used to store a 16-bit address as an address pointer to access external memory (external RAM and ROM).

5. P0, P1, P2, P3: As we already know, these are the registers of four parallel input/output ports. The contents in them correspond to the output of the pins.

6. SP: Stack pointer. (Specially used to point out the address of the top data of the stack.)

Introduction to stacking: In daily life, we have all noticed this phenomenon: the dishes at home are stacked one by one, the latest one is placed on the top, and the earliest one is placed at the bottom. When taking things out, it is just the opposite, taking things from the top first. We can summarize this phenomenon in one sentence: "First in, last out, last in, first out". Please think about it, where else can we find this phenomenon? In fact, it can be found everywhere, such as the bricks and materials piled up on the construction site, and the goods in the warehouse, which are all "first in, last out, last in, first out". This is actually a rule for storing and retrieving items, which we call "stack".

In a single-chip microcomputer, we can also construct such an area in RAM to store data. The rule for storing data in this area is "first in, last out, last in, first out", which we call a "stack". Why do we need to store data in this way? Can't the memory itself store data by address? Yes, knowing the address does allow us to know the content inside, but if we need to store a batch of data, wouldn't it be troublesome to know the address of each data? If we place the data one by one, then we only need to know the address unit of the first data (see Figure 2). If the first data is at 27H, then the second and third will be at 28H and 29H. Therefore, using a stack to store data can simplify operations.

So where is the stack in 51? The area that can store data in the single-chip microcomputer is limited. We cannot allocate a place for the stack, so we open up a place in the memory (RAM) for the stack, but which part of the memory should be used? It is still difficult to decide, because 51 is a general-purpose single-chip microcomputer, and the actual needs of each person are different. Some people need more stacks, while others do not need so much, so no matter how to allocate it, it is not suitable. How to solve this problem? If it is not easy to divide it, just don't divide it. Give the right to divide it to the user (programmer) and decide it according to their own needs. Therefore, the position of the stack in the 51 single-chip microcomputer can change. And this change is reflected in the change of the value in SP. Look at Figure 2. The value in SP is equal to 27H, isn't it equivalent to a pointer pointing to the 27H unit? Of course, in the real 51 machine, the position pointed to by the pointer at the beginning is not the location where the data is stored, but the previous location where the data is stored. For example, if the pointer points to the 27H unit at the beginning, then the location of the first data is the 28H unit, not the 27H unit. Why is this the case? We will explain it when we learn the stack command. We will introduce other SFRs when they are used.

48.jpg

Reference address:51 MCU tutorial: special function registers of MCU

Previous article:Design and implementation of automobile anti-theft alarm system based on single chip microcomputer
Next article:Design of RS 232 serial data interceptor based on AT89C51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 20:55

AVR Assembly Program Part 1: The Internal Structure of the AVR MCU's CPU
I have been studying microcontrollers for so long, and I feel that if I want to go deeper, I have to read assembly language, or at least understand the internal structure of the microcontroller. Below, we will take ATmega16 as an example to introduce the AVR microcontroller structure and assembly language. AVR microco
[Microcontroller]
AVR Assembly Program Part 1: The Internal Structure of the AVR MCU's CPU
How does a program run in the CPU (Part 3)
Preface In the previous two articles, How a Program Runs in the CPU (I) described how instructions and data are executed one by one in the CPU. How a Program Runs in the CPU (II) focused on the PC register and explained how the program is executed in order in the CPU from the perspective of assembly language. This art
[Microcontroller]
How does a program run in the CPU (Part 3)
AMD cancels K12 ARM CPU project after legendary chip architect Jim Keller leaves
According to foreign media WccfTech, legendary chip architect Jim Keller said at a meeting that his K12 ARM CPU project was "stupidly canceled" after he left his former employer AMD. The "Future of Computing" conference was reportedly held by the Department of Computer Science and Automation at the Indian Institut
[Home Electronics]
AMD cancels K12 ARM CPU project after legendary chip architect Jim Keller leaves
Detailed explanation of s3c44b0 cpu 8K cache SRAM initialization
Regarding the initialization problem of the 8K cache SRAM inside the s3c44b0 cpu. The problem is mainly caused by cpu_init() calling the icache_enable() function, which in turn calls s3c44b0_flush_cache() The s3c44b0_flush_cache() function has only a few lines, and the code is as follows: static void s3c44b0_fl
[Microcontroller]
Detailed explanation of s3c44b0 cpu 8K cache SRAM initialization
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号