MPS W5-L2 AVR IO Programming II (Bit Addressability)
MPS W5-L2 AVR IO Programming II (Bit Addressability)
AVR IO Programming II
Week5-Lecture2
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 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:
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.
Bit-addressability: GPRs
• Setting the bits
▫ d is 16-31 and k is an 8-bit value
R17 contains 0x7D
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
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?