Вы находитесь на странице: 1из 32

Microcontroller Systems

ELET 3232 Topic 6: 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


2

I/O Operations

Embedded microcontrollers must interact directly with outside hardware


Through parallel ports on the controller Microcontroller header files use sfrb and sfrw to assign labels to parallel ports

Special Function Register (Byte or Word) Defines labels such as DDRB, PINA, PORTB, etc

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> unsigned char z; void main (void) { DDRA = 0; DDRB = 0xff; while (1) { z = PINA; PORTB = z + 1; } } //register definition file //z is an 8-bit number //make Port A all inputs //make Port B all outputs //read Port A //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.

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> unsigned char x; void main (void) { DDRB = 0; DDRC = 0xff; while (1) { x = PINB; PORTC = -x; } } //register definition file //x is an 8-bit number //make Port B all inputs //make Port C all outputs //read Port B //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:

* / + %

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

Arithmetic Operators

The arithmetic operators in C are:


Multiply: Divide: Addition: Subtraction: Negate: Modulo:

* / + %

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

The modulo operator returns the remainder of x/y

10

Arithmetic Operators

The arithmetic operators in C are:


Multiply: Divide: Addition: Subtraction: Negate: Modulo:

* / + %

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

11

Arithmetic Operators

The arithmetic operators in C are:


Multiply: Divide: Addition: Subtraction: Negate: Modulo:

* / + %

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

x, y, and z are all 8 bit integers

12

Bitwise Operators

The bitwise operators in C are:


Ones compliment Left shift Right shift AND OR Exclusive OR

~ << >> & | ^

13

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;

14

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=8

x (in binary) is 0b00000100 (it is an unsigned 8-bit integer). When it is shifted left once, x is 0b00001000

15

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;

16

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 0b00000100 y (in binary) is 0b00000001 When ORed together : 0b00000101

17

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

Evaluate: char x = 5, y = 2, z; z = x && y;

18

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

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

19

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

== != < <= > >=

20

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

== != < <= > >=

Evaluate: char x = 5, y = 2, z; if (x == y) then z = 1 else z = 0;

21

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

== != < <= > >=

Evaluate: char x = 5, y = 2, z; if (x == y) then z = 1 else z = 0; z =0 x and y are not equal, so the else clause is performed

22

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

== != < <= > >=

Evaluate: char x = 5, y = 2, z; if (x = y) then z = 1 else z = 0;

23

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

== != < <= > >=

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 other languages). There is only one = between x and y, so this is actually an assignment in which we assign the value of y to x. C evaluates an assignment as TRUE, so z = 1. BTW: x = 2 and y =2

24

Increment and Decrement Operators

The increment operator in C is:


++ ++x pre-increment x++ post-increment ---x pre-decrement x-- post-decrement

The decrement operator in C is:


Evaluate: char x = 5, y = 1, z; z = x * y++;

25

Increment and Decrement Operators

The increment operator in C is:


++ ++x pre-increment x++ post-increment ---x pre-decrement x-- post-decrement

The decrement operator in C is:


Evaluate: char x = 5, y = 1, z; z = x * y++; z =5 and y = 2

This is a post-increment, so x (5) is multiplied by y (1), and then y is incremented to 2

26

Increment and Decrement Operators

The increment operator in C is:


++ ++x pre-increment x++ post-increment ---x pre-decrement x-- post-decrement

The decrement operator in C is:


Evaluate: char x = 5, y = 1, z; z = x * ++y;

27

Increment and Decrement Operators

The increment operator in C is:


++ ++x pre-increment x++ post-increment ---x pre-decrement x-- post-decrement

The decrement operator in C is:


Evaluate: char x = 5, y = 1, z; z = x * ++y; z = 10 and y = 2

In this case y is incremented before the multiplication (pre-increment)

28

Compound Assignment Operators

A short hand version of other C statements:


A += 3; B -= 2; C |= 3;

same as A = A + 3; same as B = B- 2; same as C = C | 3;

Write the short hand versions of: C = C * 5; D = D /2; E = E & 3; F = F ^ 5;

29

Compound Assignment Operators

A short hand version of other C statements:


A += 3; B -= 2; C |= 3;

same as A = A + 3; same as B = B- 2; same as C = C | 3;

Write the short hand versions of: C = C * 5; D = D /2; E = E & 3; F = F ^ 5; C *= 5; D /= 2; E &= 3; F ^=5;

30

Operator Precedence

Below is a table that shows the precedence of the operators in C

31

Summary

Evaluated and explained the results of operators 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

Вам также может понравиться