
;__________________________________________________________________________
                                                   				     ; I2C PCF DATASEND
I2CPCF:
; Send all the sequence to the PCF (address byte+data byte)
;!!!****!!!R0nan!!!===> I put a CALL SENDVAL command into this subrouteen to make it work.
    CALL	STARTBIT 				; send start bit
    MOV     A, R_DAC_ADR			; send PCF address, PCF1 0x44 and PCF2 0x46
    CALL    SENDBYTE      			; sets fI2C_NOACK if NACK received
   	JB     	fI2C_NOACK, STP_SND1		; if no acknowledge send stop
		MOV    	A, R_DAC_LSB			; send DAC LS-byte
;-----------------------*****(R0nan)******---------------------------
		CALL 	SENDVAL				; I don't know how or why, but this makes it work...?
;-----------------------*****(R0nan)******---------------------------
 		CALL   	SENDBYTE       		; sets fI2C_NOACK if NACK received
STP_SND1:  
    CALL STOPBIT         			; sends stop bit
    	JNB  fI2C_NOACK, SND_RET1  		; if slave sends no-acknowedge send error
    SETB fI2C_ERR             		; sets the error flag
    SETB I2CRS           			; this resets the I2C interface
SND_RET1:
	 RET
;______________________________________________________________________________________



;____________________________________________________________________
                                                           				; I2C SENDBYTE
SENDBYTE:
; Send 8-bits in ACC to the slave
   	MOV    	R_BITCNT,#8       	; 8 bits in a byte
   	SETB   	MDE             	; to enable SDATA pin as an output	
   	CLR    	MCO             	; make sure that the clock line is low
SENDBIT:
		RLC    	A              		; put data bit to be sent into carry
    	CALL	DELAY5				; delay 5uSec
		MOV    	MDO,C           	; put data bit on SDATA line
;-----------------------*****(R0nan)******---------------------------
		CALL	DELAY50				; delay 50uSec 
;-----------------------*****(R0nan)******---------------------------
   		SETB   	MCO             	; clock to send bit
		CALL	DELAY5				; delay 5uSec
		CALL	DELAY5				; delay 5uSec
   		CLR    	MCO             	; clear clock 
    DJNZ   	R_BITCNT,SENDBIT	; jump back and send all eight bits
   	CLR    	MDE            		; release data line for acknowledge	
	CALL	DELAY5				; delay 5uSec
	SETB   	MCO             	; send clock for acknowledge
	CALL	DELAY5				; delay 5uSec
	CALL   	DELAY5
   	JNB    	MDI,NEXT        	; this is a check for acknowledge
   		SETB   	fI2C_NOACK           	; no acknowledge, set flag		
NEXT: 
  	CLR    	MCO             	; clear clock 
	RET
;____________________________________________________________________
;____________________________________________________________________
		                                                        ; I2C STARTBIT
STARTBIT:
; Sends the start bit to initiate an I2C communication
   	SETB   	MDE             	; enable SDATA pin as an output
   	CLR    	fI2C_NOACK
   	CLR    	MDO             	; low O/P on SDATA 
   	CALL   	DELAY5          	; delay 5 uSec
   	CLR    	MCO             	; start bit
   	RET
;____________________________________________________________________
;____________________________________________________________________
                                                            		; I2C STOPBIT
STOPBIT:
; Sends the stop bit to end an I2C transmission
   	SETB   	MDE             	; to enable SDATA pin as an output
   	CLR    	MDO             	; get SDATA ready for stop
   	CALL	DELAY5				; delay 5uSec
	SETB   	MCO             	; set clock for stop
    CALL   	DELAY5				; delay 5uSec
    SETB   	MDO             	; this is the stop bit
	CALL 	DELAY5				; *(R0nan)*
    RET
;____________________________________________________________________
;____________________________________________________________________
                                                     		 ; DELAY5
DELAY5:
; 5 uSec delay
	PUSH ACC
	MOV	A,#0Eh
   	DJNZ ACC,$ 
	POP	ACC    
	RET
;____________________________________________________________________

;-----------------------*****(R0nan)******---------------------------
;                                                    				 ; DELAY50
DELAY50:
; 50 uSec delay
	PUSH B														 
	PUSH ACC
	MOV	B, #0x0A														 
DEL_50:
	MOV A, #0x0E
	DJNZ ACC, $
	DJNZ B, DEL_50
	POP ACC
	POP B    													 
	RET															 
;-----------------------*****(R0nan)******---------------------------
