
; Pin definitions for SCL and SDA

#define SDA portc,4   ; = RC.4
#define SCL portc,3   ; = RC.3

	include ii2.inc

;*****************************************************************
; PCF8583ReadByte
; Reads 1 byte from PCF8583
;
; Inputs:
; IISLAVEADR = slave-address
; IIADR      = address to read
;
; Output:
; IIDATA 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 iidata
	movwf iidata

	call iinak              ; send NAK
	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 iidata
	movfw iidata            ; send data

	call iisend

	btfsc status,carry
	goto PCF8583WriteByte    ; device send NAK - then again

	call iistop             ; thats all
	return

;*****************************************************************
; PCF8583ReadClock
;*****************************************************************

PCF8583ReadClock

; read seconds - address 2

	movlw 2
	banksel iiadr
	movwf iiadr

	call PCF8583ReadByte

	andlw H'0F'          ; single
	banksel asec_e
	movwf asec_e

	banksel iidata       ; tenner
	swapf iidata,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

	andlw H'0F'
	banksel amin_e
	movwf amin_e

	banksel iidata
	swapf iidata,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

	andlw H'0F'
	banksel ahour_e
	movwf ahour_e

	banksel iidata
	swapf iidata,w       ; Bit 7:4 <-> 3:0
	andlw H'0F'
	banksel ahour_z
	movwf ahour_z

; read day/year - address 5

	banksel iiadr
	incf iiadr,f  ; iiadr+1

	call PCF8583ReadByte

	andlw H'0F'
	banksel aday_e
	movwf aday_e

	banksel iidata
	swapf iidata,w
	andlw H'03'
	banksel aday_z
	movwf aday_z

	banksel iidata
	swapf iidata,f         ; shift Bit 7+6 to Bit 1+0
	rrf iidata,f
	rrf iidata,w
	andlw B'00000011'
	banksel ayear
	movwf ayear

; read month/dayofweek - address 6

	banksel iiadr
	incf iiadr,f  ; iiadr+1

	call PCF8583ReadByte

	andlw H'0F'
	banksel amonth_e
	movwf amonth_e

	banksel iidata
	swapf iidata,w
	andlw H'01'
	banksel amonth_z
	movwf amonth_z

	return

;*****************************************************************
; PCF8583SetClock
;*****************************************************************

PCF8583SetClock

; Set seconds address 2

	movlw 2
	banksel iiadr
	movwf iiadr

	banksel asec_e
	movfw asec_e
	banksel iidata
	movwf iidata

	banksel asec_z
	swapf asec_z,w
	banksel iidata
	iorwf iidata,f

	call PCF8583WriteByte

; Set minutes address 3

	banksel iiadr
	incf iiadr,f   ; iiadr+1

	banksel amin_e
	movfw amin_e
	banksel iidata
	movwf iidata

	banksel amin_z
	swapf amin_z,w
	banksel iidata
	iorwf iidata,f

	call PCF8583WriteByte

; Set hours address 4

	banksel iiadr
	incf iiadr,f   ; iiadr+1

	banksel ahour_e
	movfw ahour_e
	banksel iidata
	movwf iidata

	banksel ahour_z
	swapf ahour_z,w
	banksel iidata
	iorwf iidata,f

	call PCF8583WriteByte

; Set day/year address 5

	banksel iiadr
	incf iiadr,f   ; iiadr+1

	banksel ayear
	swapf ayear,w
	banksel iidata
	movwf iidata
	rlf iidata,f
	rlf iidata,f
	movlw B'11000000'
	andwf iidata,f

	banksel aday_e
	movfw aday_e
	andlw H'0F'
	banksel iidata
	iorwf iidata,f

	banksel aday_z
	swapf aday_z,w
	andlw H'30'
	banksel iidata
	iorwf iidata,f

	call PCF8583WriteByte

; Set month address 6

	banksel iiadr
	incf iiadr,f   ; iiadr+1

	banksel amonth_e
	movfw amonth_e
	banksel iidata
	movwf iidata

	banksel amonth_z
	swapf amonth_z,w
	andlw H'10'
	banksel iidata
	iorwf iidata,f

	call PCF8583WriteByte

	return

