📄 ad_module.bas
字号:
Attribute VB_Name = "AD_Module"
Option Explicit
Public Sample_period As Single '采样周期
Public row As Integer '循环时间
Public T As Single, n As Single, j As Single, m As Single
Public wencha As Single, wencha_Change As Single '给定温度与测量温度的差值
Public count_time As Single, Run_time As Single
Public Const INVALID_HANDLE_VALUE = -1
Public hDevice As Long
Public pADBuffer(0 To 65536) As Integer
Public DeviceID As Long
Public ADPara As USB2013_PARA_AD
Public ChannelCount As Integer
Public nRemainningWords As Integer
Public ReadSizeWords As Integer
Public flag(15) As Boolean '置15通道标志,用此标志确定该通道是否被刷新
Public PerLsbVolt As Double '根据设备的Bit位数,用于确定每个Lsb分配的电压值
Public ReadIndex As Integer '级链缓冲区的索引号
Public CurrentIndex As Integer '数据处理时使用的当前缓冲索引
Public Channel As Long
Public exApp As Excel.Application, _
xlWB As Excel.Workbook
Public ADstatus As Boolean
Public Dshi(1 To 7) As Single, Average As Single, Max_Dshi As Single, Min_Dshi As Single, SamF As Integer
Public All_Dshi As Single, Temp As Single, X_time As Single
Public Sub CollectDataFunction_Initial() '数据采集函数
Dim i As Long
ChannelCount = ADPara.LastChannel - ADPara.FirstChannel + 1 '采集通道总数
nRemainningWords = 1024 Mod (ChannelCount * 32) '取余数
ReadSizeWords = 1024 - nRemainningWords '使取余数以后的点,正好是通道的整数倍
If USB2013_InitDeviceAD(hDevice, ADPara) = False Then
MsgBox "不明确的初始化错误....", 0, "error"
Exit Sub
End If
End Sub
Public Sub CollectDataFunction()
All_Dshi = 0
If USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords) = 0 Then
MsgBox "读数据出错...", 0, "error"
Exit Sub
End If
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords) '重复读数
Dshi(1) = Format((pADBuffer(0) And &HFFF) * PerLsbVolt, "#.00")
'MsgBox (" Dshi(1)=" & Dshi(1))
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
Dshi(2) = Format((pADBuffer(0) And &HFFF) * PerLsbVolt, "#.00")
'MsgBox (" Dshi(2)=" & Dshi(2))
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
Dshi(3) = Format((pADBuffer(0) And &HFFF) * PerLsbVolt, "#.00")
'MsgBox (" Dshi(3)=" & Dshi(3))
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
Dshi(4) = Format((pADBuffer(0) And &HFFF) * PerLsbVolt, "#.00")
'MsgBox (" Dshi(4)=" & Dshi(4))
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
Dshi(5) = Format((pADBuffer(0) And &HFFF) * PerLsbVolt, "#.00")
'MsgBox (" Dshi(5)=" & Dshi(5))
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
Dshi(6) = Format((pADBuffer(0) And &HFFF) * PerLsbVolt, "#.00")
'MsgBox (" Dshi(6)=" & Dshi(6))
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
ADstatus = USB2013_ReadDeviceAD(hDevice, pADBuffer(0), ReadSizeWords)
Dshi(7) = Format((pADBuffer(0) And &HFFF) * PerLsbVolt, "#.00")
'MsgBox (" Dshi(7)=" & Dshi(7))
Max_Dshi = Dshi(1)
Min_Dshi = Dshi(1)
For SamF = 1 To 6
If Dshi(SamF + 1) > Max_Dshi Then
Max_Dshi = Dshi(SamF + 1)
End If
Next SamF
For SamF = 1 To 6
If Dshi(SamF + 1) < Min_Dshi Then
Min_Dshi = Dshi(SamF + 1)
End If
Next SamF
For SamF = 1 To 7
All_Dshi = All_Dshi + Dshi(SamF)
Next SamF
Average = (All_Dshi - Max_Dshi - Min_Dshi) / 2500 '滤波后的热电势
'MsgBox (Average)
AD_Form.Label4(0).Caption = Format(Average, "0.000") '显示采集到的热电偶上电势
Temp = 0.108 * (Average) ^ 3 - 3.6843 * (Average) ^ 2 + 127.0174 * Average + 36.5882 '转换后的温度
T = Format(Temp, "#.00")
AD_Form.wendu0(0).Caption = T
'AD_Form.Picture1.DrawWidth = 1 '改变直线的线宽
If row Mod 3 = 0 Then
AD_Form.Picture1.Line (m, n)-(X_time, T), QBColor(5) '绘制实时烧成曲线
m = X_time '时间变量
n = T '温度变量
End If
Call Temp_handle '温度处理函数
Call Fuzzy_PID_Module1.fuzzy_function
Call save_data '数据保存函数
'Call DA_Module.WriteDAFunction
End Sub
Public Sub save_data()
On Error GoTo save_return
'Set xlWB = exApp.Workbooks.Add
If Len(AD_Form.CommonDialog1.FileName) >= 1 Then
exApp.Workbooks.Open AD_Form.CommonDialog1.FileName
exApp.Cells(row + 1, 1) = T
exApp.Cells(row + 1, 2) = AD_Form.Label4(0).Caption
exApp.Cells(row + 1, 3) = wencha
exApp.Cells(row + 1, 4) = wencha_Change
exApp.Cells(row + 1, 5) = Kp_All
exApp.Cells(row + 1, 6) = Ki_All
exApp.Cells(row + 1, 7) = Kd_All
exApp.Cells(row + 1, 8) = Control_Output
exApp.ActiveWorkbook.Save
' xlWB.Close False
End If
Exit Sub
save_return:
'MsgBox Err.Number
AD_Form.errorlabel.Visible = True
Exit Sub
End Sub
Public Sub Temp_handle()
Run_time = Run_time + Val(AD_Form.Period_Txt.Text)
count_time = Run_time / 60
AD_Form.ShowTime_lbl.Caption = Format(count_time, "0.00")
Select Case Dcount
Case 1
If 0 <= count_time And count_time <= t0 Then
wencha = k0 * count_time + b0 - T
End If
Case 2
If 0 <= count_time And count_time <= t0 Then
wencha = k0 * count_time + b0 - T
ElseIf t0 < count_time And count_time <= (t0 + t1) Then
If k1 = 0 Then
wencha = k0 * t0 + b0 - T
Else
wencha = k1 * count_time + b1 - T
End If
End If
Case 3
If 0 <= count_time And count_time <= t0 Then
wencha = k0 * count_time + b0 - T
' MsgBox (k0 * count_time + b0)
ElseIf t0 < count_time And count_time <= (t0 + t1) Then
If k1 = 0 Then
wencha = k0 * t0 + b0 - T
Else
wencha = k1 * count_time + b1 - T
End If
ElseIf t0 + t1 < count_time And count_time <= t0 + t1 + t2 Then
If k2 = 0 Then
wencha = k1 * (t0 + t1) + b1 - T
Else
wencha = k2 * count_time + b2 - T
End If
End If
Case 4
If 0 <= count_time And count_time <= t0 Then
wencha = k0 * count_time + b0 - T
ElseIf t0 < count_time And count_time <= (t0 + t1) Then
If k1 = 0 Then
wencha = k0 * t0 + b0 - T
Else
wencha = k1 * count_time + b1 - T
End If
ElseIf t0 + t1 < count_time And count_time <= t0 + t1 + t2 Then
If k2 = 0 Then
wencha = k1 * (t0 + t1) + b1 - T
Else
wencha = k2 * count_time + b2 - T
End If
ElseIf t0 + t1 + t1 < count_time And count_time <= t0 + t1 + t2 + t3 Then
If k3 = 0 Then
wencha = k2 * (t0 + t1 + t2) + b2 - T
Else
wencha = k3 * count_time + b2 - T
End If
End If
Case 5
If 0 <= count_time And count_time <= t0 Then
wencha = k0 * count_time + b0 - T
ElseIf t0 < count_time And count_time <= (t0 + t1) Then
If k1 = 0 Then
wencha = k0 * t0 + b0 - T
Else
wencha = k1 * count_time + b1 - T
End If
ElseIf t0 + t1 < count_time And count_time <= t0 + t1 + t2 Then
If k2 = 0 Then
wencha = k1 * (t0 + t1) + b1 - T
Else
wencha = k2 * count_time + b2 - T
End If
ElseIf t0 + t1 + t1 < count_time And count_time <= t0 + t1 + t2 + t3 Then
If k3 = 0 Then
wencha = k2 * (t0 + t1 + t2) + b2 - T
Else
wencha = k3 * count_time + b2 - T
End If
ElseIf t0 + t1 + t1 + t3 < count_time And count_time <= t0 + t1 + t2 + t3 + t4 Then
If k4 = 0 Then
wencha = k3 * (t0 + t1 + t2 + t3) + b3 - T
Else
wencha = k4 * count_time + b4 - T
End If
End If
End Select
' MsgBox (wencha)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -