
;##########################################################################
;#                                                                        #
;#                            By ELECTRO8051                           	  #
;#                                                                        #
;#   Version 2                                       07/12/2001           #
;#                                                                        #
;#                                                                        #
;#     Fichier I2CISS                                                     #
;#                                                                        #
;#------------------------------------------------------------------------#
;#                                                                        #
;#  S/pgm emulation bus I2C validé de 12 a 16mhz                          #
;#  sur UC type 8051 a 12Periodes/instruction                             #
;#                                                                        #
;##########################################################################
;


SDA    		EQU	P1.7	;SDA
SCL		EQU	P1.6	;SCL
ADRESS_ISS  	EQU	40h	;42h,44h,46h,48h,4Ah,4Ch,4Eh, modifier suivant cablage A0,A1,A2
				;40h pour (A0,A1,A2)=0
RB0		EQU     000H    ; Select Register Bank 0


	DSEG AT 20h

FLAGS:    	 DS     1       ; Bit adressable memory
ACKREC  	 BIT   FLAGS.0  ; Mis à 1 pour indiquer au master d'acquitter un octet en reception

A7A0StartAdd:	   DS     1       ; Octet A7-A0 start adresse
A15A8StartAdd:	   DS     1       ; Octet A15-A8 start adresse
EP1A16StartAdd:	   DS     1       ; Octet type eprom,n° eprom,bits A19-A16 start adresse
A7A0EndAdd:	   DS     1       ; Octet A7-A0 End adresse
A15A8EndAdd:	   DS     1       ; Octet A15-A8 End adresse
EP1A16EndAdd:	   DS     1       ; Octet type eprom,n° eprom,bits A19-A16 end adresse

STACK:	 	   DS	  1       ; Pile 



	ReadIss segment code
	rseg ReadIss

;********************************************************
;* titre : Tempo7MicroS		                        *
;*------------------------------------------------------*
;*                     				        *
;* fonction: tempo de 7 µs à 12mhz		        *
;*  call 2µs,ret 2µs				        *
;*						        *
;*------------------------------------------------------*
;* param. d'entree : aucun  			        *
;********************************************************

Tempo7MicroS:       ;call :2 µs
	nop	;1 µs
	nop	;1 µs
 	nop	;1 us
	ret	;2µs






;********************************************************
;* titre : EmisI2c12m          	                        *
;* Modif : Utilise B                                    *
;*------------------------------------------------------*
;*                     				        *
;* fonction: emet un octet sur bus i2c                  *
;* genere un stop si l'esclave n'acquitte pas           *
;*------------------------------------------------------*
;* param. d'entree : A	     			        *
;* param. de sortie: c =1 succes,c=0 pas d'ack du slave *
;********************************************************
EmisI2c12m:

	MOV	B,#8

     
EMIS1:	
	CLR	SCL

	RLC	A
	MOV	SDA,C    ;Bit info dans SDA
	NOP
	NOP
	SETB	SCL
	NOP
	NOP        
	JNB	SCL,ATTsicl
EMIS2:	
	DJNZ	B,EMIS1
EMIS3:	
	CLR	SCL
	SETB	SDA ;Le master laisse floter SDA pour que le slave le tire à 0:ACK du slave
	nop
	nop
	nop
	
	MOV	B,#20 ;Durée de l'attente du ACK 
	SETB	SCL
	JB	SDA,ATTACQ ;le slave doit donner le Ack par un 0 sur SDA
ACQ:	
	nop
	nop
	nop
	CLR	SCL
	SETB	C	;C=1:le slave a bien acquitté l'octet écrit dans ce dernier
RET2:	
	RET
ATTACQ:		;Attente ACK pendant décrément de B	
	JNB	SDA,ACQ  ;Si ACK reçu Fin
	DJNZ	B,ATTACQ
ARRET:	CALL StopI2c12M
	CLR C	;C=0 pour indiquer pas ACK 
	SJMP	RET2
ATTSICL:
	JB	SCL,EMIS2
	SJMP	ARRET




;***************************************************** 
;* titre : RecI2c12m 			  	     *
;* Modif : Utilise B 				     *
;*---------------------------------------------------*
;*                     				     *
;* fonction: recoit un octet sur bus i2c             *
;*						     *
;*---------------------------------------------------*
;* reg. modifie : A      			     *
;* param. d'entree : bit Ackrec  	   	     *
;* param. de sortie: donnee dans A	             *
;*                                    		     *
;***************************************************** 

RecI2c12m:

	MOV	B,#8
REC1:	
	CLR	SCL
	call Tempo7MicroS
	SETB	SCL
	NOP
	NOP
	MOV	C,SDA
	RLC	A
	DJNZ	B,REC1
	CLR	SCL	;genere 1 master dernier byte
	nop
	nop
	JNB AckRec,glop  ;Ackrec=0 :pas d'ack,Ackrec=1 :acquittement
	CLR SDA
	sjmp glap
GLOP:	SETB	sda  ;Pas de ack:cas octet unique ou dernier octet si plusieurs
glap:	NOP
	NOP
	NOP
	SETB	SCL	; acknowledge
	call Tempo7MicroS
	clr	SCL    
	nop
	nop
	nop
	SETB SDA
	nop
	nop
	nop
	RET















;***********************************************************
;* titre : StopI2c12M 		            	           *
;*---------------------------------------------------------*
;*                     				  	   *
;* fonction: Envoie les conditions de Stop/bus I2c         *
;*                                                         *
;*---------------------------------------------------------*
;* param. d'entree : aucun  			           *
;***********************************************************


StopI2c12M:   
	CLR	SDA
	CLR 	SCL
	SETB	SCL
	Call Tempo7MicroS		;4.7 micro mini	
	SETB	SDA
        RET

;********************************************************
;* titre : StartI2c12M 		                        *
;*------------------------------------------------------*
;*                     				        *
;* fonction: Envoie les conditions de Start/bus I2c     *
;*------------------------------------------------------*
;* param. d'entree : aucun  			        *
;********************************************************



StartI2c12M:   setb 	SDA     ;
	setb SCL
	call Tempo7MicroS	;4,7 µs nécessaire
	clr SDA
        RET

;************************************************************
;* titre : WriteIss 		                            *
;*----------------------------------------------------------*
;*                     				  	    *
;* fonction: Ecriture du ISS		                    *
;* testé de 12 à 16 mhz	 			            *
;*----------------------------------------------------------*
;* param. d'entree :ADRESS_ISS,EP1A16StartAdd,A15A8StartAdd *
;* A7A0StartAdd,EP1A16EndAdd,A15A8EndAdd,A7A0EndAdd         *
;* param. de sortie: C=1 ecriture OK,C=0 Incident           *
;************************************************************

WriteIss:
        MOV    PSW,#RB0      	; Select register bank 0
        MOV    SP,#STACK

	LCALL startI2c12m	;Start I2C
	MOV A,#ADRESS_ISS	;Positionnement de l'adresse du ISS
	LCALL EmisI2c12m	;Envoi de l'adresse sur le bus I2c
	JNC FIN			;si C=0,le ISS n'acquitte pas
	MOV A,EP1A16StartAdd    ;Octet type eprom,n° eprom,bits A19-A16 start adresse
        LCALL EmisI2c12m	;Emission de A sur le bus I2C
	JNC FIN
	MOV A,A15A8StartAdd     ;Octet A15-A8 start adresse
        LCALL EmisI2c12m	;Emission de A sur le bus I2C
	JNC FIN
	MOV A,A7A0StartAdd      ;Octet A7-A0 start adresse
        LCALL EmisI2c12m	;Emission de A sur le bus I2C
	JNC FIN
	MOV A,EP1A16EndAdd      ;Octet type eprom,n° eprom,bits A19-A16 end adresse
        LCALL EmisI2c12m	;Emission de A sur le bus I2C
	JNC FIN
	MOV A,A15A8EndAdd       ;Octet A15-A8 End adresse
        LCALL EmisI2c12m	;Emission de A sur le bus I2C
	JNC FIN
	MOV A,A7A0EndAdd        ;Octet A7-A0 End adresse
        LCALL EmisI2c12m	;Emission de A sur le bus I2C
	JNC FIN
	LCALL StopI2c12M 	;Stop I2C
	SETB C			;C=1 OK
fin:	RET
	;Broche INT passe à 0 et reste à 0 tant que la diffusion du message n'est pas terminée				
	END

