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

📄 read_write2770.bas

📁 利用VB开发的针对Maxim-Dallas DS2770电量检测芯片
💻 BAS
字号:
Attribute VB_Name = "Read_Write"
Option Explicit
Dim sndbyt(0) As Byte
Public DS2770ROM(8) As Integer
Public NetAddress(8) As Integer
Public BlankROM As Integer

Public AssemblyID As Integer

Public Const REALTIME_ADDRESS   As Byte = &H1
Public Const ACR_ADDRESS        As Byte = &H10
Public Const LMD_ADDRESS        As Byte = &H26
Public Const TAD_ADDRESS        As Byte = &H27

Public Const BLOCK_0_ADDRESS  As Byte = &H20
Public Const BLOCK_1_ADDRESS    As Byte = &H30
Public Const BLOCK_2_ADDRESS    As Byte = &H40

Public Const SRAM_ADDRESS       As Byte = &H80

Sub CopyBytes(StartingAddress As Integer)
Dim wait As Double
    If DS9123.OneWireReset() Then
        SendCommand2770 (CopyData)
        SendData (StartingAddress)
        If DS9123.OneWireReset() Then
        End If
    End If
    
    wait = Timer
    While (Timer - wait) < 0.01
        If Timer > wait Then
            wait = Timer
        End If
    Wend
End Sub

Sub RecallBytes(StartingAddress As Integer)
Dim wait As Double
    If DS9123.OneWireReset() Then
        SendCommand2770 (RecallData)
        SendData (StartingAddress)
        If DS9123.OneWireReset() Then
        End If
    End If
    
    wait = Timer
    While (Timer - wait) < 0.01
        If Timer > wait Then
            wait = Timer
        End If
    Wend
End Sub

Sub LockEEPROMBytes(StartingAddress As Integer)
    ReadBytes 7, 1
    DataIn(7) = DataRead(7) Or &H40
    WriteBytes 7, 1
    
    If DS9123.OneWireReset() Then
        SendCommand2770 (LockEEPROM)
        SendData (StartingAddress)
        If DS9123.OneWireReset() Then
        End If
    End If
End Sub

Sub RefreshStatus()
    If DS9123.OneWireReset() Then
        SendCommand2770 (RefreshCommand)
        If DS9123.OneWireReset() Then
        End If
    End If
End Sub

Sub StopCharging()
    If DS9123.OneWireReset() Then
        SendCommand2770 (StopCharge)
        If DS9123.OneWireReset() Then
        End If
    End If
End Sub

Sub StartCharging()
    If DS9123.OneWireReset() Then
        SendCommand2770 (StartCharge)
        If DS9123.OneWireReset() Then
        End If
    End If
End Sub

Sub WriteBytes(StartingAddress As Integer, BytesToWrite As Integer)
Dim i As Integer
    
    If DS9123.OneWireReset() Then
        SendCommand2770 (WriteData)
        SendData (StartingAddress)
        For i = 0 To (BytesToWrite - 1)
            If Not (DataIn(StartingAddress + i) = "empty") Then
                SendData (DataIn(StartingAddress + i) And &HFF)
            End If
        Next i
        If DS9123.OneWireReset() Then
        End If
    End If
    
End Sub

Sub ReadBytes(StartingAddress As Integer, BytesToRead As Integer)
Dim i As Integer
    If DS9123.OneWireReset() Then
        SendCommand2770 (ReadData)
        SendData (StartingAddress)
        For i = 0 To (BytesToRead - 1)
            DataRead(StartingAddress + i) = ReadByte
        Next i
        If DS9123.OneWireReset() Then
        End If
    End If
    
End Sub

Sub ReadRealTime()
    If DS9123.OneWireReset() Then
        ReadBytes REALTIME_ADDRESS, &H1B
        gStatus = DataRead(1)
        gETRMSB = DataRead(2)
        gETRLSB = DataRead(3)
        gETRLWMSB = DataRead(4)
        gETRLWLSB = DataRead(5)
        
        gCTR = DataRead(6)
        gCTRLSB = DataRead(&H1B)
        
        gEEPROM = DataRead(7)
        
        gVoltMSB = DataRead(12)
        gVoltLSB = DataRead(13)
        gCurrentMSB = DataRead(14)
        gCurrentLSB = DataRead(15)
        gACRMSB = DataRead(16)
        gACRLSB = DataRead(17)
        
        gTempMSB = DataRead(24)
        gTempLSB = DataRead(25)
    End If
End Sub


Sub UpdateACR(NewACR As Single)
Dim Register As Long

    Register = NewACR * SenseResistor / 0.00625
    
    If Register < 0 Then
        Register = Register + 65536
    End If
    
    DataIn(ACR_ADDRESS + 1) = Register And &HFF
    DataIn(ACR_ADDRESS) = ((Register And &HFF00) / &H100) And &HFF
    
    WriteBytes ACR_ADDRESS, 2
    
End Sub

Sub SendData(Send As Byte)
Dim InBuffer() As Byte
Dim wait As Double
    sndbyt(0) = Send
    DS9123.MSComm1.Output = sndbyt
    
    If sndbyt(0) = &HE3 Then
        DS9123.MSComm1.Output = sndbyt
    End If
    
    ' this is to swallow the echo of the ReadROM command
    wait = Timer
    While DS9123.MSComm1.InBufferCount < 1     'Waits for Recieve response
        If ((Timer - wait) > 2) Or (Timer < wait) Then
            Exit Sub
        End If
    Wend
    InBuffer = DS9123.MSComm1.Input
End Sub

Function ReadByte() As Byte
Dim InBuffer() As Byte
Dim wait As Double

    sndbyt(0) = &HFF
    DS9123.MSComm1.Output = sndbyt
    wait = Timer
    While DS9123.MSComm1.InBufferCount < 1     'Waits for Recieve response
        If ((Timer - wait) > 2) Or (Timer < wait) Then
            Exit Function
        End If
    Wend
    InBuffer = DS9123.MSComm1.Input
    ReadByte = InBuffer(0)
End Function

Sub SendCommand2770(Command As Byte)
Dim InBuffer() As Byte
Dim i As Integer
Dim wait As Double
    
    If DS9123.OneWireReset() Then
        If frmEvalKit.SelectedDevice = 0 Then
            DS9123.SkipROM
        Else
            DS9123.MatchROM (DS9123.FoundROMs(NetAddress(frmEvalKit.SelectedDevice)))
        End If
        sndbyt(0) = Command
        DS9123.MSComm1.Output = sndbyt
        
        ' this is to swallow the echo of the ReadROM command
        wait = Timer
        While DS9123.MSComm1.InBufferCount < 1     'Waits for Recieve response
            If ((Timer - wait) > 2) Or (Timer < wait) Then
                Exit Sub
            End If
        Wend
        InBuffer = DS9123.MSComm1.Input
    End If
End Sub

Sub ReadStoredData()
Dim temp As Double
Dim wait As Double
    'Reads Data that is programmed into the DS2770 and formats
    ' the data into the appropriate variables
    ReadBytes BLOCK_0_ADDRESS, 36
    
    temp = DataRead(&H20)
    CC(0) = (temp * 256) + DataRead(&H21)  'Charge Level
    CC(1) = CC(0) + DataRead(&H22)         'Charge Level
    CC(2) = CC(1) + DataRead(&H23)         'Charge Level
    CC(3) = CC(2) + DataRead(&H24)         'Charge Level
    CC(4) = CC(3) + DataRead(&H25)         'Charge Level
    
    LMD = DataRead(&H26) * 10
    
    TADMSB = DataRead(&H27)
    TADLSB = DataRead(&H28)
    
    CB = DataRead(&H29)
    CBT(0) = DataRead(&H2A)
    CBT(1) = DataRead(&H2B)
    CBT(2) = DataRead(&H2C)
    CET(0) = DataRead(&H2D)
    CET(1) = DataRead(&H2E)
    CET(2) = DataRead(&H2F)
    
    If DataRead(&H30) = 0 Then
        SenseResistor = 0.025
        frmEvalKit.txtSenseResistor.Text = Format(SenseResistor * 1000, "#0.00")
        frmEvalKit.lblRsense.Caption = "Sense : Internal"
        frmEvalKit.txtSenseResistor.Visible = False
        frmEvalKit.optInternalResistor.Value = True
    Else
        SenseResistor = DataRead(&H30) / 4000
        frmEvalKit.txtSenseResistor.Text = Format(SenseResistor * 1000, "#0.00")
        frmEvalKit.lblRsense.Caption = "Sense : " & Format(SenseResistor * 1000, "#0.00") & " mOhms"
        frmEvalKit.txtSenseResistor.Visible = True
        frmEvalKit.optExternalResistor.Value = True
    End If
    
    StatusSave = DataRead(&H31)
    'OffsetMSB = DataRead(&H32)
    'OffsetLSB = DataRead(&H33)
    'CTRInit = DataRead(&H34)
    
    DateCode(0) = DataRead(&H35)
    DateCode(1) = DataRead(&H36)
    
    D1(4) = 0           '               'Discharge Efficiency, Rate 1
    D1(3) = DataRead(&H3A)
    D1(2) = D1(3) + DataRead(&H39)
    D1(1) = D1(2) + DataRead(&H38)
    D1(0) = D1(1) + DataRead(&H37)
    
    D2(4) = DataRead(&H3F)             'Discharge Efficiency, Rate 2
    D2(3) = D2(4) + DataRead(&H3E)
    D2(2) = D2(3) + DataRead(&H3D)
    D2(1) = D2(2) + DataRead(&H3C)
    D2(0) = D2(1) + DataRead(&H3B)
    
    AssemblyID = DataRead(&H40)
    RatedCapacity = DataRead(&H41) * 10
    BatteryID = DataRead(&H42) And &HF    '4 bits of Manu ID
    
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'WriteStoredData takes the data that is in the variables CC(x), D1(x),
'   D2(x), manipulates it into Byte form and programs bytes
'   32-47 and 52-58 of the DS2770 with the appropriate data
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub WriteStoredData()
    'Takes the data written to the table on the DS2770Data form and
    ' formats it into bytes and then writes the data tot he DS2770
    PackDate
    
    DataIn(&H20) = ((CC(0) And &HFF00) / &H100) And &HFF
    DataIn(&H21) = CC(0) And &HFF
    DataIn(&H22) = (CC(1) - CC(0)) And &HFF
    DataIn(&H23) = (CC(2) - CC(1)) And &HFF
    DataIn(&H24) = (CC(3) - CC(2)) And &HFF
    DataIn(&H25) = (CC(4) - CC(3)) And &HFF
    
    DataIn(&H26) = (LMD / 10) And &HFF 'stored in 10's of Ahrs
    
    DataIn(&H27) = TADMSB And &HFF
    DataIn(&H28) = TADLSB And &HFF
    
    DataIn(&H29) = CB And &HFF
    
    DataIn(&H2A) = CBT(0) And &HFF
    DataIn(&H2B) = CBT(1) And &HFF
    DataIn(&H2C) = CBT(2) And &HFF
    
    DataIn(&H2D) = CET(0) And &HFF
    DataIn(&H2E) = CET(1) And &HFF
    DataIn(&H2F) = CET(2) And &HFF
    
    WriteBytes BLOCK_0_ADDRESS, 16
    
    ReadBytes BLOCK_1_ADDRESS, 5
    
    DataIn(&H30) = DataRead(&H30) And &HFF  'Sense Resistor
    DataIn(&H31) = StatusSave And &HFF
    DataIn(&H32) = DataRead(&H32) And &HFF  'offsetMSB
    DataIn(&H33) = DataRead(&H33) And &HFF  'offsetLSB
    DataIn(&H34) = DataRead(&H34) And &HFF  'CTR Init
    
    DataIn(&H35) = DateCode(0) And &HFF
    DataIn(&H36) = DateCode(1) And &HFF
    
    DataIn(&H37) = (D1(0) - D1(1)) And &HFF
    DataIn(&H38) = (D1(1) - D1(2)) And &HFF
    DataIn(&H39) = (D1(2) - D1(3)) And &HFF
    DataIn(&H3A) = D1(3) And &HFF
    
    DataIn(&H3B) = (D2(0) - D2(1)) And &HFF
    DataIn(&H3C) = (D2(1) - D2(2)) And &HFF
    DataIn(&H3D) = (D2(2) - D2(3)) And &HFF
    DataIn(&H3E) = (D2(3) - D2(4)) And &HFF
    DataIn(&H3F) = D2(4) And &HFF
    
    WriteBytes BLOCK_1_ADDRESS, 16
    
    DataIn(&H40) = (AssemblyID) And &HFF
    DataIn(&H41) = (RatedCapacity / 10) And &HFF 'stored in 10's of Ahrs
    DataIn(&H42) = (BatteryID) And &HFF
    
    WriteBytes BLOCK_2_ADDRESS, 3
    
    
End Sub

Public Sub PackDate()
Dim tempdate As Long
Dim tempday As Long
Dim tempmonth As Long
Dim tempyear As Long

    tempday = Val(frmEvalKit.txtDay.Text)
    If tempday > 31 Then
        tempday = 31
    ElseIf tempday < 1 Then
        tempday = 1
    End If
    
    tempmonth = Val(frmEvalKit.txtMonth.Text)
    If tempmonth > 12 Then
        tempmonth = 12
    ElseIf tempmonth < 1 Then
        tempmonth = 1
    End If
    
    tempyear = Val(frmEvalKit.txtYear.Text) - 1980
    If tempyear > 127 Then
        tempyear = 127
    ElseIf tempyear < 0 Then
        tempyear = 0
    End If
    
    tempdate = tempday + (tempmonth * 32) + (tempyear * 512)
    DateCode(0) = tempdate And &HFF
    DateCode(1) = (tempdate And &HFF00) / &H100
End Sub

Public Sub UnpackDate()
Dim temp As Long
Dim tempdate As Long
Dim tempday As Long
Dim tempmonth As Long
Dim tempyear As Long
    temp = DateCode(1)
    tempdate = DateCode(0) + (temp * 256)
    
    tempday = tempdate And &H1F
    tempmonth = (tempdate And &H1E0) / &H20
    tempyear = (tempdate And &HFE00) / &H200
    
    frmEvalKit.txtDay.Text = tempday
    frmEvalKit.txtMonth.Text = tempmonth
    frmEvalKit.txtYear.Text = tempyear + 1980
End Sub

⌨️ 快捷键说明

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