C51 Definition of Special Function Registers in MCS51 Microcontroller

Publisher:快乐微笑Latest update time:2016-05-17 Source: eefocusKeywords:MCS51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Before we start to talk about the definition of the special register (SPR) in the C51 microcontroller, let's first briefly introduce the two keywords "sbit" and "sfr" that we often see when developing 51 microcontrollers:

SFR is used to assign a special function register of a microcontroller to a variable, so that the variable can be used to refer to the register in the subsequent program
. SBI is similar to SFR, except that SBI is a bit operation and is used to assign a specific bit in an SFR to a variable, so that the subsequent program can use the variable to clear or set the bit.

Next, we take the STC series 51 microcontroller as an example to briefly understand the special function register layout of the microcontroller, as follows:




    		    C51 Definition of Special Function Registers in MCS51 Microcontroller

In the MCS-51 microcontroller, except for the program counter PC and 4 groups of working registers, all other registers are special function registers (SPRs), which are scattered in the upper 128 bytes of the on-chip RAM area, with an address range of 80H~0FFH. There are 11 registers in the SFR that have bit addressing capabilities, and their byte addresses can be divided by 8, that is, the byte address ends with 8 or 0.

In order to directly access these SPRs, FranklinC51 provides an independent definition method, which is incompatible with the standard C language and is only suitable for C language programming of the MCS-51 series of microcontrollers. The general syntax format of the special function register C51 definition is as follows:
sfrsfr-name=intconstant;
"sfr" is the keyword of the definition statement, which must be followed by a special function register name that actually exists in the MSC-51 microcontroller. The "=" must be followed by an integer constant, and expressions with operators are not allowed. It is the byte address of the special function register "sfr-name". The value range of this constant must be within the SFR address range, located between 0x80 and 0xFF.
For example:
sfrSCON = 0x98; /* Serial port control register at address 98H*/
sfrTMOD = 0x89; /* Timer/Counter mode control register at address 89H*/
The number and type of special function registers in the MCS-51 series of microcontrollers vary, so it is recommended to put all special "sfr" definitions into a header file that should include the SFR definitions for the MCS-51 microcontroller series models. The "reg51.h" header file of the C51 compiler is such a file.
In the new MCS-51 series products, SFRs are often functionally combined into 16-bit values. When the high byte address of the SFR is located directly after the low byte, the value of the 16-bit SFR can be directly accessed. This is the case for Timer/Counter 2 of the 52 sub-series. In order to effectively access this type of SFR, the keyword "sfr16" can be used to define it. The syntax format of its definition statement is the same as that of 8-bit SFR, except that the address after "=" must use the low-byte address of the SFR where 16 is located, that is, the low-byte address is used as the definition address of "sfr16". For example:
sfr16T2=0xCC/*Timer/Counter 2; T2 low 8-bit address is 0CCH, T2 high 8-bit address is 0CDH*/
This definition is applicable to all new 16-bit SFRs, but cannot be used for timer/counter 0 and 1.
For the bits in the bit-addressed SFR, the expansion function of C51 supports the definition of special bits, which is not compatible with standard C like SFR, and uses "sbit" to define the bit addressing unit.
There are three general syntax formats for definition statements:
The first format: sbitbit-name=sfr-name^intconstant;
"sbit" is the keyword of the definition statement, followed by an addressing bit symbol name (the bit symbol name must be the bit name specified in the MCS-51 microcontroller), and the bit number in "sfr=name" after "=" must be a number in the range of 0~7. For example:
sfrPSW=0Xd0; /*Define the PSW register address as D0H*/
sfrOV=PSW^2; /*Define the OV bit as PSW.2, the address as D2H/*
sfrCY=PSW^7; /*Define the CY bit as PSW.7, the address as D7H^*/
The second format: sbitbit-name=intconstant^intconstant;
The intconstant after "=" is the byte address of the special function register where the addressing address is located, and the intconstant after the "^" symbol is the bit number of the addressing bit in the special function register. For example:
sbitOV=0Xd0^2; /*Define the OV bit address as the 2nd bit in the D0H byte*/
sbitCY=0XD0^7; /*Define the CY bit address as the 7th bit in the D0H byte*/
The third format: sbitbit-name=intconstant;
The intconstant after "=" is the absolute address of the addressing bit. For example:
sbitOV=0XD2; /*Define the OV bit address as D2H*/
sbitOY=0XD7; /*Define the CY bit address as D7H*/
The special function bit represents an independent definition class and cannot be interchanged with other bit definitions and bit fields.

After understanding the definition of special function registers, some people may have questions:

We use sfrP0=0×80 to represent P0, and sfrSP=0×81 to represent SP, which is unambiguous. But there is a question: if sbitP0_1=0×81 is used to represent the first bit of port P0, then what should I do if I want to represent the 0th bit of the SP register? If it is also defined as sbitSP_0=0×81, then there will be obvious ambiguity, and the compiler cannot understand it. In fact, this problem does not exist. As can be seen from Figure 1, SPR can be divided into two areas: bit-addressable area and non-bit-addressable area. The register address of the bit-addressable area can be divided by 8, while the register address of the non-bit-addressable area does not meet this requirement. Therefore, sbitSP_0=0×81 in the example is invalid for the SP register and should be written as sfrSP=0x81.

For example: sbitP1^1=0x81;sfrSP=0x81;
Although they both refer to the same address 0×81, the compiler has completely different meanings. The former is a byte address because of the sfr keyword, while the latter is a bit address because of the sbit keyword, indicating a bit.
Keywords:MCS51 Reference address:C51 Definition of Special Function Registers in MCS51 Microcontroller

Previous article:51 MCU Learning Notes (V)_C51 Implementation of MCU Serial Communication Simulation
Next article:MCS51 MCU timer/counter concept very good register relationship diagram

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号