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

📄 form1.frm

📁 51单片机工程应用实例
💻 FRM
📖 第 1 页 / 共 2 页
字号:
        '.Filter = "Intel Hex文件 (*.hex)|*.hex|Binary文件(*.bin)|*.bin"
        .Filter = "Intel Hex文件 (*.hex)|*.hex"
        .ShowOpen
        If Len(.FileName) = 0 Then
            Exit Sub
        End If
        name = .FileName
    End With
    ' 处理打开的文件
    filehandle% = FreeFile
    Open name For Input As #filehandle%
    Do While Not EOF(filehandle%)
    Line Input #filehandle%, strLine '输入文件中的一行
    strLen = Mid(strLine, 2, 2)   '行长度
    strAdd = Mid(strLine, 4, 4)   '地址
    strType = Mid(strLine, 8, 2)  '数据类型
    strData = Mid(strLine, 10, (Val("&H" + strLen)) * 2) '
    If Val("&H" + strAdd) > FileAddMax Then
    FileAddMax = Val("&H" + strAdd)
    lenMax = Val("&H" + strLen)
    lastads = FileAddMax + lenMax - 1
    End If
  
    If Val("&H" + strType) = 0 Then
        For I = 0 To Val("&H" + strLen) - 1
        strByte = Mid(strData, I * 2 + 1, 2)
        strArray(Val("&H" + strAdd) + I) = strByte '字节放入数组
        Next I
    End If
    strLine = ""
    Loop
    Close #filehandle%

    strData = ""
    sfile = ""
    J = 0
    Do While (I <= lastads)
    strLine = ""
    line = ""
    For I = J To J + 15
    strLine = strLine + strArray(I) + " " '每16个字节为一行
    line = line + strArray(I)
    Next I
   
    s_add = zero(J)                      '地址前加无效零
    strLine = " " & s_add & "  " & strLine '一行数据
    strData = strData & strLine & vbCrLf '用于显示的文件内容
    sfile = sfile & line                 '用于写入和校对的文件内容
    J = J + 16
    Loop
    Text1.Text = name & vbCrLf & "文件末地址:" & Hex(lastads) '文件信息
    Text2.Text = strData '送文本框显示
    strData = ""
    Dim intAdd As Integer
   
    strAdd = Hex(lastads) '文件末地址
    intAdd = Len(strAdd)
    If intAdd = 2 Then
    strAddH = "00"
    ElseIf intAdd = 3 Then
    strAddH = "0" + Left(strAdd, 1)
    Else
    strAddH = Left(strAdd, 2)
    End If
    strAddL = Right(strAdd, 2)
    Command7.Enabled = True
    Command8.Enabled = True

End Sub

Private Sub Command2_Click()     '存盘,把Flash中的数据存成Hex文件 OK!
    Dim sfile As String
    Dim Hexstr As String
     
    With CommonDialog1
        .DialogTitle = "存储读出的Flash数据为Hex文件"
        .CancelError = False
        .Filter = "Intel Hex文件 (*.hex)|*.hex|Binary文件(*.bin)|*.bin"
        .ShowSave
        If Len(.FileName) = 0 Then
            Exit Sub
        End If
        sfile = .FileName
    End With
    ' 处理打开的文件
    filehandle% = FreeFile
    Open sfile For Output As #filehandle%
    Hexstr = flash_HexFile(flash)   '把Flash中的数据字符串转变成Hex格式文件
    Print #filehandle%, Hexstr
    Close #filehandle%
    flash = ""
    Hexstr = ""
    MsgBox "存盘完成"
End Sub

Private Sub Command3_Click()     '搜索,命令A1 OK!

    ProgressBar1.Visible = True
    ProgressBar1.Min = 0
    ProgressBar1.Max = 1000
    Send ("A1")

    For I = 0 To 1000
    ProgressBar1.Value = I
    Next I
    rcmd = ""
    rcmd = Reci(1)
    Select Case rcmd

    Case "01"
    chip = "01"
    Text1.Text = "芯片是89C2051"
    chiplen = &H7FF       '2K=2048,07FF
    Case "03"
    chip = "03"
    Text1.Text = "芯片是89C51"
    chiplen = &HFFF       '4K=4096,0FFF
    Case "04"
    chip = "04"
    Text1.Text = "芯片是89C52"
    chiplen = &H1FFF      '8K=8192,1FFF

    End Select
    ProgressBar1.Visible = False

End Sub

Private Sub Command4_Click() '读闪存数据,每次读出16个字节,接收时无需延时。OK!
    Dim rstr As String
    Dim line As String
    Dim Ad As String
    Dim Dd As String

    Dim m As Integer
    Dim n As Integer
    flash = ""
    rstr = ""
    All = ""
    line = ""
    Text2.Text = ""
    n = (chiplen + 1) / 16
    ProgressBar1.Visible = True
    ProgressBar1.Min = 0
    ProgressBar1.Max = n - 1
    Send ("A4")
    For J = 0 To n - 1
        rstr = Reci(16) '收一行数据
        Send ("A0") '回答
        flash = flash + rstr '用于存盘的闪存数据
        Dd = ""
        For I = 0 To 15
            Dd = Dd + Mid(rstr, I * 2 + 1, 2) + " " '字节间加空格
        Next I
        m = J * 16
        Ad = zero(m) '地址前加无效零
        line = " " + Ad + " " + Dd + vbCrLf '显示格式:空格+地址+空格+数据
        All = All + line
        ProgressBar1.Value = J
    Next J
  
    Text2.Text = All '送文本框显示
    Text1.Text = "读出完成"
    ProgressBar1.Visible = False
    Command2.Enabled = True
End Sub

Private Sub Command5_Click()     '擦除,命令A3,2051,51,52 ALL OK!
    ProgressBar1.Visible = True
    ProgressBar1.Min = 0
    ProgressBar1.Max = 1000
    Send ("A3")
    For I = 0 To 1000
    ProgressBar1.Value = I
    Next I
    rcmd = ""
    rcmd = Reci(1)
    If rcmd = "A3" Then
    Text1.Text = "擦除完成"

    End If
    ProgressBar1.Visible = False

End Sub

Private Sub Command6_Click()     '空检查,命令A2 OK!
    ProgressBar1.Visible = True
    ProgressBar1.Min = 0
    ProgressBar1.Max = 1000
    Send ("A2")
    For I = 0 To 1000
    ProgressBar1.Value = I
    Next I

    rcmd = ""
    rcmd = Reci(1)

    If rcmd = "A2" Then
    Text1.Text = "芯片是空的"
    ElseIf rcmd = "AA" Then
    Text1.Text = "芯片不是空的"
    End If
    ProgressBar1.Visible = False
End Sub

Private Sub Command7_Click()     '编程,命令A6 只按文件长度写入,OK!
    Dim rstr As String '收数据串
    Dim str As String

    ProgressBar1.Visible = True
    ProgressBar1.Min = 0
    ProgressBar1.Max = lastads
    rcmd = ""
    Send ("A6")
    rcmd = Reci(1)
   
    Send (strAddH)
    rcmd = Reci(1)
     
    Send (strAddL)
    rcmd = Reci(1)
     
    For I = 0 To lastads
      str = Mid(sfile, I * 2 + 1, 2)
      Send (str)
      rcmd = Reci(1)
      ProgressBar1.Value = I
    Next I
    
    Text1.Text = "编程完成"
    ProgressBar1.Visible = False
End Sub

Private Sub Command8_Click()     '校验A5H,OK!
    Dim rstr As String
    Dim sf As String '文件中取2个字符
    sf = ""
    rstr = ""
  
    ProgressBar1.Visible = True
    ProgressBar1.Min = 0
    ProgressBar1.Max = lastads
    Send ("A5")
    rcmd = Reci(1)
   
    Send (strAddH)
    rcmd = Reci(1)
    
    Send (strAddL)
    rcmd = Reci(1)

    For J = 0 To lastads
        rstr = Reci(1)
        Send ("A0")
        sf = Mid(sfile, J * 2 + 1, 2)
        If sf <> rstr Then
            MsgBox "校对错误"
            Exit Sub
        End If
        ProgressBar1.Value = J
    Next J
 
    Text1.Text = "校对完成"
    ProgressBar1.Visible = False
      
End Sub

Private Sub Command9_Click() '清除窗口 OK!
    Text1.Text = ""
    Text2.Text = ""

End Sub

Private Sub Command10_Click() '退出
    
    '程序退出关闭设备
   Unload Form1
End Sub

Private Sub Command11_Click() 'LK1 OK!
   ProgressBar1.Visible = True
   ProgressBar1.Min = 0
   ProgressBar1.Max = 1000
   Send ("A7")
   For I = 0 To 1000
   ProgressBar1.Value = I
   Next I
   rcmd = ""
   rcmd = Reci(1)
   If rcmd = "A7" Then
   Text1.Text = "LK1完成"
   End If
   ProgressBar1.Visible = False
End Sub

Private Sub Command12_Click() 'LK2 OK!
   ProgressBar1.Visible = True
   ProgressBar1.Min = 0
   ProgressBar1.Max = 1000
   Send ("A8")
   For I = 0 To 1000
   ProgressBar1.Value = I
   Next I
   rcmd = ""
   rcmd = Reci(1)
   If rcmd = "A8" Then
   Text1.Text = "LK2完成"

   End If
   ProgressBar1.Visible = False

End Sub

Private Sub Command13_Click() 'LK3 OK!
   ProgressBar1.Visible = True
   ProgressBar1.Min = 0
   ProgressBar1.Max = 1000
   Send ("A9")
   For I = 0 To 1000
   ProgressBar1.Value = I
   Next I
   rcmd = ""
   rcmd = Reci(1)
   If rcmd = "A9" Then
   Text1.Text = "LK3完成"

   End If
   ProgressBar1.Visible = False

End Sub

Public Function chipinf(c As String) '单片机信息 OK!
    Select Case c

        Case "89C2051"
        chip = "01"
        chiplen = &H7FF       '2K=2048,07FF

        Case "89C51"
        chip = "03"
        chiplen = &HFFF       '4K=4096,0FFF

        Case "89C52"
        chip = "04"
        chiplen = &H1FFF      '8K=8192,1FFF

    End Select

End Function

Function zero(x As Integer) As String '地址前加无效零 OK!
    Dim ix As Integer
    Dim s1 As String
    ix = x
    If ix < 16 Then
        s1 = "000" + CStr(Hex(ix))
    ElseIf ix >= 16 And ix < 256 Then
        s1 = "00" + CStr(Hex(ix))
    ElseIf ix >= 256 And ix < 4096 Then
        s1 = "0" + CStr(Hex(ix))
    Else
        s1 = CStr(Hex(ix))
    End If
    zero = s1

End Function

Private Function flash_HexFile(sf As String) As String 'OK!
'将数据变为HEX文件格式
'Hex文件格式:nnaaaa00ddddddddddddddddddddddddddddddddcc
'nn数据字节长度
'aaaa行地址
'00数据类型
'dddd....dddd数据共32个16个字节
'cc校验和
    Dim la As Integer
    Dim ld As String
    Dim lad As String

    Dim num As Integer
    Dim Isum As Integer
    Dim Ick As Integer
    Dim Hck As String
    Dim HexLine As String
    Dim HexAll As String

    HexAll = ""
    num = (chiplen + 1) / 16 '行数
    For I = 0 To num - 1
        la = I * 16     '行地址
        lad = zero(la)  '地址前加无效零
        ld = Mid(sf, I * 32 + 1, 32)      '数据
        HexLine = ""
        Isum = 0
        For J = 0 To 15
            Isum = Isum + Val("&H" + Mid(ld, J * 2 + 1, 2)) '16字节校验和
        Next J
        Isum = Isum + 16 + la   '一行的校验和
        Isum = Isum Mod 256
        Ick = 256 - Isum '补码
        If Ick = 256 Then
            Hck = "00"
        ElseIf Ick < 16 Then
            Hck = "0" & Hex(Ick)
        Else
            Hck = Hex(Ick)
        End If
        HexLine = ":" & "10" & lad & "00" & ld & Hck '一行完整的数据
        HexAll = HexAll + HexLine + vbCrLf
    Next I
 
    HexAll = HexAll + ":00000001FF"
    flash_HexFile = HexAll

End Function

⌨️ 快捷键说明

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