Sunday, March 27, 2011

【School】 SCM weak c language tutorial --- Lesson: variable】.

<br> class variables mentioned in the program execution is a value to the process of changing its volume. .To use variables in the program must first use the identifier as a variable name, and pointed out that the type of data used and the storage mode, so that the variable distribution system to compile the corresponding storage space. .The format of a variable defined as follows: <BR> [store type] data type [memory type] variable format in the definition watch <BR> data types and variables in addition to watches is necessary, the other is optional. .There are four storage types: Auto (auto), external (extern), static (static) and registers (register), the default type to Automatic (auto). .The specific meaning of these storage types and usage, will be the seventh class "variable stores" in the further study. .<BR> And here is the data type and the fourth lesson we learned the name of the definition of data types is the same. .Illustrates the data type of a variable can also select the memory that the variable type. .Memory type is specified in the variable description of the hardware system in the C51 used in the storage area, and accurate positioning at compile time. .Table 6-1 is KEIL uVision2 can identify other memory types. .Note that only in the low AT89C51 chip RAM 128, is located in the high-80H to FFH 128-bit chip in the 52 to be useful, and the special register address and overlap. .Special register (SFR) of the address table see Appendix II list of special function registers AT89C51 memory type <? XML: NAMESPACE PREFIX = O /> <O:P> </ O: P> Description <O:P> </ O: .P> data <O:P> </ O: P> direct access to internal data memory (128 bytes), access to the fastest <O:P> </ O: P> bdata <O:P> </ O: .P> bit-addressable internal data memory (16 bytes), which allows mixed bit and byte access <O:P> </ O: P> idata <O:P> </ O: P> Indirect access to internal data memory .(256 bytes), which allows access to all internal address <O:P> </ O: P> pdata <O:P> </ O: P> page to access external data memory (256 bytes), with the MOVX @ Ri .instruction access <O:P> </ O: P> xdata <O:P> </ O: P> external data memory (64KB), with the MOVX @ DPTR instruction accesses <O:P> </ O: P> code .<O:P> </ O: P> program memory (64KB), with the MOVC @ A + DPTR instruction accesses <O:P> </ O: P> <BR> Table 6-1 Memory types of memory types if omitted, .system will compile the model by SMALL, COMPACT, or LARGE memory type required to specify the default storage area variables. .No matter what storage mode can declare variables in any storage area 8051, but the most commonly used commands, such as loop counters and queues on the internal data area index can significantly improve system performance. .Also to point out that the variable storage types and storage types are completely unrelated. .<BR> SMALL memory model variables and all functions on the 8051 system of local data segment of the internal data storage area that makes access to data very fast, but the SMALL memory model of the address space is limited. .Writing small applications, data variables and data on the internal data memory is good because the access speed, but in the larger application data area's best to store a small variable, data or frequently used variables .(such as cycle counting, data index), and large data storage area is placed in the other. .<BR> COMPACT storage mode functions and procedures of all variables and local data segment positioning system in the 8051 external data store. .External data memory can have up to 256 bytes (a), in this mode, external data storage area of the short address with @ R0/R1. .<BR> LARGE storage mode all functions and procedures of the variables and local data segments are located in the 8051 system, the external data area of external data area can have up to 64KB, which requires access to data with the data pointer DPTR. .<BR> Mentioned earlier briefly mentioned sfr, sfr16, sbit variables defined in the method, let's take a closer look again. .<BR> Sfr and sfr16 microcontroller 51 can directly define the special register is defined as follows: <BR> sfr special function register name = special function register address constant; <BR> sfr16 special function register name = SFR address constant .; <BR> AT89C51 we can define the P1 port <BR> sfr P1 = 0x90; / / defined in P1 I / O port, the address 90H <BR> sfr be followed by a key name to be defined, can be arbitrarily selected, .But to meet the naming rules for identifiers, the name has some meaning, such as the best port P1 P1 can name, this program will become a lot of good reading. .After the equal sign must be a constant, does not allow expressions with operators, and the constant need to address the special function registers within the scope of (80H-FFH), specifically to see the related tables in the appendix. .sfr is to define the 8-bit special function register is used to define the sfr16 16-bit special function registers, such as the 8052's T2 timer, can be defined as: <BR> sfr16 T2 = 0xCC; / / defined here 8052 Timer 2 .address T2L = CCH, T2H = CDH <BR> sfr16 defined by the 16-bit special function register, after the equal sign is its low address, high address low address must be located above the physical. .Note that Timer 0 and 1 can not be used for the definition. .<BR> Sbit defined bit-addressable objects. .Such as access to certain special function registers in. .In fact, this application is the regular use of the mouth, such as P1 to access the first two pins P1.1. .We can according to the following method to define: <BR> (1) sbit bit variable name = bit address <BR> sbit P1_1 = Ox91; <BR> this is the bit absolute address assigned to bit variables. .Sfr sbit with the same bit address must be located between 80H-FFH. .<BR> (2) Sbit bit variable name = name ^ bit special function register location <BR> sft P1 = 0x90; <BR> sbit P1_1 = P1 ^ 1; / / first define a special function register names and then specify the name of the variable bit .location addressable bit <BR> When special function register in this way can be used when <BR> (3) sbit bit variable name = byte address ^ bit position <BR> sbit P1_1 = 0x90 ^ 1; < .BR> In fact, this method and 2 are the same, only the address of the special function register directly with the constant said. .<BR> C51 memory type is provided in a bdata memory type, this is the bit-addressable data memory, in the MCU can be bit addressable area, you can address the requirements can be recorded in the data bit is defined as bdata, .such as: <BR> unsigned char bdata ib; / / recorded bits in the address area can be defined ucsigned char type variable ib <BR> int bdata ab [2]; / / define the array of bit-addressable area ab [2], .These are also referred to as addressable digital object <BR> sbit ib7 = ib ^ 7 / / sbit definitions with the keyword bit variable bit-addressable objects independent access to one of <BR> sbit ab12 = ab [1] ^ .12; <BR> operator "^" followed by the bit position of the maximum depends on the type of the specified base address, char0-7, int0-15, long0-31. .<BR> Lesson we use the circuit below to practice the knowledge about the lesson. .Also do some simple experiments Marquee, the project called RunLED2. .Procedures are as follows: sfr P1 = 0x90; / / There is no predefined file, <BR> sbit P1_0 = P1 ^ 0; / / but their definition of a special register <BR> sbit P1_7 = 0x90 ^ 7; / / before we use .predefined document was in fact the role of <BR> sbit P1_1 = 0x91; / / P1 ports are defined here, and P10, P11, P17 pin void main (void) <BR> {<BR> unsigned int a; <BR> .unsigned char b; <BR> do {<BR> for (a = 0; a <50000; a + +) <BR> P1_0 = 0; / / lit P1_0 <BR> for (a = 0; a <50000; a + + .) <BR> P1_7 = 0; / / lit P1_7 <BR> for (b = 0; b <255; b + +) <BR> {<BR> for (a = 0; a <10000; a + +) <BR> .P1 = b; / / Marquee with b values do tricks <BR>} <BR> P1 = 255; / / off on the P1 LED <BR> for (b = 0; b <255; b + +) < .BR> {<BR> for (a = 0; a <10000; a + +) / / P1_1 flashing <BR> P1_1 = 0; <BR> for (a = 0; a <10000; a + +) <BR> P1_1 = 1 .; <BR>} <BR>} while (1); <BR>}.

No comments:

Post a Comment