Академический Документы
Профессиональный Документы
Культура Документы
Agenda
I/O operations Arithmetic Bitwise Logical Relational Pre- and post- increment and decrement
I/O Operations
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
Data direction register (could be A, B, C, etc) DDRx = 0xff; //make all lines outputs DDRx = 0; // make all lines inputs
PORTC = value;
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
* / + %
Arithmetic Operators
* / + %
Arithmetic Operators
* / + %
10
Arithmetic Operators
* / + %
11
Arithmetic Operators
* / + %
12
Bitwise Operators
13
Bitwise Operators
14
Bitwise Operators
x (in binary) is 0b00000100 (it is an unsigned 8-bit integer). When it is shifted left once, x is 0b00001000
15
Bitwise Operators
Evaluate: char x = 4, y = 1, z; z = x | y;
16
Bitwise Operators
x (in binary) is 0b00000100 y (in binary) is 0b00000001 When ORed together : 0b00000101
17
Logical Operators
AND OR
&& ||
18
Logical Operators
AND OR
&& ||
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
Is equal to Is not equal to Less than Less than or equal to Greater than Greater than or equal to
20
Relational Operators
Is equal to Is not equal to Less than Less than or equal to Greater than Greater than or equal to
21
Relational Operators
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
Is equal to Is not equal to Less than Less than or equal to Greater than Greater than or equal to
23
Relational Operators
Is equal to Is not equal to Less than Less than or equal to Greater than Greater than or equal to
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
25
26
27
28
A += 3; B -= 2; C |= 3;
29
A += 3; B -= 2; 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
31
Summary
I/O operations (using the port names) Arithmetic (*, /, +, -, and %) Bitwise (&, <<, >>, ~, ^, and |) Logical (&& and ||) Relational (==, !=, <, >, <=, >=) Pre- and post- increment and decrement (++ and --)