Microprocessor Systems: EE-222 Summer 2022
Microprocessor Systems: EE-222 Summer 2022
Microprocessor Systems
Lecture # 05
AVR I/O Port Programming
Muhammad Imran
muhammad.imran1@seecs.edu.pk
Acknowledgement
2
PORTC PORTD
DDRC DDRD
PINC PIND
PD0 PC7
000 777
PD1 666 PC6
111
PD2 222 555 PC5
PD3 333 444 PC4
PD4 444 333 PC3
PD5 555 222 PC2
PD6 666 111 PC1
PD7 777 000 PC0
Structure of I/O Pins
7
▪ Each port has three (3) I/O registers associated with it:
▪ PORTx [Data Register][Read/Write]
▪ DDRx [Data Direction Register] [Read/Write]
▪ PINx [Port INput] [Read-Only]
▪ For example: for PORTB, we’ll have:
▪ PORTB
▪ DDRB
▪ PINB
PINx: 7 6 5 4 3 2 1 0
DDRx: 7 6 5 4 3 2 1 0
PORTx: 7 6 5 4 3 2 1 0
DDRx
PD2 222 555 1
0
PD3 333 444
PORTx.n PC4 PORTx
▪ DDRx
▪ Determines port direction i.e Input or Output
▪ 1 = output mode, 0 = input mode
▪ For example: to configure Port B as output load 0xFF in DDRB
▪ Note: Upon reset
▪ PORTx
▪ Value send through this register
▪ PINx
▪ To read the value
Example: Sending Data Out
11
DDRB: 1 1 1 1 1 1 1 1
PORTB: 1 1 1 1 1 1 1 1
▪ Solution
▪ LDI R20,0xFF ;R20 = 11111111 (binary)
▪ OUT DDRB,R20 ;DDRB = R20
▪ OUT PORTB,R20 ;PORTB = R20
DDRx
0 1
PORTx
1 pull-up Out 1
Exercise
12
▪ Write a code that will will toggle all 8 bits of Port B forever
with some time delay between “on” and “off” states
▪ Solution
▪ LDI R16,0xFF ;R16 = 0xFF = 0b11111111
▪ OUT DDRB,R16 ;make Port B an output port (1111 1111)
▪ L1: LDI R16, 0x55 ;R16 = 0x55 = 0b01010101
▪ OUT PORTB,R16 ;put 0x55 on port B pins
▪ CALL DELAY
▪ LDI R16,0xAA ;R16 = 0xAA = 0b10101010
▪ OUT PORTB,R16 ;put 0xAA on port B pins
▪ CALL DELAY
▪ RJMP L1
Pull-Up Resistor
13
vcc
1 = Close
PORTx.n 0 = Open
pin n of
port x PINx.n
AVR
VCC
PORTC.3
AVR
VCC
PORTC.3
Example – Taking Data In
15
▪ Write a code that gets the data from the pins of port C and
sends it to port B indefinitely, after adding the value 5 to it!
▪ Solution
▪ LDI R16,0x00 ;R16 = 00000000 (binary)
▪ OUT DDRC,R16 ;make Port C an input port
▪ LDI R16,0xFF ;R16 = 11111111 (binary)
▪ OUT DDRB,R16 ;make Port B an output port(1 for Out)
▪ L2: IN R16,PINC ;read data from Port C and put in R16
▪ LDI R17,5
▪ ADD R16,R17 ;add 5 to it
▪ OUT PORTB,R16 ;send it to Port B
DDRx
0 1
PORTx
▪ RJMP L2 ;jump L2 0 high impedance Out 0
1 pull-up Out 1
I/O Ports Bit Manipulation
SBI and CBI Instructions
17
▪ An LED is connected to each pin of Port D. Write a program to turn on each LED from pin D0 to
pin D7. Call a delay module before turning on the next LED
▪ Solution
▪ LDI R20, 0xFF
▪ OUT DDRD, R20 ;make PORTD an output port
▪ SBI PORTD,0 ;set bit PD0
▪ CALL DELAY ;delay before next one
▪ SBI PORTD,1 ;turn on PD1
▪ CALL DELAY ;delay before next one
▪ SBI PORTD,2 ;turn on PD2
▪ CALL DELAY
▪ SBI PORTD,3
▪ CALL DELAY
▪ SBI PORTD,4
▪ CALL DELAY
▪ SBI PORTD,5
▪ CALL DELAY
▪ SBI PORTD,6
▪ CALL DELAY
▪ SBI PORTD,7
▪ CALL DELAY
SBIC and SBIS
20
can only be used for any bits of the lower 32 I/O registers
Exercise
21
PB0
Switch
PB5
▪ Solution 270
RDx
PUD
P
DATA BUS
Q D
DDRxn
Q WR DDRxn
CLK
RESET
RRx
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK
RESET
SYNCHRONIZER
D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O
Out 0
25
RDx
PUD
P
1 1 DATA BUS
Q D 0
DDRxn
Q WR DDRxn
CLK
RESET
RRx
0 0 0 0
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK
RESET
SYNCHRONIZER
D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O
Out 1
26
RDx
PUD
P
1 1 DATA BUS
Q D 0
DDRxn
Q WR DDRxn
CLK
RESET
RRx
1 1 1 1
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK
RESET
SYNCHRONIZER
D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O
Structure of I/O Pins
27 DDRx
0 1
PORTx
1 pull-up Out 1
RDx
PUD
P
DATA BUS
Q D
DDRxn
Q WR DDRxn
CLK
RESET
RRx
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK
RESET
SYNCHRONIZER
D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O
Input – Tri-State vs Pull-Up
28
RDx
PUD
P
0 0 DATA BUS
Q D
DDRxn
Pull-up WR DDRxn
Q CLK
Resistor
RESET
0 RRx
0
Pxn Q D
PORTxn
WR PORTxn
Q CLK
Sleep RESET
SYNCHRONIZER
0 0 0 0 0
0 D Q D Q
PINxn RPx
N L Q Q
RESET RESET
CLKI/O
The represents how the content of PORTx register affects the pull-up resistor;
while the shows how a data can be read from a pin
Synchronizer Delay
29