by fram » 12 Jun 2013, 16:26
Salve, Aiutandomi con il datasheet ho scritto il Master-trasmettitore, ora lo posto, mi dite se è giusto?
L'unica cosa che non mi è chiara è quando devo fare '; Verifico TWSR' non capisco il confronto tra cosa lo devo fare, mi sapete aiutare?
.def ADRESSE_SLAVE = R11
ldi temp,104 ; SLAVE ADRESSE
mov ADRESSE_SLAVE,temp
TWI_MASTER_TRANSMITTER:
; START
ldi temp, (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)
out TWCR, temp
; Attendi, che TWINT = 1
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x08 ; Start?????????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico SLA + "W" (W=0)
mov temp, ADRESSE_SLAVE
lsl temp ; L'ultimo bit '0' indica la 'W'
out TWDR, temp
; Attendo - TWINT sia cancellato per trasmettere
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x18 ; MT_SLA_ACK????????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico l'indirizzo del puntatore RTC
out TWDR, Indirizzo_0 ;punto all'indirizzo zero
; Attendo - TWINT sia cancellato per trasmettere
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28??????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico il dato 'secondi'
out TWDR, secondi
; senden - Clear TWINT
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28?????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico il dato 'minuti'
out TWDR, minuti
; senden - Clear TWINT
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28???????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico il dato 'ore'
out TWDR, ore
; senden - Clear TWINT
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28??????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico il dato 'day'
out TWDR, day???????
; senden - Clear TWINT
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28??????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico il dato 'giorno'
out TWDR, giorno
; senden - Clear TWINT
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28????????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico il dato 'mese'
out TWDR, mese
; senden - Clear TWINT
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28??????
brne TWI_MASTER_TRANSMITTER_ERROR
; Carico il dato 'anno'
out TWDR, anno
; senden - Clear TWINT
ldi temp, (1<<TWINT) | (1<<TWEN)
out TWCR, temp
; Attendo, che TWINT sia = 1, indica che il dato è stato trasmesso
rcall TWI_TWINT_attesa
; Verifico TWSR
in temp,TWSR
andi temp, 0xF8
cpi temp, 0x28 ; 0x28?????????
brne TWI_MASTER_TRANSMITTER_ERROR
; STOP
ldi temp, (1<<TWINT)|(1<<TWEN)|(1<<TWSTO)
out TWCR, temp
ret
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ERROR
TWI_MASTER_TRANSMITTER_ERROR:
rcall TWI_ERROR
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TWI_INI: ;Inizializzo TWI
; TWI TWBR Bit RATE Register
ldi temp, 3
out TWBR, temp
; TWI TWSR STATUS-Register
ldi temp, 0
out TWSR, temp
; TWI TWCR Control-REGISTER
ldi temp, 0<<TWINT|1<<TWEA|1<<TWEN
out TWCR,temp
ret
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TWI_TWINT_attesa:
in temp, TWCR
sbrs temp, TWINT
rjmp TWI_TWINT_attesa
ret
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TWI_ERROR:
cbi PORTD,0
; TWI STOP
ldi temp, (1<<TWINT)|(1<<TWEN)|(1<<TWSTO)
out TWCR, temp
; TWI aus
ldi temp, (0<<TWEN)
out TWCR, temp
; TWI INITIALISIERUNG
rcall TWI_INI
rcall wait_1s
ret
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++