I2C2
	call    Init                ; Initialize device
	call    ByteRead            ; Byte Read operation
	RETURN


Init
    bcf     STATUS,RP1          ; Select Bank 01
    bsf     STATUS,RP0
    movlw   b'00011000'
    movwf   TRISC               ; Set PORTC to all inputs
    clrf    SSPSTAT             ; Disable SMBus inputs
    bsf     SSPSTAT,SMP         ; Disable slew rate control
    movlw   d'99'                ; Load 0x18 into WREG
    movwf   SSPADD              ; Setup 100 kHz I2C clock
    clrf    SSPCON2             ; Clear control bits
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   b'00101000'
    movwf   SSPCON              ; Enable SSP, select I2C Master mode
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bcf     PIR2,BCLIF          ; Clear Bit Collision flag
    retlw   0

BSTART
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,SEN         ; Generate Start condition
    bcf     STATUS,RP0          ; Select Bank 00
bstart_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    bstart_wait         ; If not, keep checking

    retlw   0

BRESTART
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,RSEN        ; Generate Restart condition
    bcf     STATUS,RP0          ; Select Bank 00
brestart_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    brestart_wait       ; If not, keep checking

    retlw   0

BSTOP
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,PEN         ; Generate Stop condition
    bcf     STATUS,RP0          ; Select Bank 00
bstop_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    bstop_wait          ; If not, keep checking

    retlw   0

TX
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    movf    datao,W             ; Copy datao to WREG
    movwf   SSPBUF              ; Write byte out to device
tx_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
   goto    tx_wait             ; If not, keep checking
;   bsf     STATUS,RP0          ; Select Bank 01
;   btfsc   SSPCON2,ACKSTAT     ; Check if ACK bit was received
;   goto    ackfailed           ; This executes if no ACK received    

    retlw   0

RX
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,RCEN        ; Initiate reception of byte
    bcf     STATUS,RP0          ; Select Bank 00
rx_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    rx_wait             ; If not, keep checking
    movf    SSPBUF,W            ; Copy byte to WREG
    movwf   CELZIUS               ; Copy WREG to datai
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,ACKEN       ; Generate ACK/NO ACK bit    
    bcf     STATUS,RP0          ; Select Bank 00
rx_wait2
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    rx_wait2            ; If not, keep checking

    retlw   0

ByteRead
    call    BSTART              ; Generate Start condition

                                ; Send control byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   B'10010000'          ; Load control byte for write
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send control byte to device

                                ; Send word address high byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   0x00               ; Load 0x5A for word address
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send high byte to device

    call    BRESTART            ; Generate Restart condition

                                ; Send control byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   B'10010001'           ; Load control byte for read
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send control byte to device

                                ; Read data byte
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,ACKDT       ; Select to send NO ACK bit
    call    RX                  ; Read data byte from device

    call    BSTOP               ; Generate Stop condition

    retlw   0

Poll
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   .40
    movwf   pollcnt             ; Set max polling times to 40
polling
    call    BRESTART            ; Generate start bit
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   WRITE_ADDR          ; Now send the control byte
    movwf   datao               ; Copy control byte to buffer
    call    TX                  ; Output control byte to device
    bsf     STATUS,RP0          ; Select Bank 01
    btfss   SSPCON2,ACKSTAT     ; Was the ACK bit low?
    goto    exitpoll            ; If yes, stop polling
                                ; If no, check if polled 40 times
    bcf     STATUS,RP0          ; Select Bank 00
    decfsz  pollcnt,F           ; Is poll counter down to zero?
    goto    polling             ; If no, poll again
;   goto    TimedOut            ; If yes, part didn't respond
                                ; in time, so take action
exitpoll
    call    BSTOP               ; Generate stop bit
    retlw   0