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 PC+2
	breq PC+2
	rjmp TWI_MASTER_TRANSMITTER_ERROR
	

; Carico SLA + "W"  (W=0) 							
	mov temp, ADRESSE_SLAVE  ; L'ultimo bit '0' indica la 'W'
    rcall TWI_TWINT_attesa
	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 PC+2
	breq PC+2
	rjmp TWI_MASTER_TRANSMITTER_ERROR
	
; Carico l'indirizzo del puntatore RTC
    ldi temp,0
	rcall TWI_TWINT_attesa											
	out TWDR, temp  ;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, ora
; 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, temp ;???????
; 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_MASTER_RECEIVER:

; START 											
	ldi temp, (1<<TWINT | 1<<TWSTA | 1<<TWEN )
	out TWCR, temp	
; Attendo, che TWINT = 1						
	rcall TWI_TWINT_attesa

; Verifico TWSR 										
	in temp,TWSR		
	andi temp, 0xF8
	cpi temp,  0x08 	; Confronto con START 
	brne PC+2
	breq PC+2
	rjmp TWI_MASTER_TRANSMITTER_ERROR

; Carico SLA + "W"  (W=0) 							
	mov temp, ADRESSE_SLAVE  ; 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
        ldi temp,0										
	out TWDR, temp  ;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

;CONDIZIONE DI RE-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,  0x10	; Re-Start?????????????
	brne TWI_MASTER_TRANSMITTER_ERROR

	
; Carico SLA +"R" (R=1)	(lettura)							
	mov temp, ADRESSE_SLAVE	 ; 1 x links
	subi temp,-1
	out TWDR, temp

; setta e cancella TWINT 								
	ldi temp, (1<<TWINT | 1<<TWEA  | 1<<TWEN )
	out TWCR, temp

; attendi, che TWINT = 1, indica che il dato  stato trasmesso						
	rcall TWI_TWINT_attesa
	
; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x40 	;SLA+R 
	brne TWI_MASTER_ERROR
	
; setto  "ACK" (TWEA=1)  
	ldi temp, (1<<TWINT | 1<<TWEA  | 1<<TWEN )
	out TWCR, temp

; Controllo  TWINT 									
	rcall TWI_TWINT_attesa
	
; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x50	; Dato_ACK 
	brne TWI_MASTER_ERROR	; ERROR

; Leggo dato									
	in secondi, TWDR

; setto  "ACK" (TWEA=1), indica dato letto
	ldi temp, (1<<TWINT | 1<<TWEA  | 1<<TWEN )
	out TWCR, temp

; Controllo  TWINT 									
	rcall TWI_TWINT_attesa

; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x50	; Dato_ACK 
	brne TWI_MASTER_ERROR	; ERROR

; Leggo dato									
	in minuti, TWDR

; setto  "ACK" (TWEA=1), indica dato letto
	ldi temp, (1<<TWINT | 1<<TWEA  | 1<<TWEN )
	out TWCR, temp

; Controllo  TWINT 									
	rcall TWI_TWINT_attesa

; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x50	; Dato_ACK 
	brne TWI_MASTER_ERROR	; ERROR

; Leggo dato									
	in ora, TWDR

; setto  "ACK" (TWEA=1), indica dato letto
	ldi temp, (1<<TWINT | 1<<TWEA  | 1<<TWEN )
	out TWCR, temp

; Controllo  TWINT 									
	rcall TWI_TWINT_attesa

; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x50	; Dato_ACK 
	brne TWI_MASTER_ERROR	; ERROR

; Leggo dato									
	in temp, TWDR ;????????????

; setto  "ACK" (TWEA=1), indica dato letto
	ldi temp, (1<<TWINT | 1<<TWEA  | 1<<TWEN )
	out TWCR, temp

; Controllo  TWINT 									
	rcall TWI_TWINT_attesa

; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x50	; Dato_ACK 
	brne TWI_MASTER_ERROR	; ERROR

; Leggo dato									
	in giorno, TWDR

; setto  "ACK" (TWEA=1), indica dato letto
	ldi temp, (1<<TWINT | 1<<TWEA  | 1<<TWEN )
	out TWCR, temp

; Controllo  TWINT 									
	rcall TWI_TWINT_attesa

; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x50	; Dato_ACK 
	brne TWI_MASTER_ERROR	; ERROR

; Leggo dato									
	in mese, TWDR

; setto "NOT ACK" (TWEA=0) per l'ultimo byte ricevuto
	ldi temp, (1<<TWINT | 0<<TWEA  | 1<<TWEN )
	out TWCR, temp

; Controllo TWINT 									
	rcall TWI_TWINT_attesa
	
; Verifico TWSR										
	in temp,TWSR
	andi temp, 0xF8
	cpi temp, 0x58	; lettura dell'ultimo Byte, NACK
	brne TWI_MASTER_ERROR	; ERROR

; Lettura Dato									
	in anno, TWDR

; STOP												
	
	ldi temp, (1<<TWINT | 1<<TWEN |1<<TWSTO)
	out TWCR, temp

ret
;###############################################################################
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ERROR
TWI_MASTER_ERROR:
	rcall TWI_ERROR
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

TWI_INI:                                       ;Inizializzo TWI

	; TWI TWBR 			       Bit RATE Register	
	ldi temp, 12
	out TWBR, temp
	; TWI TWSR 				STATUS-Register		
	ldi temp, 0
	out TWSR, temp
	; TWI TWCR 				Control-REGISTER	
	ldi temp, (1<<TWINT|1<<TWEA|1<<TWEN)
	out TWCR,temp
ret
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

TWI_TWINT_attesa:
	in   bus, TWCR
	sbrs bus, TWINT
	rjmp TWI_TWINT_attesa
        ret

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TWI_ERROR:
; 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
        ret
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++