Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

MPS W5-L2 AVR IO Programming II (Bit Addressability)

The document covers AVR I/O port programming, focusing on bit addressability and single-bit instructions like SBI and CBI for manipulating specific bits in I/O registers. It includes examples of programs for toggling ports, monitoring inputs, and creating delays, as well as class activities for practical application. Key concepts such as synchronizer delay and the limitations of internal RAM are also discussed.

Uploaded by

Muhammad Fahad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MPS W5-L2 AVR IO Programming II (Bit Addressability)

The document covers AVR I/O port programming, focusing on bit addressability and single-bit instructions like SBI and CBI for manipulating specific bits in I/O registers. It includes examples of programs for toggling ports, monitoring inputs, and creating delays, as well as class activities for practical application. Key concepts such as synchronizer delay and the limitations of internal RAM are also discussed.

Uploaded by

Muhammad Fahad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

EE-222 Microprocessor Systems

AVR IO Programming II
Week5-Lecture2

Dr. Sara Shakil Qureshi


I/O Port Programming
Bit Addressability
My Demo Board
I/O Ports and bit-addressability
 Accessing only 1 or 2 bits of the port.
 Instead of using entire 8-bits.

 Single-bit instructions
SBI and CBI instructions
 SBI (Set Bit in IO register)
 SBI ioReg, bit_num ;ioReg.bit_num = 1
 Examples:


SBI PORTD,0 ;PORTD.0 = 1

SBI DDRC,5 ;DDRC.5 = 1

 CBI (Clear Bit in IO register)


 CBI ioReg, bit_num ;ioReg.bit_num = 0
 Examples:


CBI PORTD,0 ;PORTD.0 = 0

CBI DDRC,5 ;DDRC.5 = 0
can only be used for any bits of the lower 32 I/O
registers
Example
• Write a program that toggles PORTB.4
continuously.
SBI DDRB,4
L1: SBI PORTB,4
CBI PORTB,4
RJMP L1
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
To make decisions
SBIC and SBIS
depending on the
state of a single pin
 SBIC (Skip if Bit in IO register Cleared)
 SBIC ioReg, bit_num ; if (ioReg.bit_num = 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_num ; if (ioReg.bit_num = 1) skip next

instruction
 Example:

SBIScanPORTD,0 ;skip
only be used next
for any bitsinstruction if I/O
of the lower 32
PORTD.0=1
registers
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
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
PB5
status of SW and send it to the 270
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
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
Example: Synchronizer
Delay
Examples:
DIY
Example
• Write a program to create a square wave of 50%
duty cycle on bit 0 of Port C.
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
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
Class Activity: Solution
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.
Bit Addressability
GPRs, SREG and Internal
RAM
20

Bit-addressability: GPRs
• Setting the bits
▫ d is 16-31 and k is an 8-bit value
R17 contains 0x7D

• Clearing the bits


▫ d is 16-31 and k is an 8-bit value
R17 contains 0x19
21

Bit-addressability: GPRs
• Copying a bit between registers
22

Bit-addressability: GPRs
• Checking a bit
23

Bit-addressability: SREG
• Checking a flag bit (s is between 0 and 7 and k is
relative addr)

Easy to use
24

Bit-addressability: SREG
• Manipulating a bit (s is between 0 and 7)
25

Bit-addressability: Internal RAM


• Internal RAM is not bit-addressable.
▫ To manipulate, bring it to GPR and then manipulate it.
26

Conclusion
• AVR I/O port programming
▫ Synchronization delay
▫ Bit addressability - GPRs, SREG and Internal RAM
27

Reading Material
• Textbook:
▫ Chapter 4
▫ Chapter 6, section 6.5.
28

Questions?

You might also like