当前位置:网站首页>Detailed explanation of C51 common data types

Detailed explanation of C51 common data types

2022-07-19 09:36:00 timerring

Catalog

Background knowledge

Summary of data types

1、 Bit variable ——bit

2、 special function register ——sfr

3、16 Bit special function register ——sfr16

4、 Addressable bits ——sbit

5、 Pointer types

Type conversion


Background knowledge

Keil C51 Is the U.S. Keil Software company-developed 51 Series compatible with SCM C Language software development system .

(1) compiler C51: American standard optimization C Cross compiler C51 You can put C Source code Convert to relocatable Target file .

(2) Assembler A51 : Assembler A51 hold MCS-51 Assembly source code conversion Into relocatable Target file .

(3) Connect / Heavy positioner BL51:BL51 Combination by C51 and A51 The generated relocatable object file generates an absolute object file .

(4) Library manager LIB51:LIB51 Combine the target file to generate a library file that can be used by the connector .

(5) converter OH51:OH51 Convert the absolute target file to Intel HEX Executable in format .

(6) Monitoring program Monitor-51: use Monitor-51 When debugging the target board , This monitor program resides in the memory of the target board .

(7) Real-time operating system RTX-51: Real-time operating system RTX-51 It simplifies the development of complex and time sensitive software projects .

Summary of data types

   KEIL C51 The data types supported by the compiler are shown in the following table :

1、 Bit variable ——bit

bit The bit variable is C51 An extended data type of a compiler , Its value is a binary bit , No 0 Namely 1.

(1) Bit variable C51 Definition

    Bit variable C51 The general syntax format of the definition is as follows :

          Bit type identifier (bit)  Bit variable name ;

    for example :

    bit  direction; /* hold direction Defined as a bit variable */

    bit  allright ; /* hold allright Defined as a bit variable */

(2) Functions can contain types "bit" Parameters of , It can also be used as a return value .

  for example :

 bit func(bit b0, bit b1) /* Variable b0,b1 As an argument to a function */

     {    

 return (b1);     /* Variable b1 As the return value of the function */

      }

(3) Restrictions on bit variable definitions .

  A bit variable cannot be defined as a pointer , If it cannot be defined :

         bit  * bit_point.

   No digit group exists , If it cannot be defined :

         bit  b_array[ ]

2、 special function register ——sfr

sfr It is also an extended data type , The value range is 0~255. Used to access the 51 All special function registers in the single chip microcomputer . special function register C51 The general syntax format of the definition is as follows :

   sfr  sfr-name =  int  constant

among “sfr” Is the keyword that defines the statement , It must be followed by MSC-51 The register name of the real special function of MCU ,“=” It must be followed by an integer constant , Expressions with operators are not allowed , Is a special function register “sfr-name” Byte address of , The range of this constant value must be SFR Address range , be located 0x80-0xFF.

     If used :sfr P1 = 0x90 Definition P1 by P1 Port on-chip register , In the program we use P1 = 255( Yes P1 All pins of the port are set high ) Such statements to operate special function registers .

      C51 The commonly used special function registers are reg51.h or reg52.h Is defined in .

3、16 Bit special function register ——sfr16

        Same as sfr equally ,sfr16 by C51 Extended data type , It is just used to define the interior of the single chip microcomputer 16 Bit special function register , And occupy two memory units . for example :DPTR、 Timer T0 and T1.

        sfr16 Define the syntax format of the statement and 8 position SFR identical , It's just "=" The following address must be 16 position SFR Low byte address , That is, the low byte address is used as "sfr16" The definition address of .

    for example :

       sfr16  T2 = 0xCC /* Timer / Counter 2:T2 low 8 The bit address is 0CCH,T2 high 8 The bit address is 0CDH*/

4、 Addressable bits ——sbit

        sbit It's also C51 An extended data type in , It can be used to access the inside of the chip RAM Addressable bits in or addressable bits in special function registers . It has three definition formats :

The first format

        sbit  bit-name = sfr-name^int constant;

   Addressing bit symbol name bit-name( Must be MCS-51 The bit name specified in the SCM ),“=” After “sfr-name” Must be defined SFR Name ,“^” After int constant( Integral constant ) Is the address bit in the special function register “sfr-name” Tag number in , Must be 0~7 Number in range .

for example :

   sfr   PSW=0xD0 ; /* Definition PSW The register address is D0H */

   sbit  OV=PSW^2 ; /* Definition OV Position as PSW.2, The address is D2H */

   sbit  CY=PSW^7 ;/* Definition CY Position as PSW.7, The address is D7H */

Second format :

      sbit  bit-name = int constant^int constant;

“=” After int constant Is the address of the special function register where the address bit is located Byte address ,“^” After the symbol int constant Is the bit number of the addressing bit in the special function register . for example :

sbit OV=0XD0^2 ;/* Definition OV The bit address is D0H The... In bytes 2 position */

sbit CY=0XD0^7 ;/* Definition CY The bit address is D0H The... In bytes 7 position */

Third format :

        sbit  bit-name = int constant;

     “=” After int constant For addressing bits Absolute bit address . for example :

sbit  OV=0XD2 ;           /* Definition OV The bit address is D2H */

sbit  CY=0XD7 ;             /* Definition CY The bit address is D7H */

      Be careful sbit and bit difference :bit And other common variable types ( Such as int) similar , It just defines a bit ordinary variable , and sbit The defined bits must be special function registers or internal RAM Addressable bits in the area .

5、 Pointer types

        Pointer data itself is a variable , What is stored is the address pointing to another data . For the definition and standard of pointer C Language is similar , example :char * pt; Define a pointer to a character variable . Pointer variables also occupy a certain memory unit , stay C51 Its length is generally 1-3 Bytes .3 A pointer of bytes includes :1 Byte storage type and 2 Byte offset address , As shown in the following table :

About C51 The variable of , Here are two additional points :

(1) In addition to using the above data types , Programmers can also redefine data types according to their habits or hobbies , The definition format is as follows :

     typedef  Existing data types   New data types ;

  for example :

     typedef unsigned char uchar  ; Put the data type unsigned

                                  ;char use uchar Instead of .

     uchar  c   ; Define a unsigned char Data variable c

    

Be careful : There are no new data types added here , It's just another symbol for an existing data type .

(2)C Language is a strong type of language . When evaluating or calculating expressions , The data type of each variable must be consistent .

Type conversion

One 、 Use the cast character “()” Explicit conversion of data types

Two 、 The implicit conversion order is as follows :

     bit→char → int → long → float

     signed → unsigned

         If there are several different data types involved in the operation at the same time , First, implicitly convert the low-level data type to the high-level type, and then do the operation , And the operation result is high-level data type .

原网站

版权声明
本文为[timerring]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171015261675.html