beautypg.com

Assembly code example - slave receiver mode, Atmega163(l) – Rainbow Electronics ATmega163L User Manual

Page 83

background image

ATmega163(L)

83

Assembly Code Example - Slave Receiver Mode

;Part specific include file and TWI include file must be included.

;

ldi

r16, (1<

out

TWCR, r16

; Enable TWI in Slave Receiver Mode

;

wait10:in

r16,TWCR

; Wait for TWINT flag set. This indicates that

sbrs

r16, TWINT

; START followed by SLA+W has been received

rjmp

wait10

in

r16, TWSR

; Check value of TWI Status Register. If status

cpi

r16, SR_SLA_ACK

; different from SR_SLA_ACK, go to ERROR

brne

ERROR

ldi

r16, (1<

out

TWCR, r16

; Clear TWINT bit in TWCR to start reception of first

; data byte. Setting TWEA indicates that

; ACK should be returned after receiving first data byte

wait11: in

r16,TWCR

; Wait for TWINT flag set. This indicates that

sbrs

r16, TWINT

; data has been received and ACK returned

rjmp

wait11

in

r16, TWSR

; Check value of TWI Status Register. If status

cpi

r16, SR_SLA_ACK

; different from SR_SLA_ACK, go to ERROR

brne

ERROR

ldi

r16, (1<

out

TWCR, r16

; Clear TWINT bit in TWCR to start reception

; of data. Setting TWEA causes ACK to be

; returned after reception of next data byte

wait12:in

r16,TWCR

; Wait for TWINT flag set. This indicates that

sbrs

r16, TWINT

; data has been received and ACK returned

rjmp

wait12

in

r16, TWSR

; Check value of TWI Status Register. If status

cpi

r16, SR_DATA_ACK

; different from SR_DATA_ACK, go to ERROR

brne

ERROR

in

r16, TWDR

; Input received data from TWDR.

nop

;

ldi

r16, (1<

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

wait13:in

r16,TWCR

; Wait for TWINT flag set. This indicates that

sbrs

r16, TWINT

; data has been received and NACK returned

rjmp

wait13