⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds9123.frm

📁 利用VB开发的针对Maxim-Dallas DS2770电量检测芯片
💻 FRM
📖 第 1 页 / 共 3 页
字号:
        SendData (&HFF)    'Channel Control Byte 2
        ReadData           'Dummy Read for Channel Info Byte
        Access2Wire = True
    End If
End Function

' Start2Wire
'
' Issues a START condition on the 2-wire bus.
' SCL stays high, SDA goes low. PIO-A = SDA PIO-B = SCL
'
' Returns: nothing
'
Sub Start2Wire()
        
        SendData (&HFF)    'Makes sure bus is in idle condition
        ReadData
        
        SendData (&HA)     'drop SDA, then SCL
        ReadData
End Sub

' Stop2Wire
'
' Issues a STOP condition on the 2-wire bus.
' SCL goes low to high, then SDA goes from low to high.
'
' Returns: nothing
'
Sub Stop2Wire()
    
    SendData (&HE8)     'SDA low, SCL low
                        'SCL goes high, then SDA
    ReadData
    SendData (&HFF)     'Makes sure bus is in idle condition
    ReadData
    OWR = OneWireReset()
End Sub


' SendByte_2Wire
'
' Sends a byte on the 2-wire bus, MSB first.
'
' Returns: True if acknowledged
'
Function SendByte_2Wire(data As Byte) As Boolean
Dim temp As Byte
Dim i As Integer
Dim mask As Byte
Dim tData(1 To 4) As Byte
   
   'Each time Two wire is sent it is send in the form
   'MSbit (  clkbit(low) databit clkbit(hi) databit clkbit(low) databit clkbit(hi) databit )
    
    mask = &H80 'Set to pick off MSbit, and will be decremented
    For i = 1 To 4 Step 1
        tData(i) = &H22         'Sets clock into low,high,low,high format
        temp = data And mask    'picks off MSbit in first loop,then 3rd,5th,7th
        If (temp) Then
            tData(i) = tData(i) Or &H5  'Sets lower half of byte high, this make that data high
        End If
        mask = mask / 2         'moves mask to pick off next bit
        temp = data And mask    'picks off 2nd MSbit in first loop,then 4th,6th,8th (or LSbit)
        If (temp) Then
            tData(i) = tData(i) Or &H50 'Sets upper half of byte high, this makes the data high
        End If
        mask = mask / 2         'moves mask to pick off next bit
        SendData (tData(i))     'Sends two bits of data at a time
        ReadData                'Reads Data back to swallow echo
    Next i
  
    SendData (&HD5) 'Sends first part of ack clock
    
    If (ReadData And &H1) = 0 Then 'If  ack, then
        SendByte_2Wire = True
    Else
        SendByte_2Wire = False
    End If
    
    SendData (&H57) 'Finishes Ack clock
    ReadData
End Function

' ReadByte_2Wire
'
' Reads a byte from the 2-wire bus.
'
' Returns: Byte
'
Function ReadByte_2Wire(ack As Boolean) As Byte
Dim temp As Byte
Dim i As Integer
Dim DummyBuff() As Byte

    temp = 0
    
    For i = 1 To 8 Step 1
        temp = temp * 2 'left shift
        SendData (&H55) 'SCL = 0
        ReadData        'Dummy Read
        SendData (&HFF) 'SCL = 1
            
        temp = temp Or (ReadData And &H1) 'Reads one bit at a time
    Next i
    
    ' following handles clocking the ACK
    If ack = True Then
        SendData (&H55)  'Acks
        ReadData
        SendData (&H0)
        ReadData
        SendData (&HAA)
        ReadData
        SendData (&H0)
        ReadData
        'SendData (&H55)
        'ReadData
    Else
        SendData (&H55) 'Nacks
        ReadData
        SendData (&HFF)
        ReadData
        SendData (&H55)
        ReadData
    End If
    
    ReadByte_2Wire = temp
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This section contains all the commands that support 3-wire protocol.
' This assumes the use of a DS2480.
'
'
'

'AccessGP2
'
' Selects DS2407 which handles /RST and GPIO
'   Sets up the part to read the current state, and only change
'       one line, leaving the other in the existing state
' Returns: True if part found, False if not.
'
Function AccessGP1() As Boolean
    AccessGP1 = False
    
    If (OneWireReset()) Then
        MatchROM (FoundROMs(ROMGPIO))

        SendData (&HF5)    'channel access command
        SendData (&H6C)    'Channel Control Byte 1
        SendData (&HFF)    'Channel Control Byte 2
        ReadData           'Dummy Read for Channel Info Byte
        AccessGP1 = True
    End If
End Function

' Access3Wire
'
' Selects the DS2407 which handles /RST and GPIO
'
' Returns: True if part found, False if not.
'
Function Access3Wire() As Boolean
    Access3Wire = False
    If (OneWireReset()) Then
        MatchROM (FoundROMs(ROMGPIO))
  
        SendData (&HF5)    'channel access command
        SendData (&H2C)    'Channel Control Byte 1
        SendData (&HFF)    'Channel Control Byte 2
        ReadData           'Dummy Read for Channel Info Byte
        Access3Wire = True
    End If
End Function

' RSTHigh
'
' Raises /RST., puts CLK in a low state
'
' Returns: nothing
'
Sub RSTHigh()

    SendData (&HFF)    'RST high, GPIO high
    ReadData
        
    If (Access2Wire()) Then
        SendData (&H55) 'makes sure CLK is low
        ReadData
    End If
End Sub

' RSTLow
'
' Drops /RST., sets CLK and DQ high
'
' Returns: nothing
'
Sub RSTLow()
        
    SendData (&HAA)     'RST low, GPIO high
    ReadData

    If (Access2Wire()) Then
        SendData (&HFF) 'return CLK and DQ high
        ReadData
    End If
    OWR = OneWireReset()
End Sub

'GPIOHigh
'
'Sets GPIO High and leaves RST in the state that it was found
'
'Returns nothing
'
Sub GPIOHigh()
Dim ExistingValue As Byte
Dim DataToWrite As Byte

    ExistingValue = ReadData And &H55 'Picks off RST value
    DataToWrite = ExistingValue Or &HAA 'Sets GPIO High, leaves RST alone
    SendData (DataToWrite)
End Sub

'GPIOLow
'
'Sets GPIO Low and leaves RST in the state that it was found
'
'Returns nothing
'
Sub GPIOLow()
Dim ExistingValue As Byte
Dim DataToWrite As Byte

    ExistingValue = ReadData And &H55 'Picks off RST value
    DataToWrite = ExistingValue Or &H0 'Sets GPIO low, leaves RST alone
    SendData (DataToWrite)
End Sub

'ResetHigh
'
'Sets ResetHigh and leaves GPIO in the state that it was found
'
'Returns nothing
'
Sub ResetHigh()
Dim ExistingValue As Byte
Dim DataToWrite As Byte

    ExistingValue = ReadData And &HAA 'Picks off GPIO value
    DataToWrite = ExistingValue Or &H55 'Sets RST High, leaves GPIO alone
    SendData (DataToWrite)
End Sub

'ResetLow
'
'Sets Reset Low and leaves GPIO in the state that it was found
'
'Returns nothing
'
Sub ResetLow()
Dim ExistingValue As Byte
Dim DataToWrite As Byte

    ExistingValue = ReadData And &HAA 'Picks off GPIO value
    DataToWrite = ExistingValue Or &H0 'Sets RST low, leaves GPIO alone
    SendData (DataToWrite)
End Sub

' Mirror
'
' Flips the bits in a Byte so that LSB is MSB and MSB is LSB.
'
' Returns: Mirrored Byte
'
Function Mirror(data As Byte) As Byte
Dim temp As Byte
Dim i As Integer
Dim mask As Byte
Dim Swap As Byte
Dim MirrorData As Byte
   
    For i = 0 To 7
        mask = 2 ^ (i)
        Swap = 2 ^ (7 - i)
        temp = ((data And mask) / mask) * Swap
        MirrorData = MirrorData + temp
    Next i
    
    Mirror = MirrorData
End Function

' SendByte_3Wire
'
' Sends a byte on the 3-wire bus, LSB first.
'
' Returns: nothing
'
Sub SendByte_3Wire(data As Byte)
Dim temp As Byte
Dim i As Integer
Dim mask As Byte
Dim tData(1 To 4) As Byte
     
    mask = &H1  'Set to pick off LSbit, and will be incremented
    For i = 1 To 4 Step 1
        tData(i) = &H22         'Sets clock into low,high,low,high format
        temp = data And mask    'picks off LSbit in first loop,then 3rd,5th,7th
        If (temp) Then
            tData(i) = tData(i) Or &H5  'Sets lower half of byte high, this make that data high
        End If
        mask = mask * 2         'moves mask to pick off next bit
        temp = data And mask    'picks off 2nd LSbit in first loop,then 4th,6th,8th (or MSbit)
        If (temp) Then
            tData(i) = tData(i) Or &H50 'Sets upper half of byte high, this makes the data high
        End If
        If i < 4 Then
            mask = mask * 2     'moves mask to pick off next bit
        End If
        SendData (tData(i))     'Sends two bits of data at a time
        ReadData                'Reads Data back to swallow echo
    Next i

End Sub

' ReadByte_3Wire
'
' Reads a byte from the 3-wire bus.
'
' Returns: Byte
'
Function ReadByte_3Wire() As Byte
Dim temp As Byte
Dim i As Integer
Dim DummyBuff() As Byte

    temp = 0
    For i = 1 To 8 Step 1
        temp = temp / 2 'right shift
        SendData (&H55) 'CLK = 0
        If (ReadData() And &H40) = &H40 Then
            temp = temp Or &H80
        End If
        SendData (&HFF) 'CLK = 1
        ReadData        'Dummy Read

    Next i
   
    ReadByte_3Wire = temp
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'This section provides for the setting of a prefered Comm Port and speed
'
'
Sub OpenPref()

    PrefCommPort = Int(GetSetting("DS9123", "Serial", "Port", "0"))
    PrefBaudRate = Int(GetSetting("DS9123", "Serial", "Baud", "0"))
    
    If ((PrefCommPort = 0) Or (PrefBaudRate = 0)) Then
        PrefBaudRate = 19200
        DS9123PrefChoose.Show vbModal, Me
        'PollSerialPorts
    End If
    
End Sub

Sub SavePref()
Dim SaveBaud As String
Dim SavePort As String

    SavePort = PrefCommPort
    SaveBaud = PrefBaudRate
    
    SaveSetting "DS9123", "Serial", "Port", SavePort
    SaveSetting "DS9123", "Serial", "Baud", SaveBaud
    
End Sub

Sub PollSerialPorts()
Dim Port As Integer
Dim StartPort As Integer
    On Error GoTo PORTERROR
    StartPort = 10
    PortFound = False
TOPPORT:

    For Port = StartPort To 1 Step -1
        'Close Port if it is open
        If MSComm1.PortOpen = True Then MSComm1.PortOpen = False

        'Sets Port
        If Not (MSComm1.CommPort = Port) Then
            MSComm1.CommPort = Port
        End If
        
        'Open Port if it is closed
        If MSComm1.PortOpen = False Then MSComm1.PortOpen = True

        'Talk to DS9123
        Reset2480

        If MSComm1.PortOpen Then    'If Port is open
'            sndbyt(0) = &HC1
'            MSComm1.Output = sndbyt 'Initialize communication
'            MSComm1.Output = Hex$(&HC1) 'Initialize communication
           ' Debug.Print Chr(&HC1); "_2"
           ' Debug.Print CStr(&HC1); "_2"
            If OneWireReset() Then  'Send a Reset, if get a response, then have found Port with DS9123 connected
                PrefCommPort = Port
                PortFound = True
                Exit For
            Else
                If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
            End If
        End If
    Next Port

    If PortFound = True Then    'If a port was found with adapter, then display prefchoose form to confirm what was found
        'DS9123PrefChoose.Show vbModal, Me
        AdapterError = False    'Retrun No Error
    Else
        AdapterError = True     'Return that there was an error
    End If

    Exit Sub

PORTERROR:
    Err.Clear
    If MSComm1.PortOpen = True Then MSComm1.PortOpen = False    'If port is open, then close it
    StartPort = Port - 1    'Go to the next Port
    Resume TOPPORT          'Start over
End Sub

Private Sub mnuExit_Click()
    Unload Me
    End
End Sub

Private Sub mnuPreferences_Click()
    DS9123PrefChoose.Show
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -