Study on Keil C51's extension of standard ANSI C

Publisher:YudieLatest update time:2011-01-25 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

One of the keys to learning C51 is to deeply understand and apply C51's extensions to standard ANSI C. This is because most of the extensions are directly targeted at the 8051 series CPU hardware. There are roughly 8 categories:

  1. 8051 memory types and memory areas
  2. Storage Mode
  3. Memory Type Declaration
  4. Variable type declaration
  5. Bit variables and bit addressing
  6. Special Function Registers (SFRs)
  7. C51 pointer
  8. Function Properties

The specific instructions are as follows (8031 is the default CPU).

Section 1 Keil C51 Extended Keywords
C51 V4.0 version has the following extended keywords (a total of 19):
_at_ idata sfr16 alien interrupt small
bdata large _task_ Code bit pdata
using reentrant xdata compact sbit data sfr

Section 2 Memory Areas:
1. Program Area:
The code indicates that there can be up to 64kBytes of program memory.

2. Internal Data Memory:
The internal data memory can be described by the following keywords:
data: directly addressable area, which is the lower 128 bytes of the internal RAM 00H~7FH
idata: indirectly addressable area, including the entire internal RAM area 00H~FFH
bdata: bit addressable area, 20H~2FH

3. External Data Memory
The external RAM can be identified by the following keywords depending on usage:
xdata: can specify up to 64KB of external direct addressing area, address range 0000H ~ 0FFFFH
pdata: can access 1 page (25bBytes) of external RAM, mainly used in compact model.

4. Speciac Function Register Memory
8051 provides 128Bytes of SFR addressing area, which can be bit-addressable, byte-addressable or word-addressable to control timers, counters, serial ports, I/O and other components. It can be described by the following keywords:
sfr: byte addressing, such as sfr P0=0x80; the PO port address is 80H, and the constant between H and FFH after "=".
sfr16: word addressing, such as sfr16 T2=0xcc; specifies the Timer2 port address T2L=0xcc T2H=0xCD
sbit: bit addressing, such as sbit EA=0xAF; specifies the 0xAF bit as EA, that is, interrupt enable

There may also be the following definition methods:
sbit 0V=PSW^2; (defining 0V as the second bit of PSW)
sbit 0V=0XDO^2; (same as above)
or bit 0V-=0xD2 (same as above).

Section 3 Storage Modes
The storage mode determines the default storage area for variables, function parameters, etc. that do not have an explicitly specified storage type. There are three types:

  1. Small mode
    All default variable parameters are loaded into the internal RAM. The advantage is fast access speed, but the disadvantage is limited space, which is only suitable for small programs.
  2. Compact mode
    All default variables are located in one page (256Bytes) of the external RAM area. The specific page can be specified by the P2 port, described in the STARTUP.A51 file, or specified by pdata. The advantage is that the space is more spacious than Small, the speed is slower than Small, and faster than Large. It is an intermediate state.
  3. Large mode
    All default variables can be placed in up to 64KB of external RAM. The advantage is that the space is large and many variables can be stored. The disadvantage is that the speed is slow.

Tip: The storage mode is selected in the C51 compiler options.

Section 4 Storage Type Declaration
The storage type of a variable or parameter can be specified by the default type specified by the storage mode, or can be directly specified by a keyword declaration. Each type is described by: code, data, idata, xdata, pdata, for example:
data uar1
char code array[ ] = "hello!";
unsigned char xdata arr[10][4][4];

Section 5 Variables or Data Types

C51 provides the following extended data types:
bit: bit variable value is 0 or 1
sbit: bit variable defined from byte 0 or 1
sfr: byte address 0 to 255
sfr16: word address 0 to 65535
Other data types such as char, enum, short, int, long, float, etc. are the same as ANSI C.

Section 6 Bit Variables and Declarations
1. Bit Variables
Bit variables can be used as variable types, function declarations, function return values, etc., and are stored in internal RAM 20H to 2FH.
Note:
(1) Functions declared with #pragma dISAble and functions specified with "usign" cannot return bit values.
(2) A bit variable cannot be declared as a pointer, such as bit *ptr; it is wrong
(3) There cannot be a bit array, such as: bit arr[5]; it is wrong.

2. The bit addressable area 20H-2FH
can be defined as follows:
int bdata i;
char bdata arr[3],
then:
sbit bito=in0; sbit bit15=I^15;
sbit arr07=arr[0]^7; sbit arr15=arr[i]^7;

Section 7 Keil C51 Pointer
C51 supports Generic Pointer and Memory_Specific Pointer.

1. General pointers
The declaration and use of general pointers are the same as those in standard C, but the storage type of the pointer can also be specified. For example:
long * state; is a pointer to a long integer, and state itself is stored according to the storage mode.
char * xdata ptr; ptr is a pointer to char data, and ptr itself is placed in the external RAM area. The data pointed to by the above long, char and other pointers can be stored in any memory.
The general pointer itself is stored in 3 bytes, which are the memory type, high offset, and low offset.

2. Memory pointer
When describing a memory-based pointer, the storage type is specified, for example:
char data * str; str points to the char data in the data area
int xdata * pow; pow points to the int integer in the external RAM.
When storing this type of pointer, only one byte or two bytes are needed, because only the offset needs to be stored.

3. Pointer conversion
That is, the pointer is converted between the above two types:

  • When a memory-based pointer is passed as an argument to a function that expects a general pointer, the pointer is automatically converted.
  • If you do not declare the prototype of an external function, the memory-based pointer will be automatically converted to a general pointer, causing an error. Therefore, please declare all function prototypes using "#include".
  • The pointer type can be forcibly changed.

Section 8 Keil C51 Function
C51 function declaration extends ANSI C, including:

1. Interrupt function declaration:
The interrupt declaration method is as follows:

void serial_ISR () interrupt 4 [using 1]
{
/* ISR */
}

To improve the fault tolerance of the code, an iret statement is generated at the unused interrupt entry to define the unused interrupt.

/* define not used interrupt, so generate IRET in their entrance */
void extern0_ISR() interrupt 0{} /* not used */
void timer0_ISR () interrupt 1{} /* not used */
void extern1_ISR() interrupt 2{ } /* not used */
void timer1_ISR () interrupt 3{} /* not used */
void serial_ISR () interrupt 4{} /* not used */

2. General storage work area

3. Select the general storage work area and declare it by using x, as shown in the above example.

4. Specify the storage mode
Describe it by small, compact and large, for example:
void fun1(void) small { }
Tip: All internal variables of the function described by small use internal RAM. The key and frequently time-consuming places can be declared in this way to improve the running speed.

5. #pragma dISAble
is declared before the function and is only valid for one function. The function cannot be interrupted during the call.

6. Recursive or reentrant function specification
Functions that can be called in both the main program and the interrupt are prone to problems. Because 51 is different from PC, PC uses the stack to pass parameters, and all internal variables except static variables are in the stack; while 51 generally uses registers to pass parameters, and internal variables are generally in RAM. When the function is reentered, the data of the last call will be destroyed. You can use the following two methods to solve this problem: a
. Use the aforementioned "#pragma dISAble" declaration before the corresponding function, that is, only one of the main program or interrupt is allowed to call the function; b. Describe the function as reentrant. As follows:
void func(param...) reentrant;
After KeilC51 is compiled, a reentrant variable stack will be generated, and then the method of passing variables through the stack can be simulated.
Since reentrant functions are generally called by the main program and interrupts, interrupts usually use a different R register group from the main program.
In addition, for reentrant functions, add the switch "#pragma noaregs" in front of the corresponding function to prohibit the compiler from using absolute register addressing, so that code that does not depend on the register group can be generated.

7. Specify PL/M-51 functions
Specified by alien.

Reference address:Study on Keil C51's extension of standard ANSI C

Previous article:Design of signal processing circuit for RFID reader based on C8051F131
Next article:51 MCU RTL8019AS network card driver

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号