L12-IO Port Programming - Bit Manipulation
L12-IO Port Programming - Bit Manipulation
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
4
Example
• Write a program that toggles PORTB.4 continuously.
SBI DDRB,4
L1: SBI PORTB,4
CBI PORTB,4
RJMP L1
5
Example
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
8
Example
VCC
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.
24
THANK YOU