Atmega163(l) – Rainbow Electronics ATmega163L User Manual
Page 80

ATmega163(L)
80
wait6:
in
r16,TWCR
; Wait for TWINT flag set. This indicates that
sbrs
r16, TWINT
; SLA+R has been transmitted, and ACK/NACK has
rjmp
wait6
; been received
in
r16, TWSR
; Check value of TWI Status Register. If status
cpi
r16, MR_SLA_ACK
; different from MR_SLA_ACK, go to ERROR
brne
ERROR
ldi
r16, (1<<TWINT) | (1<<TWEA) | (1<<TWEN)
out
TWCR, r16
; Clear TWINT bit in TWCR to start reception of data.
; Setting TWEA causes ACK to be
; returned after reception of data byte
wait7:
in
r16,TWCR
; Wait for TWINT flag set. This indicates that
sbrs
r16, TWINT
; data has been received and ACK returned
rjmp
wait7
in
r16, TWSR
; Check value of TWI Status Register. If status
cpi
r16, MR_DATA_ACK
; different from MR_DATA_ACK, go to ERROR
brne
ERROR
in
r16, TWDR
; Input received data from TWDR.
nop
;<do something with received data>
ldi
r16, (1<<TWINT) | (1<<TWEA) | (1<<TWEN)
out
TWCR, r16
; Clear TWINT bit in TWCR to start reception
; of data. Setting TWEA causes ACK to be
; returned after reception of data byte
;<Receive more data bytes if needed>
;receive next to last data byte.
wait8:
in
r16,TWCR
; Wait for TWINT flag set. This indicates that
sbrs
r16, TWINT
; data has been received and ACK returned
rjmp
wait8
in
r16, TWSR
; Check value of TWI Status Register. If status
cpi
r16, MR_DATA_ACK
; different from MR_DATA_ACK, go to ERROR
brne
ERROR
in
r16, TWDR
; Input received data from TWDR.
nop
;<do something with received data>
ldi
r16, (1<<TWINT) | (1<<TWEN)
out
TWCR, r16
; Clear TWINT bit in TWCR to start reception of data.
; Not setting TWEA causes NACK to be
; returned after reception of next data byte
; receive last data byte. Signal this to slave by returning NACK
wait9:
in
r16,TWCR
; Wait for TWINT flag set. This indicates that
sbrs
r16, TWINT
; data has been received and NACK returned
rjmp
wait9
in
r16, TWSR
; Check value of TWI Status Register. If status
cpi
r16, MR_DATA_NACK
; different from MR_DATA_NACK, go to ERROR