Datasheet

113
ATmega323(L)
1457E11/01
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
brne ERROR