Download - Embedded C Operators
-
8/13/2019 Embedded C Operators
1/32
-
8/13/2019 Embedded C Operators
2/32
Agenda
Evaluate and explain the results of operatorsused in C
I/O operations
Arithmetic Bitwise
Logical
Relational
Pre- and post- increment and decrement
Explain the precedence of the operators
2
-
8/13/2019 Embedded C Operators
3/32
I/O Operations
Embedded microcontrollers must interactdirectly with outside hardware
Through parallel ports on the controller
Microcontroller header files use sfrb and sfrw toassign labels to parallel ports Special Function Register (Byte or Word)
Defines labels such as DDRB, PINA, PORTB, etc
3
-
8/13/2019 Embedded C Operators
4/32
I/O Operations
To define inputs/outputs write to DDRx Data direction register (could be A, B, C, etc)
DDRx = 0xff; //make all lines outputs
DDRx = 0; // make all lines inputs
To write to an output port PORTC = value;
To read an input port
Variable_name = PINA;
4
-
8/13/2019 Embedded C Operators
5/32
I/O Operations
Example:
5
#include //register definition fileunsigned char z; //z is an 8-bit number void main (void) {
DDRA = 0; //make Port A all inputsDDRB = 0xff; //make Port B all outputswhile (1) {
z = PINA; //read Port APORTB = z + 1; //write the value (+1) to Port B
}}
-
8/13/2019 Embedded C Operators
6/32
I/O Operations Hands-on: write a C program to initialize Port C as all outputs and
Port B as all inputs. Read Port B, negate the number read and thenoutput the new number on Port C. Assume you are to use an
Atmega128.
6
-
8/13/2019 Embedded C Operators
7/32
I/O Operations Hands-on: write a C program to initialize Port C as all outputs and
Port B as all inputs. Read Port B, negate the number read and thenoutput the new number on Port C. Assume you are to use an
Atmega128.
7
#include //register definition fileunsigned char x; //x is an 8-bit number void main (void) {
DDRB = 0; //make Port B all inputsDDRC = 0xff; //make Port C all outputswhile (1) {
x = PINB; //read Port BPORTC = -x; //write the value to Port C
}}
-
8/13/2019 Embedded C Operators
8/32
Arithmetic Operators The arithmetic operators in C are:
Multiply: *
Divide: /
Addition: +
Subtraction: -
Negate: -
Modulo: %
8
-
8/13/2019 Embedded C Operators
9/32
Arithmetic Operators The arithmetic operators in C are:
Multiply: *
Divide: /
Addition: +
Subtraction: -
Negate: -
Modulo: %
9
Evaluate:
char x = 10, y = 3, z;z = x % y;
-
8/13/2019 Embedded C Operators
10/32
Arithmetic Operators The arithmetic operators in C are:
Multiply: *
Divide: /
Addition: +
Subtraction: -
Negate: -
Modulo: %
10
The modulo operator returns the
remainder of x/y
Evaluate:
char x = 10, y = 3, z;z = x % y;
z =1
-
8/13/2019 Embedded C Operators
11/32
Arithmetic Operators The arithmetic operators in C are:
Multiply: *
Divide: /
Addition: +
Subtraction: -
Negate: -
Modulo: %
11
Evaluate:
char x = 15, y = 4, z;z = x / y;
-
8/13/2019 Embedded C Operators
12/32
Arithmetic Operators The arithmetic operators in C are:
Multiply: *
Divide: /
Addition: +
Subtraction: -
Negate: -
Modulo: %
12
Evaluate:
char x = 15, y = 4, z;z = x / y;
z = 3
x, y, and z are all 8 bit integers
-
8/13/2019 Embedded C Operators
13/32
Bitwise Operators The bitwise operators in C are:
Ones compliment ~
Left shift >
AND &
OR |
Exclusive OR ^
13
-
8/13/2019 Embedded C Operators
14/32
Bitwise Operators The bitwise operators in C are:
Ones compliment ~
Left shift >
AND &
OR |
Exclusive OR ^
14
Evaluate:
char x = 4, y = 1, z;z = x
-
8/13/2019 Embedded C Operators
15/32
Bitwise Operators The bitwise operators in C are:
Ones compliment ~
Left shift >
AND &
OR |
Exclusive OR ^
15
Evaluate:
char x = 4, y = 1, z;z = x
-
8/13/2019 Embedded C Operators
16/32
Bitwise Operators The bitwise operators in C are:
Ones compliment ~
Left shift >
AND &
OR |
Exclusive OR ^
16
Evaluate:
char x = 4, y = 1, z;z = x | y;
-
8/13/2019 Embedded C Operators
17/32
Bitwise Operators The bitwise operators in C are:
Ones compliment ~
Left shift >
AND &
OR |
Exclusive OR ^
17
Evaluate:
char x = 4, y = 1, z;z = x | y;
z = 5
x (in binary) is 0b00000100
y (in binary) is 0b00000001When ORed together : 0b00000101
-
8/13/2019 Embedded C Operators
18/32
Logical Operators The logical operators in C are:
AND &&
OR ||
Results in TRUE or FALSE TRUE is any non-zero result
FALSE is a result equal to 0
18
Evaluate:
char x = 5, y = 2, z;z = x && y;
-
8/13/2019 Embedded C Operators
19/32
Logical Operators The logical operators in C are:
AND &&
OR ||
Results in TRUE or FALSE TRUE is any non-zero result
FALSE is a result equal to 0
19
Evaluate:
char x = 5, y = 2, z;z = x && y;
TRUE because both operands are non-zero (i.e.; TRUE && TRUE = TRUE)
-
8/13/2019 Embedded C Operators
20/32
Relational Operators The relational operators in C are:
Is equal to ==
Is not equal to !=
Less than =
20
-
8/13/2019 Embedded C Operators
21/32
-
8/13/2019 Embedded C Operators
22/32
-
8/13/2019 Embedded C Operators
23/32
Relational Operators The relational operators in C are:
Is equal to ==
Is not equal to !=
Less than =
23
Evaluate:
char x = 5, y = 2, z;if (x = y) then z = 1 else z = 0;
-
8/13/2019 Embedded C Operators
24/32
Relational Operators The relational operators in C are:
Is equal to ==
Is not equal to !=
Less than =
24
Evaluate:
char x = 5, y = 2, z;if (x = y) then z = 1 else z = 0;
z =1
This is a common error in C (and otherlanguages). There is only one = between x
and y, so this is actually an assignment inwhich we assign the value of y to x. Cevaluates an assignment as TRUE, so z = 1.BTW: x = 2 and y =2
-
8/13/2019 Embedded C Operators
25/32
Increment and Decrement Operators The increment operator in C is:
++
++x pre-increment
x++ post-increment
The decrement operator in C is: --
--x pre-decrement
x-- post-decrement
25
Evaluate:
char x = 5, y = 1, z;z = x * y++;
-
8/13/2019 Embedded C Operators
26/32
Increment and Decrement Operators The increment operator in C is:
++
++x pre-increment
x++ post-increment
The decrement operator in C is: --
--x pre-decrement
x-- post-decrement
26
Evaluate:
char x = 5, y = 1, z;z = x * y++;
z =5 and y = 2
This is a post-increment, so x (5) ismultiplied by y (1), and then y is
incremented to 2
-
8/13/2019 Embedded C Operators
27/32
Increment and Decrement Operators The increment operator in C is:
++
++x pre-increment
x++ post-increment
The decrement operator in C is: --
--x pre-decrement
x-- post-decrement
27
Evaluate:
char x = 5, y = 1, z;z = x * ++y;
-
8/13/2019 Embedded C Operators
28/32
Increment and Decrement Operators The increment operator in C is:
++
++x pre-increment
x++ post-increment
The decrement operator in C is: --
--x pre-decrement
x-- post-decrement
28
Evaluate:
char x = 5, y = 1, z;z = x * ++y;
z = 10 and y = 2
In this case y is incremented before themultiplication (pre-increment)
-
8/13/2019 Embedded C Operators
29/32
Compound Assignment Operators A short hand version of other C statements:
A += 3; same as A = A + 3;
B -= 2; same as B = B- 2;
C |= 3; same as C = C | 3;
29
Write the short hand versions of:
C = C * 5;D = D /2;E = E & 3;F = F ^ 5;
-
8/13/2019 Embedded C Operators
30/32
Compound Assignment Operators A short hand version of other C statements:
A += 3; same as A = A + 3;
B -= 2; same as B = B- 2;
C |= 3; same as C = C | 3;
30
Write the short hand versions of:
C = C * 5; C *= 5;D = D /2; D /= 2;E = E & 3; E &= 3;F = F 5; F =5;
-
8/13/2019 Embedded C Operators
31/32
-
8/13/2019 Embedded C Operators
32/32
Summary
Evaluated and explained the results ofoperators used in C
I/O operations (using the port names)
Arithmetic (*, /, +, -, and %) Bitwise (&, , ~, ^, and |)
Logical (&& and ||)
Relational (==, !=, , =)
Pre- and post- increment and decrement (++ and --)
Explained the precedence of the operators
32