Tuesday, December 21, 2010

Weak current College】 【MCU c language tutorial-lesson four: data type】.

<br> first to speak briefly on C language identifiers and keywords.. Identifier is used to identify the source of the name of an object, these objects can be a statement, data types, functions, variables, arrays and so on.. .C language is sensitive to the size of a high-level language words, if we want to define a timer 1 can be written as "Timer1", if the program has "TIMER1", then the two are completely different definitions of the .identifier.. Identifier by the strings, numbers and underscores etc., to note that the first character must be a letter or underscore, such as "1Timer" is wrong, there will be compile-time error.. Some build system-specific .identifier is an underscore at the beginning, it is generally not named with an underscore at the beginning of the identifier.. Identifier in the naming should be simple, clear meaning, which helps reading comprehension program.. In the C51 compiler, supports only the .first 32-bit identifier for the effective identification, general enough to be used, unless you write a book from heaven: P.. <BR> Programming language keywords are special identifiers reserved, they have a fixed name and meaning of identifiers in programming .and critical information is not allowed likewise.. KEIL uVision2 the keywords in addition to the standard ANSI C 32 51 Keywords things, according to the characteristics of SCM extends the relevant keywords.. In fact, in KEIL uVision2 text editor to write C programs, .the system can be reserved words in different colors, the default color is blue.. (Standard and extended keyword See Appendix I to Schedule 1-1 and Schedule 1-2) <BR> look at Table 4-1, the table lists .the KEIL uVision2 C51 compiler supports the data types.. In the standard C language for the basic data types char, int, short, long, float and double, but the C51 compiler and short the same int, float, and double the same as .described here is not listed.. Here take a look at their specific definition: <BR> data type <? XML: NAMESPACE PREFIX = O /> <O:P> </ O: P> length < .O: P> </ O: P> range <. O: P> </ O: P> unsigned char <O:P> </ O: P> single-byte <O:P> .</ O: P> 0 ~ 255 <O:P> </ O: P>. signed char <O:P> </ O: P> single-byte <O:P> < ./ O: P> -128 ~ +127 <O:P> </ O: P> unsigned int <O:P> <. / O: P> byte <O:P> </ O .: P> 0 ~ 65535 <O:P> </ O: P> signed int <O:P> </ O: P> byte <. O: P> </ O: P> - .32768 ~ +32767 <O:P> </ O: P> unsigned long <O:P> </ O: P> four-byte <O:P> </ O.: P> 0 .~ 4294967295 <O:P> </ O: P> signed long <O:P> </ O: P> four-byte <O:P> </ O: P> -2147483648 ~ + .2147483647 <. O: P> </ O: P> float <O:P> </ O: P> four-byte <O:P> </ O: P> ± 1.175494E-38 .~ ± 3.402823E +38 <O: .P> </ O: P> * <O:P> </ O: P> 1 ~ 3-byte <O:P> </ O: .P> object's address <O:P> </ O: P>. bit <O:P> </ O: P> bit <O:P> </ O: P> 0 or 1 < .; O: P> </ O: P> sfr <O:P> </ O: P>. single-byte <O:P> </ O: P> 0 ~ 255 <O: .P> </ O: P> sfr16 <O:P> </ O: P> byte <O:P> </ O.: P> 0 ~ 65535 <O:P> </ .O: P> sbit <O:P> </ O: P> bit <O:P> </ O: P> 0 or 1 <O:P> <. / O: P> Table .4-1 KEIL uVision2 C51 compiler supports the data type <O:P> <BR> </ O: P> <BR> 1.. char character type <BR> char type is one byte in length, .usually used to define a variable character data processing or constant.. Sub-type of unsigned char unsigned char and signed char type signed char, the default value of signed char type.. unsigned char type, in bytes, of all the bits to represent .values that can express a range of values from 0 to 255.. signed char type with the highest bit bytes bytes of data symbols, "0" indicates that positive, "1" indicates a negative number, negative by complement.. That .values can range -128 ~ +127.. unsigned char ASCII characters commonly used in treatment or for handling integer less than or equal to the number 255.. <BR> * Is the complement of the number of the original code with the same .negative complement binary number equal to its absolute value to bit, add 1.. 2.. <BR> int int int int length of two bytes, one byte used to store data.. Integer divided int signed int signed and unsigned integer unsigned .int, default is signed int type.. signed int values that range from -32768 ~ +32767, the highest bit byte to represent data symbols, "0" indicates that positive, "1" means negative.. unsigned int values that .range from 0 to 65535.. Well, first pause it, let's write a small program to see unsigned char and unsigned int for the delay of the different effects, indicating that their length is different, uh, even though it does not have significant practical .applications, here we. learn from their usage on the line.. The minimum is still a lesson with our system to experiment, but to Cadogan a resistor and LED, as shown in Figure 4-1.. Experiments using light that is used D1 ., unsigned int value of delay, that is lit with D2 values with unsigned char delay.. <BR> <BR> Figure 4-1 Lesson 4 <BR> experimental circuit as this project we TwoLED, experimental procedures are as follows .: <BR> # include <AT89X51.h> / / pre-order void main (. void) / / main function name <BR> {<BR> unsigned int a; / / define the variable a, unsigned .int type <BR> unsigned char b; / / define the variable b as unsigned char types do <BR> {/ /. do while loop formed <BR> for (a = 0; a <65535; a + .+) <BR> P1_0 = 0; / / 65535 second set P1.0 port is low, light LED <BR> P1_0 = 1;. / / set P1.0 port is high, off LEDfor (a = 0; .a <30000; a + +); / / empty loop for (b = 0; b <255; b + +) <BR> P1_1 = 0; /. / set P1.1 port 255 is low, light .LED <BR> P1_1 = 1; / / set P1.1 port is high, off LED <BR> for (a = 0; a <30000;. a + +); / / empty loop <BR> .} <BR> while (1); <BR>} compile the same programming, the power to run you can see the results.. Obviously longer than D2 D1 light lit the time.. Program loop delay time is not well established ., and is not suitable for precision delay requirements of the occasion, on this we will do after the discussion.. Must want to say here is that when defining a variable for a particular data type, the program uses the value of the variable should not .make it over the range of data types.. In this case the variable b can not be assigned out of 0 to 255 values, such as for (b = 0; b <255; b + +) to for (b = 0 .; b <256; b + +), compiler can be adopted,. However, there will be problems when running, that the value of b is always less than 256, so not out of the loop to the next one P1_1 = 1 ., resulting in an endless loop.. Similarly, should not exceed a value of 0 to 65535.. We can burn films to see the results of the experiment to run the same software simulation is to see the results.. 3.. < .BR> long long long long integer integer length is four bytes, to hold a four-byte data.. Sub-signed long signed long long int and unsigned long unsigned long, default is signed long type.. signed int values that range is - .2147483648 ~ +2147483647, the highest bit byte to represent data symbols, "0" indicates that positive, "1" means negative.. unsigned long value that ranges from 0 to 4294967295. 4.. <BR> float float float .float has a 7 in the decimal digits, is in line with IEEE-754 standard single precision floating point type data, taking four bytes.. More complex due to the structure of floating-point numbers in the later chapters do detail.. 5. ..* Pointer-type <BR> pointer type itself is a variable in the variable data stored in the address to another.. The pointer variable to hold a certain memory cell, the length of the different processors are not the same, in the .C51 in its length is generally 1 to 3 bytes.. Pointer variable also has the type of course in the future to do of course have a special one here is not to say.. 6.. scalar <BR> bit bit bit bit .scalar is an expansion of C51 compiler data type, use it to define a bit scalar, but can not define bit pointers, we can not define the median group.. Its value is a binary bit, either 0 or 1, some high-level .language similar to the type of Boolean True and False.. 7.. Special Function Register <BR> sfr sfr is a data type extension, the point with a memory unit, range from 0 to 255.. Use it within 51 MCU can .access all the special function registers.. If using sfr P1 = 0x90 This one set for the P1 port P1 register in the chip, in a later statement, we used to use the P1 = 255 (all of P1 port pin high) to operate .like the statement. special function registers.. <BR> 8.. sfr16 16 bit special function registers <BR> sfr16 occupy two memory cell, range 0 to 65535.. sfr16 and sfr as special function registers for the operation, the .difference is that it accounts for two-byte register operation, good timers T0 and T1. 9.. sbit address bits can be recorded bits are C51 <BR> sbit with an expansion in the data type, use it to access on-chip .RAM in bit addressable special function registers or addressable bit.. As previously we define <BR> sfr P1 = 0x90; / / for P1 port is bit-addressable register, so we can define <BR> sbit P1_1 = P1 ^ 1 .; / / P1_1 to P1 in P1.. 1 pin <BR> / / P1.1 same address we can use to write, such as sbit P1_1 = 0x91; <BR> program in the future so that we can use P1_1 .statement to read on the P1.1 pin. write operation.. Often, these can directly use the system to provide the preprocessed file, which has defined the names of special function registers a simple, direct reference can save a little time, I have .always used.. Of course you can also write their own definition file, do you think the name easy to remember.. On data type conversion operations and other related courses or programs in the following examples will be mentioned.. We can rewrite the data .types mentioned examples of what this lesson procedures, better understanding of the various types.. <BR>..

No comments:

Post a Comment