EE-222: Microprocessor Systems
AVR Microcontroller:
I/O Ports Bit Manipulation
My Demo Board
2
I/O Ports Bit Manipulation
3
SBI and CBI instructions
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
4
Example
• Write a program that toggles PORTB.4 continuously.
SBI DDRB,4
L1: SBI PORTB,4
CBI PORTB,4
RJMP L1
5
Example
• 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.
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
6
SBIC and SBIS
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
7
Example
• 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.
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
8
Example
VCC
• A switch is connected to 4.7k AVR
pin PB0 and an LED to pin PB0
Switch
PB5. Write a program to PB5
get the status of SW and 270
send it to the LED. LED
CBI DDRB,0 ;make PB0 an input
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
9
Synchronizer Delay
10
The structure of I/O pins
RDx
PUD
P
DATA BUS
Q D
DDRxn
Q WR DDRxn
CLK
RESET
DDRx.n
RRx
OUTPUT
PORTx.n
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK
PINx.n INPUT
RESET
SYNCHRONIZER
D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O
11
Out 0
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
12
Out 1
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
13
The structure of I/O pins
DDRx
00 11
DDRx
PORTx
PORTx
00 high
highimpedance
impedance
Out
Out0 0
RDx
PUD
11 pull-up
pull-up Out
Out1 1 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
14
Input (Tri-state vs. pull up)
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
15
Synchronizer Delay
• 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
16
Example: Synchronizer Delay
17
Examples: DIY
18
Example
• Write a program to create a square wave of 50% duty
cycle on bit 0 of Port C.
19
Example
• 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
20
Class Activity
• 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
21
Class Activity: Solution
22
Class Activity
• 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.
23
Reading
• The AVR Microcontroller and Embedded Systems: Using
Assembly and C by Mazidi et al., Prentice Hall
– Chapter-4: Complete
– Go through all the examples carefully and make sure you
run them on Atmel Studio for firm understanding.
• Also perform the left-over class activities
24
THANK YOU