0% found this document useful (0 votes)
5 views

L11-IO Port Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

L11-IO Port Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CS-336: Embedded Systems

AVR Microcontroller:
I/O Port Programming

Slight modification of slides shared by Dr. Rehan Ahmed

Instructor: Asra Abid Siddiqui [[email protected]]


I/O Port Programming

2
AVR I/O Ports

3
The Structure of I/O pins
PB0 000 000 PA0
PB1 111 111 PA1
PB2 222 222 PA2
PB3 333 333 PA3
PB4 444 444 PA4
PB5 555 555 PA5
PB6 666 666 PA6
PB7 777 777 PA7
PORTB PINA
DDRB DDRA
PINB PORTA

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

4
The Structure of I/O pins
PB0 000 000 PA0
PB1 111 111 PA1
PB2 222 222 PA2
PB3 333 333 PA3
PB4 444 444 PA4
PB5 555 555 PA5
PB6 666 666 PA6
PB7 777 777 PA7
PORTB PINA PINx: 7 6 5 4 3 2 1 0
DDRB DDRA
PINB PORTA DDRx: 7 6 5 4 3 2 1 0
PORTx: 7 6 5 4 3 2 1 0
PORTC PORTD
DDRC DDRD
PINC PIND Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0
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

5
I/O Port Programming
• 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

Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0

6
The Structure of I/O pins
PB0 000 000 PA0
PB1 111 111 PA1
PB2 222 222 PA2
PB3 333 333 PA3
PB4 444 444 PA4
PB5 555 555 PA5
PB6 666 666 PA6
PB7 777 777 PA7
PORTB PINA PINx: 7 6 5 4 3 2 1 0
DDRB DDRA
PINB PORTA DDRx: 7 6 5 4 3 2 1 0
PORTx: 7 6 5 4 3 2 1 0
PORTC PORTD
DDRC DDRD
PINC PIND Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0
PD0 PC7
000 777
PD1 666 PC6
111
PD2 DDRx.n
222 555 PC5
PD3 333 444
PORTx.n PC4
PD4 444 333 PC3
PD5 555 222
PINx.n PC2
PD6 666 111 PC1
PD7 777 000 PC0

7
I/O Registers
• DDRx
– Determines port direction i.e Input or Output
– 1 = output mode, 0 = input mode
– For example: to configure Port B as output, we load 0xFF in
DDRB
– On reset, DDRx has zeros !

• PORTx
– Value sent through this register

• PINx
– To read the value at pin

8
Example: Outputting Data
• Write a program that makes all the pins of PORTB one.

DDRB 1 1 1 1 1 1
: 1 11 1
PORTB: 1 1 1 1
1 1

LDI R20,0xFF ;R20 = 11111111 (binary)


OUT DDRB,R20 ;DDRB = R20
OUT PORTB,R20 ;PORTB = R20

DDRx
00 11

DDRx
PORTx
PORTx
00 high
highimpedance
impedance
Out
Out0 0

11 pull-up
pull-up Out
Out1 1

9
Example: Outputting Data
• The following code will toggle all 8 bits of Port B forever
with some time delay between “on” and “off” states:

LDI R16,0xFF ;R16 = 0xFF = 0b11111111


OUT DDRB,R16 ;make Port B an output port
LOOP: 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 LOOP

11
Pull-up resistor
PORTx [Data Register] [R/W]
DDRx [Data Direction Register]
[R/W]
PINx [Port INput] [R
only]
vcc
1 = Close
PORTx.n 0 = Open

pin n of
port x PINx.n

Outside the Inside the

DDRx
AVR chip AVR chip 00 11

DDRx
PORTx
PORTx
00 high
highimpedance
impedance
Out
Out0 0

11 pull-up
pull-up Out
Out1 1

12
Example: Inputting Data
• The following code gets the data present at the pins of port C
and sends it to port B indefinitely, after adding the value 5 to it:

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)
LOOP : 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
RJMP LOOP ;jump to label LOOP

14
Reading
• The AVR Microcontroller and Embedded Systems: Using
Assembly and C by Mazidi et al., Prentice Hall
– Chapter-4:
– Go through all the examples carefully and make sure you
run them on Atmel Studio for firm understanding.

15
THANK YOU

You might also like