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

📄 aduc702x_i2c_downloader.frm

📁 ADuC7026_I2C_downloader是ADuC7026 ARM 单片机的I2C接口的烧写程序
💻 FRM
📖 第 1 页 / 共 3 页
字号:
 Next
 
 Open Dir For Input As 1
 
 OK = True
 NumOfLines = 0
 Do While Not EOF(1)
  Line Input #1, FileLine
  NumOfLines = NumOfLines + 1
  If (Check_HEX_FileLine(FileLine) = False) Then
   OK = False
   Close #1
   GoTo FileError
  End If
  Loop
  
 Close #1
 
FileError:
 If OK = False Then
  MsgBox "错误的HEX文件,打开文件失败。", vbOKOnly + vbCritical, "错误"
  NumOfLines = 0
 End If
 Check_HEX_File = NumOfLines
 Exit Function
 
Exit_sub:
  MsgBox "文件不存在。", vbOKOnly + vbCritical, "打开文件错误"
End Function

Private Function WriteOneLine(ByRef DataLine As String) As Long
 
 Dim HEX_Value(0 To 50) As Byte
 Dim HEX_Char(0 To 100) As Byte
 Dim TempAddr As Long
 Dim i As Long
 Dim n As Long
   
 n = Len(DataLine)
 
 For i = 0 To n - 1
  HEX_Char(i) = Asc(Mid(DataLine, i + 1, 1))
 Next
 
 For i = 1 To n - 1 Step 2
  If (HEX_Char(i) - 48) > 9 Then
   HEX_Value((i - 1) / 2) = 16 * (HEX_Char(i) - 65 + 10)
  Else
   HEX_Value((i - 1) / 2) = 16 * (HEX_Char(i) - 48)
  End If
  If (HEX_Char(i + 1) - 48) > 9 Then
   HEX_Value((i - 1) / 2) = HEX_Value((i - 1) / 2) + (HEX_Char(i + 1) - 65 + 10)
  Else
   HEX_Value((i - 1) / 2) = HEX_Value((i - 1) / 2) + (HEX_Char(i + 1) - 48)
  End If
 Next i
 
 n = HEX_Value(0) 'n bytes
 
 If n = 0 Then
  If HEX_Value(3) = 1 Then FileEnd = True
  WriteOneLine = &HFF00
  Exit Function 'If n is 0,exit
 End If
 
 If HEX_Value(3) = 0 Then 'Data line
 
  If (n And 1) = 1 Then
   n = n + 1
   HEX_Value(n + 3) = &HFF
  End If
  
  
  DataBuffer(0) = &H7
  DataBuffer(1) = &HE
  DataBuffer(2) = 5 + n
  DataBuffer(3) = Asc("W")
  DataBuffer(4) = 0
  DataBuffer(5) = 0
  DataBuffer(6) = HEX_Value(1)
  DataBuffer(7) = HEX_Value(2)
 
  TempAddr = DataBuffer(6)
  TempAddr = TempAddr * 256 + DataBuffer(7)
  
  For i = 0 To n - 1
   DataBuffer(i + 8) = HEX_Value(i + 4)
   'DataBuffer(i + 8) = &HFF
   If (TempAddr = &H14) Or (TempAddr = &H15) Or (TempAddr = &H16) Or (TempAddr = &H17) Then
    DataBuffer(i + 8) = &HFF
   End If
   TempAddr = TempAddr + 1
  Next i
  
  Cal_Sum DataBuffer, DataBuffer(2) + 3
  
  WritePacket &H4, DataBuffer, DataBuffer(2) + 4
  
  Delay  '延迟。等待数据返回
  Delay
  Delay
  Delay
  
  ReadData &H4, DataBuffer, 1
  
  WriteOneLine = DataBuffer(0)
  
 Else
  WriteOneLine = &HFF00
  If HEX_Value(3) = 1 Then 'File end
   FileEnd = True
  End If
 End If
 Exit Function
 
Exit_sub:
 Error = True
End Function

Private Function WriteOneFile(ByVal Dir As String, ByVal NumOfLines As Long) As Long
 On Error GoTo Exit_sub
 Dim FileLine As String
 Dim OK As Boolean
 Dim NumOfCurrentLine As Long
 Dim Result As Long
 
 StatusBar1.Panels("status").Text = "Downloading..."
 
 Open Dir For Input As 1
 
 OK = True
 
 NumOfCurrentLine = 0
 FileEnd = False
 Do While Not EOF(1)
  Line Input #1, FileLine
  NumOfCurrentLine = NumOfCurrentLine + 1
  
  Result = WriteOneLine(FileLine)
  
  If FileEnd = True Then GoTo Exit_do
  
  If (Result <> &HFF00) Then
   If (Result <> 6) Then
    OK = False
    MsgBox "设备应答错误,下载被取消。" & vbCrLf & "下载至HEX文件行:" & NumOfCurrentLine, vbOKOnly + vbCritical, "下载出错"
    WriteOneFile = 0
    Close #1
    StatusBar1.Panels("status").Text = "Downloading failed"
    Exit Function
   End If
  End If
  
  ProgressBar1.Value = NumOfCurrentLine * 100 / NumOfLines
  
 Loop
 
Exit_do:

 Close #1
 
 WriteOneFile = 1
 StatusBar1.Panels("status").Text = "Downloading OK"
 
 Exit Function

Exit_sub:
  MsgBox "文件不存在。", vbOKOnly + vbCritical, "打开文件错误"
End Function


Private Sub LPT1_Click()
 Combo1.Text = "378H(LPT1)"
 Combo1_Click
End Sub

Private Sub LPT2_Click()
 Combo1.Text = "278H(LPT2)"
 Combo1_Click
End Sub

Private Sub LPT3_Click()
 Combo1.Text = "3BCH(LPT3)"
 Combo1_Click
End Sub

Private Sub Open_Click()
 OpenFile_Click
End Sub

Private Sub OpenFile_Click()
 On Error GoTo Exit_sub
 
 Dim File_Dir As String

 CommonDialog1.CancelError = True
 
 CommonDialog1.FileName = FileDir.Text
 CommonDialog1.Filter = "HEX文件(*.hex)|*.hex"
 CommonDialog1.DialogTitle = "装载文件"
 CommonDialog1.ShowOpen
 File_Dir = CommonDialog1.FileName

 If CommonDialog1.FileName = "" Then Exit Sub
 
 FileDir.Text = File_Dir
 Check_HEX_File (File_Dir)

 Exit Sub
 
Exit_sub:
If Err.Number = cdlCancel Then
 Exit Sub
 
Else
 MsgBox "打开文件时发生错误", vbOKOnly + vbCritical, "错误"
End If
End Sub

Private Sub ReadID_Click()
 Dim NumOfLines As Long
 
 DownLoad.Enabled = False
 OpenFile.Enabled = False
 Run.Enabled = False
 ReadID.Enabled = False
 Verify.Enabled = False
 
 StatusBar1.Panels("status").Text = "Check chip ID"
 DelayTime = 10 * (10002 - Speed.Value)
 
 If (CheckChipID() = True) Then
  Status.Caption = "芯片ID:" & ChipID
 Else
  Status.Caption = "读取芯片ID错误。请按开发板上的复位键后再试。"
  GoTo Exit_sub
 End If
 
Exit_sub:
 DownLoad.Enabled = True
 OpenFile.Enabled = True
 Run.Enabled = True
 ReadID.Enabled = True
 Verify.Enabled = True
End Sub

Private Sub Run_Click()
 
StatusBar1.Panels("status").Text = "Run..."
If ChipOK = True Then
 DelayTime = 10 * (10002 - Speed.Value)
 
 Dim Address(0 To 3) As Byte
 Address(0) = 0
 Address(1) = 0
 Address(2) = 0
 Address(3) = 0
 If (RunTo(Address) = 6) Then
  StatusBar1.Panels("status").Text = "Run OK"
  Status.Caption = "程序运行成功!"
 Else
  StatusBar1.Panels("status").Text = "Run failed"
  Status.Caption = "程序运行失败!"
 End If
Else
 MsgBox "请先读芯片ID", vbOKOnly + vbInformation, "芯片ID错误"
 StatusBar1.Panels("status").Text = "Run failed"
End If
End Sub

Private Sub Timer1_Timer()
 StatusBar1.Panels("time").Text = Time
End Sub

Private Function VerifyOneLine(ByRef DataLine As String) As Long
 
 Dim HEX_Value(0 To 50) As Byte
 Dim HEX_Char(0 To 100) As Byte
 Dim TempAddr As Long
 Dim TempData As Long
 Dim i As Long
 Dim n As Long
  
 n = Len(DataLine)
 
 For i = 0 To n - 1
  HEX_Char(i) = Asc(Mid(DataLine, i + 1, 1))
 Next
 
 For i = 1 To n - 1 Step 2
  If (HEX_Char(i) - 48) > 9 Then
   HEX_Value((i - 1) / 2) = 16 * (HEX_Char(i) - 65 + 10)
  Else
   HEX_Value((i - 1) / 2) = 16 * (HEX_Char(i) - 48)
  End If
  If (HEX_Char(i + 1) - 48) > 9 Then
   HEX_Value((i - 1) / 2) = HEX_Value((i - 1) / 2) + (HEX_Char(i + 1) - 65 + 10)
  Else
   HEX_Value((i - 1) / 2) = HEX_Value((i - 1) / 2) + (HEX_Char(i + 1) - 48)
  End If
 Next i
 
 n = HEX_Value(0) 'n bytes
 
 If n = 0 Then
  If HEX_Value(3) = 1 Then FileEnd = True
  VerifyOneLine = &HFF00
  Exit Function 'If n is 0,exit
 End If
 
 If HEX_Value(3) = 0 Then 'Data line
  If (n And 1) = 1 Then
   n = n + 1
   HEX_Value(n + 3) = &HFF
  End If
  
  
  DataBuffer(0) = &H7
  DataBuffer(1) = &HE
  DataBuffer(2) = 5 + n
  DataBuffer(3) = Asc("V")
  DataBuffer(4) = 0
  DataBuffer(5) = 0
  DataBuffer(6) = HEX_Value(1)
  DataBuffer(7) = HEX_Value(2)
 
  TempAddr = DataBuffer(6)
  TempAddr = TempAddr * 256 + DataBuffer(7)
  
  For i = 0 To n - 1
   DataBuffer(i + 8) = HEX_Value(i + 4)
   'DataBuffer(i + 8) = &HFF
   If (TempAddr = &H14) Or (TempAddr = &H15) Or (TempAddr = &H16) Or (TempAddr = &H17) Then
    DataBuffer(i + 8) = &HFF
   End If
   TempAddr = TempAddr + 1
  Next i
  
  For i = 0 To n - 1
   TempData = DataBuffer(i + 8)
   TempData = ((TempData * 8) And &HF8) + (TempData And &HE0) / 32
   DataBuffer(i + 8) = TempData And &HFF
  Next
  
  Cal_Sum DataBuffer, DataBuffer(2) + 3
  
  WritePacket &H4, DataBuffer, DataBuffer(2) + 4
  
  Delay
  Delay
  Delay
  Delay
  
  ReadData &H4, DataBuffer, 1
  
  VerifyOneLine = DataBuffer(0)
  
 Else
  VerifyOneLine = &HFF00
  If HEX_Value(3) = 1 Then 'File end
   FileEnd = True
  End If
 End If
 Exit Function
 
Exit_sub:
 Error = True
End Function



Private Function VerifyOneFile(ByVal Dir As String, ByVal NumOfLines As Long) As Long
 On Error GoTo Exit_sub
 Dim FileLine As String
 Dim OK As Boolean
 Dim NumOfCurrentLine As Long
 Dim Result As Long
 
 StatusBar1.Panels("status").Text = "Verifying..."
 
 Open Dir For Input As 1
 
 OK = True
 
 NumOfCurrentLine = 0
 FileEnd = False
 Do While Not EOF(1)
  Line Input #1, FileLine
  NumOfCurrentLine = NumOfCurrentLine + 1
  
  Result = VerifyOneLine(FileLine)
  
  If FileEnd = True Then GoTo Exit_do
  
  If (Result <> &HFF00) Then
   If (Result <> 6) Then
    OK = False
    MsgBox "校验失败!" & vbCrLf & "校验至至HEX文件行:" & NumOfCurrentLine, vbOKOnly + vbCritical, "校验出错"
    VerifyOneFile = 0
    Close #1
    StatusBar1.Panels("status").Text = "Verify failed"
    Exit Function
   End If
  End If
  
  ProgressBar1.Value = NumOfCurrentLine * 100 / NumOfLines
  
 Loop
 
Exit_do:

 Close #1
 
 VerifyOneFile = 1
 StatusBar1.Panels("status").Text = "Verify OK"
 
 Exit Function

Exit_sub:
  MsgBox "文件不存在。", vbOKOnly + vbCritical, "打开文件错误"
End Function

Private Sub Verify_Click()
 Dim NumOfLines As Long
 Dim Result As Long
 
 DownLoad.Enabled = False
 OpenFile.Enabled = False
 Run.Enabled = False
 ReadID.Enabled = False
 Verify.Enabled = False
 
 
 StatusBar1.Panels("status").Text = "Verify..."
 DelayTime = 10 * (10002 - Speed.Value)
 

   
 If (ChipOK = True) Then
  NumOfLines = Check_HEX_File(FileDir.Text)
  If NumOfLines = 0 Then
   GoTo Exit_sub
  End If
  
  Status.Caption = ""
  ProgressBar1.Visible = True
  
  
  Result = VerifyOneFile(FileDir.Text, NumOfLines)
  
  
  ProgressBar1.Visible = False
  
  If Result = 0 Then
   Status.Caption = "校验失败!"
  End If
  
  If Result = 1 Then
   Status.Caption = "校验成功!"
  End If
  
  Else

  MsgBox "请先读芯片ID", vbOKOnly + vbInformation, "芯片ID错误"
  StatusBar1.Panels("status").Text = "Verify failed"
  GoTo Exit_sub
 End If
 
Exit_sub:
 DownLoad.Enabled = True
 OpenFile.Enabled = True
 Run.Enabled = True
 ReadID.Enabled = True
 Verify.Enabled = True
 
End Sub

⌨️ 快捷键说明

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