Excerpt from Baidu Library
Each writing a program, always inseparable from the application of data, in learning C51 language in the process of mastering the understanding of data types is also very important. Looking at table 3-1, the table lists the data types supported by the KEIL UVision2 microcontroller C language compiler. The basic data types in standard C are char,int,short,long,float and double, whereas in the C51 compiler the int and short are the same, and float and double are the same, no description is listed here. Here's a look at their specific definitions:
Data type |
Length |
Range |
unsigned char |
Single byte |
0~255 |
Signed Char |
Single byte |
-128~+127 |
unsigned int |
Double byte |
0~65535 |
Signed int |
Double byte |
-32768~+32767 |
unsigned long |
Four bytes |
0~4294967295 |
Signed Long |
Four bytes |
-2147483648~+2147483647 |
Float |
Four bytes |
±1.175494e-38~±3.402823e+38 |
* |
bytes |
The address of the object |
Bit |
-bit |
0 or 1 |
SFr |
Single byte |
0~255 |
Sfr16 |
Double byte |
0~65535 |
Sbit |
-bit |
0 or 1 |
Table 3-1 KEIL uVision2 Single-chip microcomputer C language compiler supported data types
1. char character type
The length of a char type is a byte that is typically used to define a variable or constant that handles character data. The unsigned character type unsigned char and the signed character type signed char, the default value is signed char type. The unsigned char type uses all the bits in the byte to represent the value, and the range of values that can be expressed is 0~255. The signed char type represents the symbol for the data in bytes with the highest byte, "0" for positive numbers, "1" for negative numbers, and negative numbers in complement notation. The range of values that can be represented is -128~+127. Unsigned char is commonly used to process ASCII characters or to handle integers less than or equal to 255.
* The complement of a positive number is the same as the original code, and the complement of the negative binary is equal to its absolute value reversed by a bitwise plus 1.
2. int integral type
The int integer length is two bytes and is used to hold a double-byte data. signed int and unsigned integer number unsigned int, with the default value of signed int type. Signed int represents a numeric range of -32768~+32767, the highest-level byte representation of the data symbol, "0" for positive numbers, and "1" for negative numbers. The range of values represented by unsigned int is 0~65535.
Just stop and write a little program. unsigned char and unsigned int are used for a different effect of delay, indicating that their lengths are different and learn how to use them. Still use the last one of the smallest system to do experiments, but to add a resistor and LED, 3-1. The use of D1 in the experiment indicates that the unsigned int value delay is being used and the D2 is used to indicate that the unsigned char value delay is being used.
Figure 3-1 Section 3 of the experiment circuit to call this project twoled, the experimental procedure is as follows:
#include//Preprocessing commands
void main (void)//main function name
{
unsigned int A; Define variable A as unsigned int type
unsigned char b; Define variable B as unsigned char type
Do
{//do while composition loop
for (a=0; a<65535; a++)
P1_0 = 0; 65,535 Times set P1.0 port as low level, light LED p1_0 = 1; Set the P1.0 port to high, turn off the LED
for (a=0; a<30000; a++); Empty loop
for (b=0; b<255; b++)
p1_1 = 0; 255 times set P1.1 port as low level, light LED p1_1 = 1; Set the P1.1 port to high, turn off the LED
for (a=0; a<30000; a++); Empty loop
}
while (1);
}
Also compile and write, and you can see the results when you power up. It is obvious that the D1 light is longer than the time D2 lit.
What must be said here is that when a variable is defined as a specific data type, the variable should not be used by the program to exceed the value of the data type. If the variable B in this example cannot be assigned beyond the value of 0~255, such as for (b=0; b<255; b++) to for (b=0; b<256; b++), compile can pass, but the runtime will have a problem, that is, the value of B is always less than 256, So we can't jump out of the loop and execute the next sentence p1_1 = 1, causing a dead loop. Likewise, the value of a should not exceed 0~65535.
3. Long integer type
A long length of four bytes is used to hold a four-byte data. The symbol long Long integer signed long and the unsigned longer integer unsigned, the default value is signed long type. Signed int represents a numeric range of -2147483648~+2147483647, the highest-level byte representation of the data symbol, "0" for positive numbers, and "1" for negative numbers. Unsigned long indicates that the range of values is 0~4294967295.
4. float Float Type
Float floating-point type has 7 significant digits in decimal and is a single-precision floating-point data that conforms to the IEEE-754 standard and occupies four bytes. Because the structure of floating-point number is more complex in later chapters, we will discuss in detail.
5. * Pointer-type pointer is itself a variable in which the address that points to another data is stored in this variable. This pointer variable to occupy a certain memory unit, the different processor length is not the same, in c51 its length is generally 1~
3 bytes. Pointer variables also have a type, in a later course there is a special lesson to be explored, here is not much to say.
6. Bit bit scalar
Bit scalar is an extended data type of the C51 compiler that can be used to define a bit scalar, but not to define bit pointers or to define bit arrays. Its value is a bits, not 0 is 1, similar to some high-level languages in the Boolean type of True and False.
7. SFR Special Function Register
SFR is also an extended data type that points to a memory unit with a range of 0~255. It can be used to access all the special function registers inside the 51 single chip microcomputer. If using SFR P1 = 0x90 This sentence P1 is the P1 port on the chip register, in the following statement to use P1 = 255 (all pins on the P1 port) and other statements to operate the special function register.
8. Sfr16 16-bit special function register
The sfr16 occupies two memory units with a range of 0~65535. Sfr16 and SFR are used to operate special function registers, not the same as it is used to operate a register of two bytes, such as Timers T0 and T1.
9. Sbit Addressable Location
Sbit is also a kind of extended data type in MCU C language, which can access addressable bits in RAM inside the chip or addressable bits in special function registers. As previously defined SFR P1 = 0x90; Because the register of the P1 port is addressable, it is possible to define sbit p1_1 = p1^1; P1_1 is the P1.1 pin in the P1
Also we can use P1.1 address to write, such as sbit p1_1 = 0x91; This allows the P1.1 pin to be read and written using P1_1 in later program statements. Usually these can directly use the system supply of pre-processing files, which have been defined in each special function register simple name, direct reference can save a little time, I have been using. Of course, you can also write your own definition files, using the name you think is good to remember.
Data types for C51