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

Microprocessor Systems: EE-222 Summer 2022

This document contains a lecture on AVR I/O port programming. It discusses the structure of I/O ports and pins on AVR microcontrollers. Each port has three associated I/O registers - PORT, DDR, and PIN - that are used to program the direction and value of pins. Examples are given for configuring pins as inputs or outputs and reading from or writing to pin values. Instructions like SBI, CBI, SBIC, and SBIS are also introduced for manipulating individual pin bits.

Uploaded by

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

Microprocessor Systems: EE-222 Summer 2022

This document contains a lecture on AVR I/O port programming. It discusses the structure of I/O ports and pins on AVR microcontrollers. Each port has three associated I/O registers - PORT, DDR, and PIN - that are used to program the direction and value of pins. Examples are given for configuring pins as inputs or outputs and reading from or writing to pin values. Instructions like SBI, CBI, SBIC, and SBIS are also introduced for manipulating individual pin bits.

Uploaded by

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

EE-222 Summer 2022

Microprocessor Systems

Lecture # 05
AVR I/O Port Programming

Muhammad Imran
muhammad.imran1@seecs.edu.pk
Acknowledgement
2

▪ Content from following has been used in these slides


▪ Textbook Contents & PowerPoint Slides
▪ The AVR Microcontroller and Embedded Systems, M. Ali Mazidi,
Sarmad Naimi, Sepehr Naimi, 4th Edition
Contents
3

▪ I/O Port Programming


▪ I/O Ports Bit Manipulation
▪ Synchronizer Delay
I/O Port Programming
AVR I/O Ports
5
Structure of I/O Pins
6

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
Structure of I/O Pins
7

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 PINx: 7 6 5 4 3 2 1 0
DDRx: 7 6 5 4 3 2 1 0
PORTC PORTD
DDRC DDRD PORTx: 7 6 5 4 3 2 1 0
PINC PIND
PD0 PC7
000 777 Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0
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
I/O Port Programming
8

▪ 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


Structure of I/O Pins
9

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
DDRx.n PC5

DDRx
PD2 222 555 1
0
PD3 333 444
PORTx.n PC4 PORTx

PD4 444 333 PC3 0 high impedance Out 0


PD5 555 222
PINx.n PC2 1 pull-up Out 1
PD6 666 111 PC1
PD7 777 000 PC0
I/O Registers
10

▪ 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

▪ Write a program that makes all the pins of PORTB one.

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

0 high impedance Out 0

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

Outside the Inside the


AVR chip AVR chip
Pull-Up Resistor
14

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

▪ SBI (Set Bit in IO register)


▪ SBI ioReg, bit ;ioReg.bit = 1
▪ Examples:
▪ SBI PORTD,0 ;PORTD.0 = 1
▪ SBI DDRC,5 ;DDRC.5 = 1

▪ CBI (Clear Bit in IO register)


▪ CBI ioReg, bit ;ioReg.bit = 0
▪ Examples:
▪ CBI PORTD,0 ;PORTD.0 = 0
▪ CBI DDRC,5 ;DDRC.5 = 0
Exercise
18

▪ Write a program that toggles PORTB.4 continuously


▪ Solution
▪ SBI DDRB,4
▪ L1: SBI PORTB,4
▪ CBI PORTB,4
▪ RJMP L1
Another Exercise
19

▪ 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

▪ SBIC (Skip if Bit in IO register Cleared)


▪ SBIC ioReg, bit ; if (ioReg.bit = 0) skip next instruction
▪ Example:
▪ SBIC PORTD,0 ;skip next instruction if PORTD.0=0
▪ INC R20
▪ LDI R19,0x23

▪ SBIS (Skip if Bit in IO register Set)


▪ SBIS ioReg, bit ; if (ioReg.bit = 1) skip next instruction
▪ Example:
▪ SBIS PORTD,0 ;skip next instruction if PORTD.0=1
▪ INC R20
▪ LDI R19,0x23

can only be used for any bits of the lower 32 I/O registers
Exercise
21

▪ Write a program to perform the following:


a. Keep monitoring the PB2 bit until it becomes HIGH;
b. When PB2 becomes HIGH, write value $45 to Port C, and also send a HIGH-
to-LOW pulse to PD3.
▪ Solution
▪ CBI DDRB, 2 ;make PB2 an input
▪ SBI PORTB,2
▪ LDI R16, 0xFF
▪ OUT DDRC, R16 ;make Port C an output port
▪ SBI DDRD, 3 ;make PD3 an output
▪ AGAIN: SBIS PINB, 2 ;Skip if Bit PB2 is HIGH
▪ RJMP AGAIN ;keep checking if LOW
▪ LDI R16, 0x45
▪ OUT PORTC, R16 ;write 0x45 to port C
▪ SBI PORTD, 3 ;set bit PD3 (H-to-L)
▪ CBI PORTD, 3 ;clear bit PD3
▪ HERE: RJMP HERE
Another Exercise
22

▪ A switch is connected to pin PB0 and an LED to pin PB5.


Write a program to get the status of SW and send it to the
VCC
LED. 4.7k AVR

PB0
Switch
PB5
▪ Solution 270

▪ CBI DDRB,0 ;make PB0 an input LED

▪ SBI DDRB,5 ;make PB5 an output


▪ AGAIN: SBIC PINB,0 ;skip next if PB0 is clear
▪ RJMP OVER ;(JMP is OK too)
▪ CBI PORTB,5
▪ RJMP AGAIN ;we can use JMP too
▪ OVER: SBI PORTB,5
▪ RJMP AGAIN ;we can use JMP too
Synchronizer Delay
Structure of I/O Pins
24

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

0 high impedance Out 0

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

▪ The input circuit of the AVR has a delay of 1 clock cycle:


▪ Input data in PIN register is latched one clock cycle after
▪ The PIN register represents the data that was present at the pins one
clock cycle ago
▪ Solution: Put NOP before the IN
Synchronizer Delay – Example
30
Practice Exercises
Exercise 1
32

▪ Write a program to create a square wave of 50% duty cycle


on bit 0 of Port C.
Exercise 2
33

▪ Write a program to perform the following:


▪ Keep monitoring the PB2 bit until it becomes HIGH
▪ When PB2 becomes HIGH, write the value $45 to Port C and also
send a HIGH-to-LOW pulse to PD3
Exercise 3
34

▪ Assume that bit PB3 is an input and represents the


condition of a door alarm.
▪ If it goes LOW, it means that the door is open
▪ Monitor the bit continuously
▪ Whenever it goes LOW, send a HIGH-to-LOW pulse to port PC5 to
turn on a buzzer
Exercise 3 – Solution
35
Exercise 4
36

▪ A switch is connected to pin PB0 and an LED to pin PB7.


Write a program to get the status of SW and send it to the
LED.
▪ Solution
Relevant Reading
37

▪ The AVR Microcontroller and Embedded Systems, M. Ali


Mazidi, Sarmad Naimi, Sepehr Naimi, 4th Edition
▪ Chapter 5
▪ Go through all the examples carefully and make sure you run them
on Atmel Studio for firm understanding.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy