L11-IO Port Programming
L11-IO Port Programming
AVR Microcontroller:
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:
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
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
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:
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
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:
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