'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
'   Library for DS1624 Temperature Sensor and EEPROM Chip
'   Requires: I2CLib.bas
'
'   DS1624_Config_Temp(mode)        - Configures temperature reading mode
'     								  (continuousor on-demand).
'   DS1624_Init_Temp()              - Starts an internal temperature conversion.
'   temp = DS1624_Rd_Temp()         - Returns the temperature in °C as Single.
'   DS1624_Wr_E2PROM(addr, value)   - Writes a byte to EEPROM at given address.
'   value = DS1624_Rd_E2PROM(addr)  - Reads a byte from EEPROM at given address.
'
'   Device Address: 0x49 (7-bit) → 0x92 write, 0x93 read
'
'   Author: COS
'   Date:   2025/07/20
'   Platform: PIC18, Oshonsoft PIC18 Basic v5.89
'--------------------------------------------------------------------------------
'--------------------------------------------------------------------------------

' Device 7-bit address = 0x49 → Write = 0x92, Read = 0x93
Const DS1624_WR_ADDRESS = 0x92 '(0x49 << 1) | 0
Const DS1624_RD_ADDRESS = 0x93 '(0x49 << 1) | 1

'------------------------------------------------------------
' Configure temperature reading mode
' Ct = 1 → One-shot mode (on-demand)
' Ct = 0 → Continuous conversion mode
'------------------------------------------------------------
Function DS1624_Config_Temp(Ct As Bit) As Byte
    Dim dt As Byte
    START_I2C()
    WRITE_I2C(DS1624_WR_ADDRESS)
    WRITE_I2C(0xAC)               ' Access configuration register
    START_I2C()
    WRITE_I2C(DS1624_RD_ADDRESS)
    dt = READ_I2C(0)              ' Read current config, send NACK
    STOP_I2C()

    dt.0 = Ct                     ' Set mode bit (bit 0)
    START_I2C()
    WRITE_I2C(DS1624_WR_ADDRESS)
    WRITE_I2C(0xAC)
    WRITE_I2C(dt)                 ' Write modified config
    STOP_I2C()
    WaitMs 15                    ' Wait for write cycle to complete
End Function

'------------------------------------------------------------
' Start internal temperature conversion
' (Only needed in one-shot mode)
'------------------------------------------------------------
Function DS1624_Init_Temp() As Byte
    START_I2C()
    WRITE_I2C(DS1624_WR_ADDRESS)
    WRITE_I2C(0xEE)              ' Start conversion command
    STOP_I2C()
End Function

'------------------------------------------------------------
' Read temperature in °C as a floating-point value
' Temperature = MSB + LSB/256 (only upper 5 bits used)
'------------------------------------------------------------
Function DS1624_Rd_Temp() As Single
    Dim dt_HB, dt_LB As Byte
    Dim tp As Single
    START_I2C()
    WRITE_I2C(DS1624_WR_ADDRESS)
    WRITE_I2C(0xAA)              ' Read temperature command
    START_I2C()
    WRITE_I2C(DS1624_RD_ADDRESS)
    dt_HB = READ_I2C(1)          ' MSB, send ACK
    dt_LB = READ_I2C(0)          ' LSB, send NACK
    STOP_I2C()
    tp = CSingle dt_HB + 0.03125 * (dt_LB >> 3) ' Convert to °C
    ReturnValue tp
End Function

'------------------------------------------------------------
' Read a byte from EEPROM memory (0x17 command)
' addr = EEPROM address (0 to 255)
'------------------------------------------------------------
Function DS1624_Rd_E2Prom(address As Byte) As Byte
    Dim dt As Byte
    START_I2C()
    WRITE_I2C(DS1624_WR_ADDRESS)
    WRITE_I2C(0x17)              ' Read EEPROM command
    WRITE_I2C(address)           ' Target address
    START_I2C()
    WRITE_I2C(DS1624_RD_ADDRESS)
    dt = READ_I2C(0)             ' Read data, send NACK
    STOP_I2C()
    ReturnValue dt
End Function

'------------------------------------------------------------
' Write a byte to EEPROM memory (0x17 command)
' addr = EEPROM address (0 to 255)
' dt   = Byte to write
'------------------------------------------------------------
Function DS1624_Wr_E2Prom(address As Byte, dt As Byte) As Byte
    START_I2C()
    WRITE_I2C(DS1624_WR_ADDRESS)
    WRITE_I2C(0x17)              ' Write EEPROM command
    WRITE_I2C(address)
    WRITE_I2C(dt)
    STOP_I2C()
    WaitMs 5                    ' Wait for EEPROM write to complete
End Function
