Docsity

Embedded C Operators - Embedded System Programming | ETEE 3285, Assignments of Electrical and Electronics Engineering

Partial preview of the text

Download Embedded C Operators - Embedded System Programming | ETEE 3285 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Embedded Systems Programming

ETEE 3285

Topic 4: Embedded C Operators

Agenda

 Evaluate and explain the results of operators

used in C

 I/O operations

 Arithmetic

 Bitwise

 Logical

 Relational

 Pre- and post- increment and decrement

 Explain the precedence of the operators

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;

I/O Operations

 Example:

#include <MEGA8535.h> //register definition file
unsigned char z; //z is an 8-bit number
void main (void) {
DDRA = 0; //make Port A all inputs
DDRB = 0xff; //make Port B all outputs
while (1) {
z = PINA; //read Port A
PORTB = z + 1; //write the value (+1) to Port B

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 then

output the new number on Port C. Assume you are to use an

Atmega128.

#include <MEGA128.h> //register definition file
unsigned char x; //x is an 8-bit number
void main (void) {
DDRB = 0; //make Port B all inputs
DDRC = 0xff; //make Port C all outputs
while (1) {
x = PINB; //read Port B
PORTC = -x; //write the value to Port C

Arithmetic Operators

 The arithmetic operators in C are:

 Multiply: *  Divide: /  Addition: +  Subtraction: -  Negate: -  Modulo: %

Arithmetic Operators

 The arithmetic operators in C are:

 Multiply: *  Divide: /  Addition: +  Subtraction: -  Negate: -  Modulo: %

The modulo operator returns the

remainder of x/y

Evaluate:
char x = 10, y = 3, z;
z = x % y;
z =

Arithmetic Operators

 The arithmetic operators in C are:

 Multiply: *  Divide: /  Addition: +  Subtraction: -  Negate: -  Modulo: %

Evaluate:
char x = 15, y = 4, z;
z = x / y;

Bitwise Operators

 The bitwise operators in C are:

 Ones compliment ~  Left shift <<  Right shift >>  AND &  OR |  Exclusive OR ^

Bitwise Operators

 The bitwise operators in C are:

 Ones compliment ~  Left shift <<  Right shift >>  AND &  OR |  Exclusive OR ^

Evaluate:
char x = 4, y = 1, z;
z = x << y;

Bitwise Operators

 The bitwise operators in C are:

 Ones compliment ~  Left shift <<  Right shift >>  AND &  OR |  Exclusive OR ^

Evaluate:
char x = 4, y = 1, z;
z = x | y;

Bitwise Operators

 The bitwise operators in C are:

 Ones compliment ~  Left shift <<  Right shift >>  AND &  OR |  Exclusive OR ^

Evaluate:
char x = 4, y = 1, z;
z = x | y;
z = 5

x (in binary) is 0b

y (in binary) is 0b

When ORed together : 0b

Logical Operators

 The bitwise operators in C are:

 AND &&
 OR ||

 Results in TRUE or FALSE

 TRUE is any non-zero result  FALSE is a result equal to 0

Evaluate:
char x = 5, y = 2, z;
z = x && y;
TRUE because both operands are non-zero (i.e.; TRUE && TRUE = TRUE)

Relational Operators

 The relational operators in C are:

 Is equal to ==  Is not equal to !=  Less than <  Less than or equal to <=  Greater than >  Greater than or equal to >=