twi_slave.asm
1 | ; TWI_INDATA = Daten vom Master
| 2 | ; TWI_OUTDATA = Daten zum Master
| 3 |
| 4 |
| 5 | TWI_INT:
| 6 | ; TWI Interrupt Handler
| 7 | in SREG_SV,SREG
| 8 | push TMPX
| 9 | in TMPX,TWSR ; Was liegt an?
| 10 | andi TMPX,0xF8 ; Bits ausmaskieren
| 11 | cpi TMPX,0x60 ; SLA+W empfangen
| 12 | breq TWI_SLA_W ; Master sendet Byte Start
| 13 | cpi TMPX,0x80 ; Daten erhalten
| 14 | breq TWI_GETDATA ; Einlesen der Daten
| 15 | cpi TMPX,0xA0 ; STOP erhalten
| 16 | breq TWI_STOP
| 17 | cpi TMPX,0xA8 ; SLA+R
| 18 | breq TWI_SLA_R ; Byte schreiben
| 19 | TWI_DEFAULT:
| 20 | ldi TMPX,(1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTO)|(0<<TWSTA)|(0<<TWWC); Enable TWI Interupt and clear the flag to send byte
| 21 | out TWCR, TMPX
| 22 | TWI_EXIT:
| 23 | pop TMPX
| 24 | out SREG,SREG_SV
| 25 | reti
| 26 |
| 27 |
| 28 | TWI_SLA_W:
| 29 | ; SLA+W empfangen ($60)
| 30 | ; Antworten, dass bereit
| 31 | ldi TMPX,(1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTO)|(0<<TWSTA)|(0<<TWWC); Enable TWI Interupt and clear the flag to send byte
| 32 | out TWCR, TMPX
| 33 | rjmp TWI_EXIT
| 34 |
| 35 | TWI_STOP:
| 36 | ; STOP empfangen
| 37 | ; Wieder auflegen, passiv mode
| 38 | ldi TMPX,(1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTO)|(0<<TWSTA)|(0<<TWWC); Enable TWI Interupt and clear the flag to send byte
| 39 | out TWCR, TMPX
| 40 | rjmp TWI_EXIT
| 41 |
| 42 | TWI_GETDATA:
| 43 | in TMPX,TWDR
| 44 | sts TWI_INDATA,TMPX
| 45 | ldi TMPX,(1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTO)|(0<<TWSTA)|(0<<TWWC); Enable TWI Interupt and clear the flag to send byte
| 46 | out TWCR, TMPX
| 47 | rjmp TWI_EXIT
| 48 | TWI_SLA_R:
| 49 | ; SLA+R empfangen ($A8)
| 50 | ; Antworten, Byte senden
| 51 | lds TMPX,TWI_OUTDATA
| 52 | out TWDR,TMPX
| 53 | ldi TMPX,(1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTO)|(0<<TWSTA)|(0<<TWWC); Enable TWI Interupt and clear the flag to send byte
| 54 | out TWCR, TMPX
| 55 | rjmp TWI_EXIT
|
|