0% found this document useful (0 votes)
83 views18 pages

L12-IO Port Programming - Bit Manipulation

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)
83 views18 pages

L12-IO Port Programming - Bit Manipulation

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

CS-336: Embedded Systems

AVR Microcontroller:
I/O Ports Bit Manipulation

Slight modification of slides shared by Dr. Rehan Ahmed

Instructor: Asra Abid Siddiqui [[email protected]]


I/O Ports Bit Manipulation

3
I/O Ports Bit Manipulation
• Powerful feature of AVR:
– To access individual bits of a port without altering rest of the
bits
– Able to use all bits(8) or any individual bit without affecting
other
– Instructions can be used for lower 32 I/O registers (0X00 to
0X1F)

4
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

5
Example
• Write a program that toggles PORTB.4 continuously.

Set data-direction
of pin 4 of Port-B as
an output pin
SBI DDRB,4
LOOP: SBI PORTB,4 Send 1 to pin 4 of
CBI PORTB,4 Port-B
RJMP LOOP

Send 0 to pin 4 of
Port-B

6
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
7
SBIC and SBIS
 SBIC (Skip next instruction 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 next instruction 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

8
Example
VCC
A switch is connected to pin 4.7k AVR
PB0 and an LED to pin PB5.
PB0
Write a program to get the Switch

status of SW and send it to the PB5


270
LED.
LED

CBI DDRB,0 ;make PB0 an input


SBI DDRB,5 ;make PB5 an output
AGAIN: SBIC PINB,0 ;skip next instruction 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
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

Draw a
flowchart
and then
write the
code

10
Class Activity: Solution

11
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

12
Synchronizer Delay

13
Synchronizer Delay

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

– Input data in PIN register is latched after one clock cycle

 The PIN register represents the data that was present at the
pins one clock cycle ago

– Solution: Put NOP before the IN

14
Example: Synchronizer Delay

.EQU MYTEMP = 0x100 ;save it here


LDI R16, 0x00 ;R16 = 00000000
OUT DDRB, R16 ; make PORT B an input port (0 for In)
NOP
IN R16, PINB ;move data from pins of Port B to R16
STS MYTEMP, R16 ;store this data in MYTEMP

Use “NOP” before


inputting the data
from the PINx
register

15
Examples: DIY

16
Example
• Write a program to create a square wave of 50% duty
cycle on bit 0 of Port C.

17
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

18
THANK YOU

You might also like