Attribute VB_Name = "SerialNumber"

Dim UnPort As Integer '= &H7 '0111
Dim UnDDrBit As Integer '= &H2 '0010
Dim UnCLKBit As Integer '= &H4 '0100
Dim UnDinBit As Integer ' = &H1 '0001
Dim UnDoutBit As Integer '= &H1 '0001
Dim UnShadow As Long '= &H2 '0010
'Dim DS1682_ADDR As Integer
Dim Un_SET_WP_CMD As Integer
Dim Un_READ_CMD As Integer
Dim Un_WRITE_CMD As Integer
Dim Un_ASN_ADD_CMD As Integer
Dim Un_CLR_ADD_CMD As Integer


Public Sub Un_Port_Init()
    UnPort = &H7 '0111             'Indicates register 7 (consider reg0 as 1st reg)
    UnDDrBit = &H20 '0010 0000           'Indicates 1st in register 7
    UnCLKBit = &H8 '00001000           'Indicates the 2nd bit in register 7 (from lsb if lsb=0)
    UnDinBit = &H10 '00010000           'Indicates least significant bit in register 7 (from lsb if lsb=0)
    UnDoutBit = &H10 '00010000          'Indicates the least significant bit in register 7 (from lsb if lsb=0)
    
    Un_SET_WP_CMD = &H60
    Un_READ_CMD = &H61
    Un_WRITE_CMD = &H62
    Un_ASN_ADD_CMD = &H64
    Un_CLR_ADD_CMD = &H66
End Sub


Public Sub Un_rst_RegPtr()
Dim er As Integer
    Un_Start_seq
    er = er + Un_Write_byte(Un_WRITE_CMD Or &H8)
    er = er + Un_Write_byte(&H0) ' writing the device id
    er = er + Un_Write_byte(0) ' register address
    If er > 0 Then
        'MsgBox "Error in writing"
    End If
    Un_Stop_seq
End Sub

Public Sub Un_Stop_seq()
    'STOP CONDITION: A change in the state of data line, from LOW to HIGH, while the clock line is HIGH,
    'defines the STOP condition.
    
    '             _____
    'Data        |    _|______
    '            |   / |
    '       _____|__/  |
    '            |     |
    '            |     |
    'Clock      _|_____|______
    '          / |     |
    '    _____/  |_____|
    
    
    ' Set DDR to output
    Un_SET_DDR_OUT
    
    ' Make Dout Low
    Un_SET_DATA_LOW
    ' Make Clock low
    Un_CLOCK_LOW
    
    ' Make Clock high
    Un_CLOCK_HIGH
    ' MaKe Dout high
    Un_SET_DATA_HIGH
End Sub

Public Sub Un_Start_seq()
    'START SEQUENCE:A change in the state of data line from, HIGH to LOW, while the clock is HIGH,
    'defines the start condition

    '         -----
    'Data ___|__   |
    '        |  \  |
    '        |   \_|__________
    '        |     |
    '        |     |
    'clock___|_____|___
    '        |     |   \
    '        |     |    \_______
    '         -----
    
    Un_CLOCK_LOW
    Un_SET_DDR_OUT
    Un_SET_DATA_LOW
    Un_CLOCK_HIGH
    Un_CLOCK_LOW
    'Make the data line high
    Un_SET_DATA_HIGH
    'Make the clock high
    Un_SET_DATA_HIGH
    
    'Make the clock high
    Un_CLOCK_HIGH
    'Set ddr to output
    
    'Make the data line low
    Un_SET_DATA_LOW
    'Make the clock low
    Un_CLOCK_LOW
End Sub

Public Function Un_Read_byte(nack As Integer) As Integer
Dim Rd_val As Integer
' Purpose: To read the byte from the address pointer by the register pointer. ( It is assumed that
' the register pointer has already been set. See page10 of data sheet for more details)
' Returns: The value read

' Set DDR for input
    Un_SET_DDR_IN
    Un_CLOCK_LOW
    I = &H80
    Rd_val = 0
    While CInt(I) > 0
    ' Raise clock
        Un_CLOCK_HIGH
    ' Read Din bit
        If Un_GET_DATA Then
            Rd_val = Rd_val Or (I)
        End If
    ' Lower clock
        Un_CLOCK_LOW
        I = I / 2
    Wend '........Continue sequence for 8 times for 8 bits (1Byte)

'GENRATING THE ACKNOWLEDGEMENT
If nack Then
        Un_SET_DATA_HIGH ' No acknowledge
Else
        Un_SET_DATA_LOW  ' Ack
End If

' Set DDR for output
Un_SET_DDR_OUT

If nack Then
        Un_SET_DATA_HIGH ' No acknowledge
Else
        Un_SET_DATA_LOW  ' Ack
End If
' Raise clock
    Un_CLOCK_HIGH
    
' Lower clock
    Un_CLOCK_LOW
    
    Un_SET_DDR_IN
    
    Un_Read_byte = Rd_val ' Return the value
    
End Function
Public Function Un_Write_byte(w_val As Integer) As Integer
'Purpose: To write the value passed in as argument to the register pointed by the register pointer. (It
' is assumed the register pointer is already set. (See page 10 of data sheet for details).
' Return: Returns true if write was successful else -1
Dim er As Integer

'Set DDR to output
    Un_SET_DDR_OUT
'Lower the clock
    Un_CLOCK_LOW
    
I = &H80
    While CInt(I) > 0
    'write to the dout corresponding to the value passed starting starting with msb
        If (I And w_val) Then
            Un_SET_DATA_HIGH
            'MsgBox Un_GET_DATA
        Else
            Un_SET_DATA_LOW
            'MsgBox Un_GET_DATA
        End If
'Raise clock
    Un_CLOCK_HIGH
'Lower clock
    Un_CLOCK_LOW
    
        I = I / 2
        
    Wend '..... Repeat  until 8 times (1 byte is writen)

'Check for the ACKNOWLEDGEMENT
'Set DDR for input
    Un_SET_DDR_IN
'Raise clock
    Un_CLOCK_HIGH
'Check the din bit (should be 0) else error
    
    If Un_GET_DATA > 0 Then
            er = 1
    Else
            er = 0
    End If
'Lower the clock
    Un_CLOCK_LOW

Un_Write_byte = er ' return if write was successful

End Function

Public Sub Un_SET_DATA_HIGH() ' Make data high
    UnShadow& = UnShadow& Or (UnDoutBit)
    vbapp_Write UnPort, UnShadow&
End Sub

Public Sub Un_SET_DATA_LOW() ' Make data low
    UnShadow& = UnShadow& And (Not UnDoutBit)
    vbapp_Write UnPort, UnShadow&
End Sub

Public Sub Un_CLOCK_HIGH() ' Raise the clock
    UnShadow& = UnShadow& Or (UnCLKBit)
    vbapp_Write UnPort, UnShadow&
End Sub

Public Sub Un_CLOCK_LOW() 'Lower the clock
    UnShadow& = UnShadow& And (Not UnCLKBit)
    vbapp_Write UnPort, UnShadow&
End Sub

Public Sub Un_SET_DDR_OUT() 'Set DDR to output
    UnShadow& = UnShadow& Or (UnDDrBit)
    vbapp_Write UnPort, UnShadow&
End Sub

Public Sub Un_SET_DDR_IN() 'Set DDR for input
    UnShadow& = UnShadow& And (Not UnDDrBit)  'Set DDR for input
    vbapp_Write UnPort, UnShadow&
End Sub

Public Function Un_GET_DATA() As Long
    portvalue& = vbapp_Read(UnPort) ' Read Port
    If (portvalue& And UnDinBit) > 0 Then
            Un_GET_DATA = 1
    Else
            Un_GET_DATA = 0
    End If
End Function
Public Function Un_Read_From_Addr(addr As Integer) As Integer
    
    Dim dat As Integer
    Dim er As Integer
    
    Un_Start_seq
    er = er + Un_Write_byte(Un_WRITE_CMD Or &H8)
    er = er + Un_Write_byte(&H0) ' writing the device id
    er = er + Un_Write_byte(addr) ' register address
    Un_Stop_seq

    Un_Start_seq
    er = er + Un_Write_byte(Un_READ_CMD Or &H8)
    er = er + Un_Write_byte(&H0) ' writing the device id
    If er > 0 Then
        'MsgBox "Error"
    End If
    dat = Un_Read_byte(1)
    Un_Stop_seq
    Un_Read_From_Addr = dat
    
End Function

Public Function Un_Write_To_Addr(addr As Integer, dat As Integer) As Integer
Dim er As Integer
Un_Start_seq
er = er + Un_Write_byte(Un_WRITE_CMD Or &H8)
er = er + Un_Write_byte(&H0) ' writing the device id
er = er + Un_Write_byte(addr) ' register address
er = er + Un_Write_byte(dat)
Un_Stop_seq
Un_Write_To_Addr = er
End Function

Public Function Un_GetSerialNumber() As String
Dim temp As Long
Dim str_temp As String
    Un_ClearAddress
    Un_Start_seq
    er = Un_Write_byte(Un_ASN_ADD_CMD Or &H8)
    If er > 0 Then
        'MsgBox "error"
        Un_GetSerialNumber = -1
        Exit Function
    End If
    er = Un_Write_byte(&H0)
    If er > 0 Then
        'MsgBox "error"
        Un_GetSerialNumber = -1
        Exit Function
    End If
    temp = 0
    temp = Un_Read_byte(0)
    str_temp = str_temp & Hex$(temp)
    temp = Un_Read_byte(0)
    str_temp = str_temp & Hex$(temp)
    temp = Un_Read_byte(0)
    str_temp = str_temp & Hex$(temp)
    temp = Un_Read_byte(0)
    str_temp = str_temp & Hex$(temp)
    temp = Un_Read_byte(0)
    str_temp = str_temp & Hex$(temp)
    temp = Un_Read_byte(1)
    str_temp = str_temp & Hex$(temp)

    Un_Stop_seq
    Un_GetSerialNumber = str_temp
End Function


Public Sub Un_ClearAddress()
    Un_Start_seq
    er = Un_Write_byte(Un_CLR_ADD_CMD)
    If er > 0 Then
        'MsgBox "error"
    End If
    er = Un_Write_byte(0)
    If er > 0 Then
        'MsgBox "error"
    End If
    Un_Stop_seq
End Sub
