
; Pin definitions for SCL and SDA

#define SDA portb,6   ; = RB.6
#define SCL portb,7   ; = RB.7

	include ii2.inc

;*****************************************************************
; LM75ReadByte
; Reads 1 byte from LM75
;
; Inputs:
; IISLAVEADR = slave-address
; IIADR      = address to read
;
; Output:
; IIDATA and W-Register = received byte from LM75
;*****************************************************************


LM75ReadByte

	call iistart            ; start

	banksel iislaveadr
	bcf status,carry
	rlf iislaveadr,w        ; Slave-Address
	iorlw B'10010000'       ; device-code for LM75 + write

	call iisend             ; send control-byte

	btfsc status,carry
	goto LM75ReadByte       ; device send NAK - then again


	banksel iiadr
	movfw iiadr             ; send address

	call iisend

	btfsc status,carry
	goto LM75ReadByte    ; device send NAK - then again

	call iistart            ; repeated start

	banksel iislaveadr
	bcf status,carry
	rlf iislaveadr,w        ; Slave-Address
	iorlw B'10010001'       ; device-code for LM75 + read

	call iisend             ; send control-byte

	btfsc status,carry
	goto LM75ReadByte    ; device send NAK - then again

	call iirec              ; read byte
	banksel iidataLM1
	movwf iidataLM1

	call iiack	

	call iirec              ; read byte
	banksel iidataLM
	movwf iidataLM

	call iinak              ; send NAK
	call iistop             ; thats all
	return


;*****************************************************************
; PCF8583ReadByte
; Reads 1 byte from PCF8583
;
; Inputs:
; IISLAVEADR = slave-address
; IIADR      = address to read
;
; Output:
; IIDATAPCF and W-Register = received byte from PCF8583
;*****************************************************************


PCF8583ReadByte

	call iistart            ; start

	banksel iislaveadr
	bcf status,carry
	rlf iislaveadr,w        ; Slave-Address
	iorlw B'10100000'       ; device-code for PCF8583 + write

	call iisend             ; send control-byte

	btfsc status,carry
	goto PCF8583ReadByte    ; device send NAK - then again

	banksel iiadr
	movfw iiadr             ; send address

	call iisend

	btfsc status,carry
	goto PCF8583ReadByte    ; device send NAK - then again

	call iistart            ; repeated start

	banksel iislaveadr
	bcf status,carry
	rlf iislaveadr,w        ; Slave-Address
	iorlw B'10100001'       ; device-code for PCF8583 + read

	call iisend             ; send control-byte

	btfsc status,carry
	goto PCF8583ReadByte    ; device send NAK - then again

	call iirec              ; read byte
	banksel iidataPCF
	movwf iidataPCF

	call iinak              ; send NAK
	call iistop             ; thats all
	return

;*****************************************************************
; LM75WriteByte
; Writes 1 byte to LM75
;
; Inputs:
; IISLAVEADR = slave-address
; IIADR      = address to read
; IIDATALM     = data send to LM75
;
; Output     = nothing
;*****************************************************************

LM75WriteByte

	call iistart            ; start

	banksel iislaveadr
	bcf status,carry
	rlf iislaveadr,w        ; Slave-Address
	iorlw B'10010000'       ; device-code for LM75 + write

	call iisend             ; send control-byte

	btfsc status,carry
	goto LM75WriteByte    ; device send NAK - then again

	banksel iiadr
	movfw iiadr             ; send address

	call iisend

	btfsc status,carry
	goto LM75WriteByte    ; device send NAK - then again

	banksel iidataLM
	movfw iidataLM            ; send data

	call iisend

	btfsc status,carry
	goto LM75WriteByte    ; device send NAK - then again

	call iistop             ; thats all
	return

;*****************************************************************
; PCF8583WriteByte
; Writes 1 byte to PCF8583
;
; Inputs:
; IISLAVEADR = slave-address
; IIADR      = address to read
; IIDATA     = data send to PCF8583
;
; Output     = nothing
;*****************************************************************

PCF8583WriteByte

	call iistart            ; start

	banksel iislaveadr
	bcf status,carry
	rlf iislaveadr,w        ; Slave-Address
	iorlw B'10100000'       ; device-code for PCF8583 + write

	call iisend             ; send control-byte

	btfsc status,carry
	goto PCF8583WriteByte    ; device send NAK - then again

	banksel iiadr
	movfw iiadr             ; send address

	call iisend

	btfsc status,carry 
	goto PCF8583WriteByte    ; device send NAK - then again

	banksel iidataPCF
	movfw iidataPCF            ; send data

	call iisend

	btfsc status,carry
	goto PCF8583WriteByte    ; device send NAK - then again

	call iistop             ; thats all
	return


;*****************************************************************
; LM75ReadTemp
;*****************************************************************

LM75ReadTemp

; read celsius - address 0

	movlw B'00000000'
	banksel iiadr
	movwf iiadr

	call LM75ReadByte

	banksel iidataLM
	movfw iidataLM        
	banksel celsius
	movwf celsius

	banksel iidataLM1
	movfw iidataLM1         
	banksel celsius_hi
	movwf celsius_hi

	
	return

;*****************************************************************
; PCF8583ReadClock
;*****************************************************************

PCF8583ReadClock

; read seconds - address 2

	movlw 2
	banksel iiadr
	movwf iiadr

	call PCF8583ReadByte

        banksel iidataPCF
        movwf iidataPCF
        banksel asec
        movwf asec

	andlw H'0F'          ; single
	banksel asec_e
	movwf asec_e

	banksel iidataPCF       ; tenner
	swapf iidataPCF,w       ; Bit 7:4 <-> 3:0
	andlw H'0F'          ; mask Bit 3:0
	banksel asec_z       ; store
	movwf asec_z

                 
; read minutes - address 3

	banksel iiadr
	incf iiadr,f  ; iiadr+1

	call PCF8583ReadByte
	
	banksel iidataPCF
	movwf iidataPCF
	banksel aminut
	movwf aminut
	
	andlw H'0F'
	banksel amin_e
	movwf amin_e

	banksel iidataPCF
	swapf iidataPCF,w       ; Bit 7:4 <-> 3:0
	andlw H'0F'
	banksel amin_z
	movwf amin_z

; read hours - address 4

	banksel iiadr
	incf iiadr,f  ; iiadr+1

	call PCF8583ReadByte

	banksel iidataPCF
	movwf iidataPCF
	banksel ahour
	movwf ahour

	andlw H'0F'
	banksel ahour_e
	movwf ahour_e

	banksel iidataPCF
	swapf iidataPCF,w       ; Bit 7:4 <-> 3:0
	andlw H'0F'
	banksel ahour_z
	movwf ahour_z


	return



;*****************************************************************
; PCF8583SetClock
;*****************************************************************

PCF8583SetClock

; Set seconds address 2

	movlw 2
	banksel iiadr
	movwf iiadr

	banksel asec_e
	movlw 0
	banksel iidataPCF
	movwf iidataPCF

	banksel asec_z
	swapf 0,w
	banksel iidataPCF
	iorwf iidataPCF,f

	call PCF8583WriteByte

; Set minutes address 3

	banksel iiadr
	incf iiadr,f   ; iiadr+1

	banksel amin_e
	movfw amin_e
	banksel iidataPCF
	movwf iidataPCF

	banksel amin_z
	swapf amin_z,w
	banksel iidataPCF
	iorwf iidataPCF,f

	call PCF8583WriteByte

; Set hours address 4

	banksel iiadr
	incf iiadr,f   ; iiadr+1

	banksel ahour_e
	movfw ahour_e
	banksel iidataPCF
	movwf iidataPCF

	banksel ahour_z
	swapf ahour_z,w
	banksel iidataPCF
	iorwf iidataPCF,f

	call PCF8583WriteByte


       
        	return





