English
Language : 

ATMEGA163_14 Datasheet, PDF (99/187 Pages) ATMEL Corporation – Nonvolatile Program and Data Memories
Assembly Code Example –
Slave Transmitter Mode
1142E–AVR–02/03
ATmega163(L)
; Part specific include file and TWI include file must be included.
; <Initialize registers, including TWAR, TWBR and TWCR>
ldi r16, (1<<TWINT) | (1<<TWEA) | (1<<TWEN)
out TWCR, r16
Transmitter Mode
; Enable TWI in Slave
; <Receive START condition and SLA+R>
wait14:in r16,TWCR
sbrs r16, TWINT
rjmp wait14
; Wait for TWINT flag set. This indicates that
; SLA+R has been received, and ACK/NACK has
; been returned
in r16, TWSR
; Check value of TWI Status Register. If status
cpi r16, ST_SLA_ACK; different from ST_SLA_ACK, go to ERROR
brne ERROR
ldi r16, 0x33
; Load data (here, data = 0x33) into TWDR Register
out TWDR, r16
ldi r16, (1<<TWINT) | (1<<TWEA) | (1<<TWEN)
out TWCR, r16
; Clear TWINT bit in TWCR to start transmission of
; data. Setting TWEA indicates that ACK should be
; received when transfer finished
; <Send more data bytes if needed>
wait15: in
r16,TWCR ; Wait for TWINT flag set. This indicates that
sbrs
r16, TWINT ; data has been transmitted, and ACK/NACK has
rjmp
wait15
; been received
in
cpi
brne
r16, TWSR ; Check value of TWI Status Register. If status
r16, ST_DATA_ACK ; different from ST_DATA_ACK, go to ERROR
ERROR
ldi
out
ldi
out
wait16:in
sbrs
rjmp
r16, 0x44 ; Load data (here, data = 0x44) into TWDR Register
TWDR, r16
r16, (1<<TWINT) | (1<<TWEA) | (1<<TWEN)
TWCR, r16 ; Clear TWINT bit in TWCR to start transmission of
; data. Setting TWEA indicates that ACK should be
; received when transfer finished
r16,TWCR ; Wait for TWINT flag set. This indicates that
r16, TWINT ; data has been transmitted, and ACK/NACK has
wait16
; been received
in
cpi
brne
r16, TWSR ; Check value of TWI Status Register. If status
r16, ST_DATA_ACK ; different from ST_DATA_ACK, go to ERROR
ERROR
ldi
r16, 0x55 ; Load data (here, data = 0x55) into TWDR Register
out
TWDR, r16
ldi
r16, (1<<TWINT) | (1<<TWEN)
out
TWCR, r16 ; Clear TWINT bit in TWCR to start transmission of
; data. Not setting TWEA indicates that NACK should
99