Definition and application methods of variables in 51 microcontrollers

Publisher:快乐旅行Latest update time:2024-02-28 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A variable is a quantity whose value can continuously change during program execution. To use a variable in a program, you must first use an identifier as the variable name and indicate the data type and storage mode used, so that the compilation system can allocate corresponding storage space for the variable. The format for defining a variable is as follows:

In the definition format, except for the data type and variable name table, which are necessary, everything else is optional. There are four storage types: automatic (auto), external (extern), static (statIC) and register (register). The default type is automatic (auto).


The data types here are the same as the definitions of the data types we learned in Lesson 4. After specifying the data type of a variable, you can optionally specify the memory type of the variable. The description of the memory type is to specify the storage area used by the variable in the C51 hardware system and accurately locate it at compile time. Note that the RAM in the AT89C51 chip only has the lower 128 bits, and the upper 128 bits located from 80H to FFH are only useful in the 52 chip and overlap with the special register address. For the address table of the special register (SFR), please see Appendix 2 AT89C51 Special Function Register List

Definition and application methods of variables in 51 microcontrollers

If the memory type is omitted, the system will specify the storage area of ​​the variable according to the default memory type specified by the compilation mode SMALL, COMPACT or LARGE. Variables can be declared in any 8051 storage area regardless of storage mode. However, placing the most commonly used commands such as loop counters and queue indexes in the internal data area can significantly improve system performance. It should also be pointed out that the storage type of the variable has nothing to do with the memory type.

SMALL storage mode places all function variables and local data segments in the internal data storage area of ​​the 8051 system, which makes accessing data very fast, but the address space of SMALL storage mode is limited. When writing small applications, it is good to place variables and data in the data internal data memory because the access speed is fast, but in larger applications, it is better to store only small variables, data or commonly used variables in the data area. (such as cycle count, data index), while large data is placed in other storage areas.

All functions and program variables and local data segments in the COMPACT storage mode are located in the external data storage area of ​​the 8051 system. The external data storage area can have up to 256 bytes (one page). In this mode, the short address of the external data storage area is @R0/R1.

LARGE storage mode The variables and local data segments of all functions and procedures are located in the external data area of ​​the 8051 system. The external data area can be up to 64KB, which requires the use of DPTR data pointers to access data.

I briefly mentioned the methods of defining variables in sfr, sfr16, and sbit before. Let’s take a closer look below.

sfr and sfr16 can directly define the special registers of the 51 microcontroller. The definition method is as follows:

sfr special function register name = special function register address constant;

sfr16 special function register name = special function register address constant;

We can define the P1 port of AT89C51 like this

sfr P1 = 0x90; //Define P1 I/O port, its address is 90H

The sfr key is followed by a name to be defined. You can choose it arbitrarily, but it must comply with the naming rules of identifiers. The name should have a certain meaning. For example, the P1 port can be named P1, which will make the program much easier to read. The equal sign must be followed by a constant, expressions with operators are not allowed, and the constant must be within the address range of the special function register (80H-FFH). For details, please check the related table in the appendix. sfr defines an 8-bit special function register and sfr16 is used to define a 16-bit special function register, such as the T2 timer of 8052, which can be defined as:

sfr16 T2 = 0xCC; //8052 timer 2 is defined here, the address is T2L=CCH, T2H=CDH

When using sfr16 to define a 16-bit special function register, the equal sign is followed by its low-order address, and the high-order address must be located above the physical low-order address. Note that it cannot be used for the definition of timers 0 and 1.

sbit defines bit-addressable objects. Such as accessing a bit in a special function register. In fact, this application is often used, such as accessing the second pin P1.1 in the P1 port. We can define it in the following way:

(1) sbit bit variable name = bit address

sbit P1_1 = Ox91;

This assigns the absolute address of the bit to the bit variable. Like sfr, the bit address of sbit must be between 80H-FFH.

(2) Sbit bit variable name = special function register name ^ bit position

sft P1 = 0x90;

sbit P1_1 = P1 ^ 1; //First define a special function register name and then specify the location of the bit variable name

This method can be used when the addressable bit is located in a special function register

(3) sbit bit variable name = byte address ^ bit position

sbit P1_1 = 0x90^1;

This method is actually the same as 2, except that the address of the special function register is directly expressed as a constant.

The C51 memory type provides a memory type of bdata, which refers to a bit-addressable data memory located in the bit-addressable area of ​​the microcontroller. The data that requires bit-addressability can be defined as bdata, such as:

unsigned char bdata ib; //Define ucsigned char type variable ib in the addressable area

int bdata ab[2]; //Define array ab[2] in the bit-addressable area, these are also called addressable bit objects

sbit ib7=ib^7 //Use the keyword sbit to define a bit variable to independently access one of the addressable bit objects.

sbit ab12=ab[1]^12;

The maximum value of the bit position after the operator "^" depends on the specified base address type, char0-7, int0-15, long0-31.

Let's use the circuit from the previous lesson to practice the knowledge in this lesson. We are also doing a simple marquee experiment, the project is called RunLED2. The procedure is as follows:

sfr P1 = 0x90; //No predefined file is used here,

sbit P1_0 = P1 ^ 0; //Define the special register yourself instead

sbit P1_7 = 0x90 ^ 7; //The predefined file we used before actually has this function

sbit P1_1 = 0x91; //The P1 port and P10, P11, and P17 pins are defined here respectively.

void main(void)

{

unsigned int a;

unsigned char b;

do{

for (a=0;a<50000;a++)

P1_0 = 0; //Light up P1_0

for (a=0;a<50000;a++)

P1_7 = 0; //Light up P1_7

for (b=0;b<255;b++)

{

for (a=0;a<10000;a++)

P1 = b; //Use the value of b to make marquee patterns

}

P1 = 255; //Turn off the LED on P1

for (b=0;b<255;b++)

{

for (a=0;a<10000;a++) //P1_1 flashes

P1_1 = 0;

for (a=0;a<10000;a++)

P1_1 = 1;

}

}while(1);

}


Reference address:Definition and application methods of variables in 51 microcontrollers

Previous article:Design of data acquisition system based on ADmC812 micro-conversion chip and DSP chip TMS320F206
Next article:Detailed explanation of various instructions of microcontroller

Recommended ReadingLatest update time:2024-11-16 09:40

Disassembly confirms iPhone 11 Pro Max has only 4GB of RAM
There were two previous reports about the actual memory size of the iPhone 11 Pro and iPhone 11 Pro Max. The first was the 4GB memory identified by the benchmark software, and the second was the rumored 6GB, which was also written on T-Mobile's homepage. A few days ago, Xcode's iPhone 11 Pro simulator also gave 4GB of
[Mobile phone portable]
Disassembly confirms iPhone 11 Pro Max has only 4GB of RAM
Montage Technology's first-generation DDR5 memory interface and module supporting chips have achieved mass production
On October 29, Montage Technology announced that its first-generation DDR5 memory interface and module supporting chips have been successfully mass-produced. This series of chips is an important component of DDR5 memory modules, including registered clock driver (RCD), data buffer (DB), serial detection hub (SPD Hub),
[Mobile phone portable]
ROM combines storage island encryption technology with Taisilicon MCU chip security protection solution
At present, the industry generally uses read-only memory images (ROMs) with dynamic passwords (OTPs) to develop some boot upgrade code logics with high security requirements. This solution will be burned in once during tape-out, which can prevent users from making changes to the MCU's startup upgrade logic, thereby hi
[Mobile phone portable]
ROM combines storage island encryption technology with Taisilicon MCU chip security protection solution
STM32 memory usage and allocation
Heap space and stack space configuration Stack Size, generally 0X400 is enough for small projects, and we set 0X1000 for comprehensive experiments, so there is no need to set it too large by default. The value of Stack_Size is calculated according to the maximum size of local variables in your program.  Heap Size, i
[Microcontroller]
TDK launches enhanced embedded motor controller with increased memory, power and reliability
Embedded Motor Controller TDK launches enhanced embedded motor controller with increased memory, power and reliability Delivers 4 x 1 A peak current for driving brushless DC (BLDC), brushed DC (BDC), and stepper motors Available in 4 KB SRAM, 2 KB EEPROM (32 KB), and 64 KB
[Industrial Control]
TDK launches enhanced embedded motor controller with increased memory, power and reliability
Embedded memory reading and writing methods, as well as knowledge points on mandatory type conversion
Read by function flash, type 0 represents 128-byte storage area, 1 represents 64K storage area (512 bytes/sector) void InterFlashRead(unsigned short addr, unsigned char nLength, unsigned char* dat)  { unsigned char i; EA = 0; PSCTL=0x04; FLSCL = 0x00; for(i=0; i { *(dat+i) = *(unsigned char code*)(addr+i); } PSCTL =
[Microcontroller]
Although South Korea has a high global market share in memory, only a small portion of it is exported to Japan.
The trade dispute between South Korea and Japan has been going on for a month. South Korea has also taken a tough stance on imposing sanctions on Japan. Recently, there have been rumors that South Korea might use memory as a weapon to sanction Japan, but the South Korean government later denied it, saying it would not
[Embedded]
Processor Architecture (VI) armv4v5v6 Architecture Reference Manual (2) Memory and System Architecture
Memory and system architecture (part B) Introduction to memory and system architecture ARMv6 was the first architecture variant to standardize the memory model and many system-level features. It is the first architectural variant to require a system control coprocessor and a consistent level of system-level har
[Microcontroller]
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号