Monday, December 27, 2010

【School】 SCM weak c language tutorial --- Lesson: operators and expressions (3)】.

<br> bit assembly operator <BR> friends learned that the processing power of the bit assembly is very strong, but the C language can also be carried by operand bit of the operation, so that the C language also has some of the .the ability to operate the hardware directly. .The role of bitwise bitwise operations on the variables, but does not change the value of the variable involved in computing. .If the changes required by the bit value of the variable, will have to use the appropriate assignment. .There is a bit operator can not be used to manipulate floating point data. .C51 total of 6-bit operator. .<BR>-Bit computing in general expression is as follows: <BR> variable 1 variable 2 <BR> operator bitwise operators have precedence, in descending order are :"~"( to bit) → "< .; <"(left) → ">>"( right) →" & "(bitwise and) →" ^ "(bitwise XOR) →" | "(bitwise or) <BR .> Table 7-1 is a logical truth table, X indicates that the variable 1, Y indicates that the variable 2 <BR> <BR> XY ~ X ~ YX & YX | YX ^ Y0011000011001110010111100110 <BR> Table 7-1 Bitwise .inversion, and, or and exclusive or truth table logic of the experimental use of previously established board, let's do an experiment to test whether it really does not change the bit operation in the value of the variable, the expression of both forms of learning-bit computing. .The procedure is simple to do with the P1 port operator variables, P1.0-P1.7 P1 variables corresponding to the lowest level to the highest level, by connecting the LED in the P1 port we can visually see whether each bit computing has changed after the variable .or how to change. .Procedures are as follows: <BR> # include </ AT89X51.H> <BR> void main (void) <BR> {<BR> unsigned int a; <BR> unsigned int b; <BR> unsigned char temp; / / temporary .variable <BR> P1 = 0xAA; // lit D1, D3, D5, D7 P1 port binary 10101010, 0 lit LED <BR> for (a = 0; a <1000; a + +) <BR> .for (b = 0; b <1000; b + +); / / Delay <BR> temp = P1 & 0x7; / / simply write P1 | 0x7 is meaningless, because no variables are affected, will not be compiled .<BR> / / implementation of P1 | 0x7 after the results into temp, then change the temp, but the P1 will not be affected. .<BR> / / When LED does not change, still the D1, D3, D5, D7 bright <BR> for (a = 0; a <1000; a + +) <BR> for (b = 0; b <1000; b + + .); / / delay <BR> P1 = 0xFF; / / off LED <BR> for (a = 0; a <1000; a + +) <BR> for (b = 0; b <1000; b + +); / / .Delay <BR> P1 = 0xAA; / / lit D1, D3, D5, D7 P1 port binary 10101010, 0 lit LED <BR> for (a = 0; a <1000; a + +) <BR .> for (b = 0; b <1000; b + +); / / Delay <BR> P1 = P1 & 0x7; / / then LED will become only the D2 off <BR> / / because before P1 = 0xAA = .10101010 <BR> / / with 0x7-bit and 0x7 = 00000111 <BR> / / results into P1 P1 = 00000010 / / bit is O, light LED, the circuit to see Lesson <BR> for (a = 0; a < .; 1000; a + +) <BR> for (b = 0; b <1000; b + +); / / Delay <BR> P1 = 0xFF; / / off LED <BR> while (1); <BR> / / Members .According to the above procedure can be done bit or, left, inverted and so on. .<BR>} <BR> <BR> <BR> Compound assignment operators compound assignment operator is the assignment operator "=" in front with other operators. .The following is a C language compound assignment operator: <BR> + = addition assignment>> = right shift assignment <BR> -= subtraction assignment & = logical and assignment <BR> *= multiplication assignment | = logical .or assignment <BR> / = division assignment ^ = XOR assignment <BR>% = modulo assignment -= logic of non-assignment <BR> <<= left shift assignment <BR> <BR> general composite operation .form: <BR> variable compound assignment operator is the meaning of the expression <BR> <BR> variables and expressions to operators before the operation required, and then computing the results of operations assigned to the variables involved. .In fact, this is the C language, a simplified procedure for a method of computing any two items can be used to simplify complex expression assignment operator. .For example: <BR> a + = 56 is equivalent to a = a +56 <BR> y / = x +9 equivalent to y = y / (x +9) <BR> obviously using the compound assignment operator will reduce the program .readability, but it is able to make the code simple, and can improve the efficiency of compilation. .For the beginner C programming language, best friends in the understanding and according to their customary ways of expression, to use the program, not the blind pursuit of short code. .<BR> <BR> Comma operator <BR> If you have programming experience, then the role of the comma will not be a stranger. .If the VB in "Dim a, b, c" the comma is to define multiple variables for the same type of variables in C, too, such as "int a, b, c", these examples of comma-separated expressions .use. .However, the comma in the C language or a special operator, that is, the comma operator, you can use it to connect two or more expressions to form a comma expression. .The general form of comma expression: <BR> expression 1, expression 2, expression 3 ... ... so the expression n <BR> comma operator expression composed of the program is running, is calculated from left to right .the value of each expression, and the whole composition with a comma operator is equal to the right of the value of the expression value of the expression is "the expression n" value. .In practical applications, in most cases, the use of a comma expression were obtained were only for the purpose ofexpressions of value, and not necessarily to get and use of the comma expression. .Note that there are not appear anywhere in the program the comma, the comma can be considered as operator. .Such as function parameters, the definition of the same type variable is only used in the interval of a comma instead of a comma operator is used. .<BR> <BR> <BR> Conditional operator C language, we have said above, there is a ternary operator, it is "?:" Conditional operator, it requires three operands. .It can be connected to three expressions constitute a conditional expression. .The general form of the conditional expression is as follows: <BR> logical expression? Expressions 1: expression 2 <BR> the role of the conditional operator is simply a logical expression based on the value of options using the expression values. .When the logical expression evaluates to true (non 0 value), the entire expression is the value of expression 1; when the logical expression is false (value 0), the expression of the entire expression is .Type 2 value. .Note that the conditional expression in the type of logical expression with the expression of type 1 and 2 expression is not the same. .The following is an example of a logical expression. .<BR> <BR> If a = 1, b = 2 ab then we ask is to take the smaller of two numbers into the min value of the variable, you might write: <BR> if (a min = .a; <BR> else <BR> min = b; / / This means that when a section of the conditional operator used to form the conditional expression becomes simple and clear: <BR> min = (a result it is obvious .and meaning, and above all a process is the same, but the code is much less than on a program, compile the efficiency is relatively high, but with different and complex assignment expression is the readability of the relative disadvantage of poor efficiency. In practice, .applications to be used according to their own use, for myself I like to use it more easy to read manner and with appropriate comments, which can help debug and write, but also facilitate future changes to read and write. pointer and address .operator <BR> we learned in the fourth data types, pointer types studied, know that it is another data point to the address stored in the variable type. C language pointer is a very important concept, but also to learn C .a difficult language. For the pointer will be the ninth class to do a detailed explanation. Here, we first take a look at the two C language provides a pointer and address specifically for operators: <BR> * check the contents of .<BR> & The address <BR> take the general form of the content and address are: <BR> variable = * pointer to a pointer variable = & variable <BR> <BR> <BR> target variable is a pointer to take the contents of operation .variable points to the left of the target variable value is assigned to the variable; The address of operation is the address of the target variable is the variable assigned to the left. It should be noted are: a pointer variable can only contain the address (that is, pointer-based data), the general .Do not the case of non-pointer types of data assigned to a pointer variable. <BR> look at an example below, and with a simple to understand charts and a pointer to an instance of the use and meaning. <BR> <BR> with two unsigned int .ABC Office CBA stored in the variable 0x0028, 0x002A in <? XML: NAMESPACE PREFIX = O /> <O:P> </ O: P> There is also a pointer variable portA stored in 0x002C in <O:P> </ O: .P> Then we write a program to see * this, & the operation result <O:P> </ O: P> <O:P> </ O: P> unsigned int data ABC _at_ 0x0028; <O: .P> </ O: P> unsigned int data CBA _at_ 0x002A; <O:P> </ O: P> unsigned int data * Port _at_ 0x002C; <O:P> </ O: P> <O:P> .</ O: P> # include </AT89X51.H> <O:P> </ O: P> # include <STDIO.H> </ STDIO.H> <O:P> </ O: P> < .O: P> </ O: P> void main (void) <O:P> </ O: P> {<O:P> </ O: P> SCON = 0x50; / / serial port mode 1, to allow the reception .<O:P> </ O: P> TMOD = 0x20; / / Timer 1 Timer mode 2 <O:P> </ O: P> TH1 = 0xE8; / / 11.0592MHz 1200 baud <O: P .> </ O: P> TL1 = 0xE8; <O:P> </ O: P> TI = 1; <O:P> </ O: P> TR1 = 1; / / start the timer <O: P .> </ O: P> <O:P> </ O: P> ABC = 10; / / set initial <O:P> </ O: P> CBA = 20; <O:P> </ O .: P> <O:P> </ O: P> Port = &CBA; / / get the addresses into the pointer variable CBA Port <O:P> </ O: P> * Port = 100; / / Change .Port pointer variable contents of the address pointed to <O:P> </ O: P> <O:P> </ O: P> printf ("1: CBA =% d \ n", CBA); / / display .At this point the value of CBA <O:P> </ O: P> <O:P> </ O: P> Port = &ABC; / / get the addresses into the pointer variable ABC Port <O:P> < ./ O: P> CBA = * Port; / / the current contents of the address referred Port assigned to the variable CBA <O:P> </ O: P> <O:P> </ O: P> printf (" .2: CBA =% d \ n ", CBA); / / Display the value of CBA at this time <O:P> </ O: P> printf (" ABC =% d \ n ", ABC); / / display ABC .the value of the <O:P> </ O: P>} <O:P> </ O: P> program initially <O:P> </ O: P> value <O:P> </ O: .P> Address <O:P> </ O: P> Description <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x002DH <O:P> </ O: P> .<O:P> </ O: P> 0x00 <O:P> </ O: P> 0x002CH <O:P> </ O: P> <O:P> </ O: P> 0x00 <O: .P> </ O: P> 0x002BH <O:P> </ O: P> <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x002AH <O:P> < ./ O: P> <O:P> </ O: P> 0x0A <O:P> </ O: P> 0x0029H <O:P> </ O: P> <O:P> </ O: P .> 0x00 <O:P> </ O: P> 0x0028H <O:P> </ O: P> <O:P> </ O: P> <O:P> </ O: P> implementation of ABC = .10; referred to the ABC to write the address 0x28H 10 (0xA), because ABC is the int type to take 0x28H and 0x29H two bytes of memory, low byte into the high address, so 0x28H put 0x00 in ., 0x29H in place 0x0A <O:P> </ O: P> value <O:P> </ O: P> Address <O:P> </ O: P> Description <O:P> </ O .: P> 0x00 <O:P> </ O: P> 0x002DH <O:P> </ O: P> <O:P> </ O: P> 0x00 <O:P> </ O: P> .0x002CH <O:P> </ O: P> <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x002BH <O:P> </ O: P> <O: .P> </ O: P> 0x00 <O:P> </ O: P> 0x002AH <O:P> </ O: P> <O:P> </ O: P> 0x0A <O:P> < ./ O: P> 0x0029H <O:P> </ O: P> ABC for the int type occupy two bytes <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x0028H < .O: P> </ O: P> <O:P> </ O: P> <O:P> </ O: P> implementation of CBA = 20; principles and on an as <O:P> </ O .: P> value <O:P> </ O: P> Address <O:P> </ O: P> Description <O:P> </ O: P> 0x00 <O:P> </ O: P .> 0x002DH <O:P> </ O: P> <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x002CH <O:P> </ O: P> <O .: P> </ O: P> 0x14 <O:P> </ O: P> 0x002BH <O:P> </ O: P> CBA takes two bytes for the int type <O:P> </ O: .P> 0x00 <O:P> </ O: P> 0x002AH <O:P> </ O: P> <O:P> </ O: P> 0x0A <O:P> </ O: P> 0x0029H .<O:P> </ O: P> ABC for the int type occupy two bytes <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x0028H <O:P> </ .O: P> <O:P> </ O: P> <O:P> </ O: P> CBA Executive Port = &CBA; take the first address pointer variables into the Port <O:P> </ .O: P> value <O:P> </ O: P> Address <O:P> </ O: P> Description <O:P> </ O: P> 0x00 <O:P> </ O: .P> 0x002DH <O:P> </ O: P> <O:P> </ O: P> 0x2A <O:P> </ O: P> 0x002CH <O:P> </ O: P> CBA .the first address into the Port <O:P> </ O: P> 0x14 <O:P> </ O: P> 0x002BH <O:P> </ O: P> <O:P> </ O: .P> 0x00 <O:P> </ O: P> 0x002AH <O:P> </ O: P> <O:P> </ O: P> 0x0A <O:P> </ O: P> 0x0029H .<O:P> </ O: P> <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x0028H <O:P> </ O: P> <O: P .> </ O: P> <O:P> </ O: P> * Port = 100; change the pointer variable pointed to by the Port address the contents of the <O:P> </ O: P> value <O:P> .</ O: P> Address <O:P> </ O: P> Description <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x002DH <O:P> </ .O: P> <O:P> </ O: P> 0x2A <O:P> </ O: P> 0x002CH <O:P> </ O: P> <O:P> </ O: P> .0x64 <O:P> </ O: P> 0x002BH <O:P> </ O: P> Port points to the CBA where the address 2AH <O:P> </ O: P> 0x00 <O:P> </ .O: P> 0x002AH <O:P> </ O: P> and stored in 100 <O:P> </ O: P> 0x0A <O:P> </ O: P> 0x0029H <O:P> </ .O: P> <O:P> </ O: P> 0x00 <O:P> </ O: P> 0x0028H <O:P> </ O: P> <O:P> </ O: P> .the other reason is the same statement, we can use Keil's step and open the Memory Viewer to look, making it easier to understand. Figure 7-6 Figure 7-7 memory view window in the final outcome of the serial debug window .<BR> sizeof operator <BR> seems this is indeed a strange operator, a bit like a function, but is not. you should see the size and the size of guessed that, right? Yes, sizeof is used to find .data types, variables, expressions, or an operator the number of bytes, but it does not like the "=" operator like that before the implementation of the program to calculate the results, it is directly at compile time to produce results. It .The syntax is as follows: <BR> sizeof (data type) <BR> sizeof (expression) <BR> listen Here are two applications, the program we can try to write about. printf ("char is how many bytes?% bd .byte \ n ", sizeof (char)); <BR> printf (" long how many bytes?% bd bytes \ n ", sizeof (long)); result is: <BR> char is the number of words .Day? 1 byte <BR> long is how many bytes? 4 bytes <BR> <BR> cast operator <BR> I wonder if you have prepared yourself to try some of the procedures, from which have encountered some .problem? Beginners I encountered such a problem: the number of two different data types will appear in each assignment wrong values. as shown in a small program: <BR> void main (void) <BR> {< .BR> unsigned char a; <BR> unsigned int b; b = 100 * 4; <BR> a = b; <BR> while (1); <BR>} <BR> this small program and there is no real .significance of application, if you are careful friend will find a value is not equal to 100 * 4. Yes a and b one is a char type is int type, char from the previous study shows that only a byte value .the maximum can only be 255. but why not go wrong when compiling it? first look at the operation of this program: <BR> <BR> Figure 7-8 the operation of the applet b = 100 * 4 to be able to tell b = .0x190, then we can see a value in the Watches, watches the window for hours at a simple 5 studied in this window to view pages Locals run in the value of a variable, you can also watch page, enter the desired view .the value of the variable name to view it. practice is based on the figure 1 watch # 1 (or watch # 2), and then move the cursor to the figure 2 Press F2 key, so that you can enter a variable name. Here we .can see the value of a 0x90, which is b, the lower 8 bits. This is because the implementation of the implicit data type conversion. Implicit conversion program is compiled automatically by the compiler to process completed. it is necessary to .Learn the rules of implicit conversion: <BR> 1. variable assignment occurs when the implicit conversion, "=" No data type of expression on the right into the left of the variable data type. just like in the example above, assigned to the INT .CHAR char, CHAR will get the low 8-bit INT. If the floating-point number assigned to the integer variable, the fractional part will be lost. <BR> 2. all the char-type operands convert the int type. <BR> .3. the two have different data types of operands connected by operators, implicit conversion is carried out in the following order: If one operand is a float type, the other operand will be converted into a float type; If one operand .for the long type, the other into long; if one operand is unsigned type, the other operations will be converted to unsigned type. <BR> from the above rules can probably know that there are several data types that can be hidden .conversion. Yes, in C51, only char, int, long and float these types of basic data types can be implicitly converted. while others can only be used to display data type conversion. To use the cast operator .should follow the following expression: <BR> <BR> (type) expression <BR> <BR> to deal with the display type conversion between different types of data operations and the assignment is very easy and convenient, especially for pointer variable assignment .is very useful. saw her a small program: <BR> <BR> # include </ AT89X51.H> <BR> # include <STDIO.H> </ STDIO.H> void main (void) <BR> { .<BR> char xdata * XROM; <BR> char a; <BR> int Aa = 0xFB1C; <BR> long Ba = 0x893B7832; <BR> float Ca = 3.4534; <BR> SCON = 0x50; / / serial port mode 1 ., allows the receiving <BR> TMOD = 0x20; / / Timer 1 Timer mode 2 <BR> TH1 = 0xE8; / / 11.0592MHz 1200 baud <BR> TL1 = 0xE8; <BR> TI = 1; <BR> .TR1 = 1; / / start the timer <BR> XROM = (char xdata *) 0xB012; / / initial value to a pointer variable assigned XROM <BR> * XROM = 'R'; / / to point to the absolute address assignment XROM < .BR> a = * ((char xdata *) 0xB012); / / is equivalent to a = * XROM <BR> printf ("% bx% x% d% c \ n", (char) Aa, (int) Ba, .( int) Ca, a); / / conversion type and output <BR> while (1); <BR>} <BR> <BR> result of the program: 1c 7832 3 R <BR> <BR> In the above .program, it is clear that the mandatory conversion to various types of basic use of the program in the external data memory in XDATA first define a character pointer variable XROM, when used XROM = (char xdata *) 0xB012 this statement .When the address pointer put 0xB012 assigns a XROM, as you would with XROM is illegal, this method is particularly suited to use the identifier to access an absolute address, such as in the program prior to using # define ROM 0xB012 this statement ., in the program can use the above method using the absolute address 0xB012 ROM access operation was carried out. <BR> <BR> in Appendix III shows the priority of operators. <BR> <BR> the end of this lesson .after, C language data types and operations of some basic learning rule has been finished, the next lesson will start about syntax, functions, etc.. <BR>.

No comments:

Post a Comment