MCU sfr and sbit keywords

Publisher:fengtingLatest update time:2022-07-05 Source: csdnKeywords:MCU  sfr  sbit Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The usage of bit is similar to sfr, except that sbit is a bit operation, which is used to assign a specific bit in an sfr to a variable, so that the program can use the variable to clear or set the bit.

 


sfr is used to assign a special function register of a microcontroller to a variable, so that the variable can refer to the register in subsequent programs.


The usage of sbit is similar to that of sfr, except that sbit is a bit operation, which is used to assign a specific bit in an sfr to a variable, so that the program can use the variable to clear or set the bit.


The special function register layout of STC series microcontrollers is as follows:

6597809737983524400.gif

After looking at so many special function registers in Figure 1, you may have some confusion. We use sfr P0 = 0×80 to represent P0 and sfr SP = 0×81 to represent SP. There is no ambiguity. What is confusing is: if sbit P0_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 sbit SP_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, SFR 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, sbit SP_0 = 0×81 in the example is invalid for the SP register and should be written as sfr SP=0x81.


For example: sbit P1^1=0x81;sfr SP=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:MCU  sfr  sbit Reference address:MCU sfr and sbit keywords

Previous article:Nuvoton N76E003AT20 multi-channel ADC acquisition method
Next article:A brief discussion on the use of macro definitions in single-chip microcomputer development

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号