170 likes | 528 Views
Operators and Expressions. Bitwise Operators: Bitwise operators perform functions that will affect the operand at the bit level. These operators work on non–floating point operands: char, int, and long. ` Bitwise Operators: Ones Complement: ~ Left Shift: << Right Shift: >>
E N D
Operators and Expressions Bitwise Operators: Bitwise operators perform functions that will affect the operand at the bit level. These operators work on non–floating point operands: char, int, and long. ` Bitwise Operators: Ones Complement: ~ Left Shift: << Right Shift: >> AND: & Exclusive OR: ^ OR (Inclusive OR): |
Some Examples of Bitwise Operations Assume that an unsigned character y = 0xC9. OperationResult x = ~y; x = 0x36 x = y << 3; x = 0x48 x = y >> 4; x = 0x0C x = y & 0x3F; x = 0x09 x = y ^ 1; x = 0xC8 x = y | 0x10; x = 0xD9
Logical and relational operators: Logical and relational operators are all binary operators but yield a result that is either TRUE or FALSE. TRUE is represented by a nonzero value, and FALSE by a zero value. Logical Operators: AND && OR | | These differ greatly from the bitwise operators in that they deal with the operands in a TRUE and FALSE sense. The AND logical operator yields a TRUE if both operands are TRUE, otherwise, a FALSE is the result. The OR logical operator yields a TRUE if either of the operators is TRUE. In the case of an OR, both operators must be FALSE in order for the result to be FALSE.
Examples: Assume x = 5 and y = 2; (x && y) is TRUE, because both are non-zero. (x & y) is FALSE, because the pattern 101b and 010b ANDed bitwise are zero. (x || y) is TRUE, because either value is non-zero. (x | y) is TRUE, because the pattern 101b and 010b Inclusive-ORed bitwise is 111b (non-zero).
Relational Operators: Relational operators use comparison operations. As in the logical operators, the operands are evaluated left to right and a TRUE or FALSE result is generated. They effectively “ask” about the relationship of two expressions in order to gain a TRUE or FALSE reply. Relational operators Is Equal to == Is Not Equal to != Less Than < Less Than or Equal to <= Greater Than > Greater Than or Equal to >=
Examples: Assume that x = 3 and y = 5. OperationResult (x == y) FALSE (x != y) TRUE (x < y) TRUE (x <= y) TRUE (x > y) FALSE (x >= y) FALSE
INCREMENT, DECREMENT, AND COMPOUND ASSIGNMENT Increment Operators Increment operators allow for an identifier to be modified, in place, in a pre-increment or post-increment manner. For example, x = x + 1; is the same as ++x; // pre-increment operation and as x++; // post-increment operation For example, i = 1; k = 2 * i++; // at completion, k = 2 and i = 2 i = 1; k = 2 * ++i; // at completion, k = 4 and i = 2 In the first case, i is incremented after the expression has been resolved. In the second case, i was incremented before the expression was resolved.
Decrement Operators Decrement operators function in a similar manner, causing a subtraction-of-one operation to be performed in a pre-decrement or post-decrement fashion: j--; // j = j-1 --j; // j = j-1
Compound Assignment Operators Compound assignment operators are another method of reducing the amount of syntax required during the construction of a program. A compound assignment is really just the combining of an assignment operator ( = ) with an arithmetic or logical operator. The expression is processed right to left, and syntactically it is constructed somewhat like the increment and decrement operators. Here are some examples: a += 3; // a = a + 3 b -= 2; // b = b – 2 c *= 5; // c = c * 5 d /= a; // d = d / a
This combining of an assignment with another operator works with modulo and bitwise operators (%, >>, <<, &, |, and ^) as well as the arithmetic operators (+, –, *, and /), as shown below: a |= 3; // a = a OR 3 b &= 2; // b = b AND 2 c ^= 5; // c = c exclusively ORed with 5 PORTC &= 3; // Write the current value on PORTC // ANDed with 3 back to port C. // Forcing all of the bits except the // lower 2 to 0 and leaving the lower 2 // bits unaffected.
Some Data Types Widely Used by C Compilers Data Type Size in Bits Data Range/Usage unsigned char 8-bit 0 to 255 char 8-bit –128 to +127 unsigned int 16-bit 0 to 65,535 int 16-bit –32,768 to +32,767 unsigned long 32-bit 0 to 4,294,967,295 long 32-bit –2,147,483,648 to +2,147,483,648 float 32-bit ±1.175e-38 to ±3.402e38 double 32-bit ±1.175e-38 to ±3.402e38
Unsigned char Because the AVR is an 8-bit microcontroller, the character data type is the most natural choice for many applications. The unsigned char is an 8-bit data type that takes a value in the range of 0–255 (00–FFH). It is one of the most widely used data types for the AVR Remember that C compilers use the signed char as the default unless we put the keyword unsigned in front of the char (see Example 7-1). We can also use the unsigned char data type for a string of ASCII characters, including extended ASCII characters. Example 7-2 shows a string of ASCII characters. See Example 7-3 for toggling a port 200 times.
Example 7-1: Write an AV C program to send values 00–FF to Port B. Solution: #include <avr/io.h> //standard AVR header int main(void) { unsigned char z; DDRB = 0xFF; //PORTB is output for(z = 0; z <= 255; z++) PORTB = z; return 0; } //Notice that the program never exits the for loop because if you //increment an unsigned char variable when it is 0xFF, it will //become zero.
Example 7-2: Write an AVR C program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C, and D to Port B. Solution: #include <avr/io.h> //standard AVR header int main(void) //the code starts from here { unsigned char myList[]= "012345ABCD"; unsigned char z; DDRB = 0xFF; //PORTB is output for(z=0; z<10; z++) //repeat 10 times and increment z PORTB = myList[z]; //send the character to PORTB while(1); //needed if running on a trainer return 0; }
Example 7-3: Write an AVR C program to toggle all the bits of Port B 200 times. Solution: //toggle PB 200 times #include <avr/io.h> //standard AVR header int main(void) //the code starts from here { DDRB = 0xFF; //PORTB is output PORTB = 0xAA; //PORTB is 10101010 unsigned char z; for(z=0; z < 200; z++) //run the next line 200 times PORTB = ~ PORTB; //toggle PORTB while(1); //stay here forever return 0; }
References: 1)Embedded C Programming and the Atmel AVR, 2nd Edition Author: Barnett/Cox/O'Cull Publisher: Cengage Learning 2) The AVR Microcontroller and Embedded Systems: Using Assembly and C Author:Muhammad Ali Mazidi, Sarmad Naimi, Sepehr Naimi Publisher: Prentice Hall