ADS1115地址端接GND
Option Explicit
Dim iBuff As arrRBuffer
Dim buffer As arrRBuffer
Dim PGA As String
Private Sub Command1_Click() '启动AD
Dim Channel As String
Dim SPS As String
Dim buff(0 To 1) As Long
If Option1.Value = True Then PGA = "1"
If Option2.Value = True Then PGA = "3"
If Option3.Value = True Then PGA = "5"
If Option4.Value = True Then PGA = "7"
If Option5.Value = True Then PGA = "9"
If Option6.Value = True Then PGA = "B"
If Option7.Value = True Then Channel = "C"
If Option8.Value = True Then Channel = "D"
If Option9.Value = True Then Channel = "E"
If Option10.Value = True Then Channel = "F"
If Option11.Value = True Then SPS = "0"
If Option12.Value = True Then SPS = "2"
If Option13.Value = True Then SPS = "4"
If Option14.Value = True Then SPS = "6"
If Option15.Value = True Then SPS = "8"
If Option16.Value = True Then SPS = "A"
If Option17.Value = True Then SPS = "C"
If Option18.Value = True Then SPS = "E"
'=================
' 启动AD
'=================
Call mStrtoVal("9001" & Channel & PGA & SPS & "3", buffer, 4) '将输入的十六进制格式字符数据转成数值数据
bopen = CH341StreamI2C(mIndex, 4, buffer, 1, iBuff)
Sleep (50)
'=======================
' 读AD
'=======================
Call mStrtoVal("900091", buffer, 3) '将输入的十六进制格式字符数据转成数值数据
bopen = CH341StreamI2C(mIndex, 3, buffer, 2, iBuff)
buff(0) = iBuff.buf(0) * 256
buff(1) = iBuff.buf(1)
If Option1.Value = True Then Label2.Caption = "输入电压=" & Format((buff(0) + buff(1)) * 12.288 / 65535, "0.0000") & " V"
If Option2.Value = True Then Label2.Caption = "输入电压=" & Format((buff(0) + buff(1)) * 8.192 / 65535, "0.0000") & " V"
If Option3.Value = True Then Label2.Caption = "输入电压=" & Format((buff(0) + buff(1)) * 4.096 / 65535, "0.0000") & " V"
If Option4.Value = True Then Label2.Caption = "输入电压=" & Format((buff(0) + buff(1)) * 2.048 / 65535, "0.0000") & " V"
If Option5.Value = True Then Label2.Caption = "输入电压=" & Format((buff(0) + buff(1)) * 1.024 / 65535, "0.0000") & " V"
If Option6.Value = True Then Label2.Caption = "输入电压=" & Format((buff(0) + buff(1)) * 0.512 / 65535, "0.0000") & " V"
End Sub
Private Sub Command2_Click() 'QUIT
CH341SetDeviceNotify 0, vbNullString, 0&
If (mOpen = True) Then CH341CloseDevice (mIndex) '如果CH341已打开则关闭
Unload Me '关闭窗体
End Sub
Private Sub Form_Load()
mIndex = 0
hopen = CH341OpenDevice(mIndex)
If (hopen = INVALID_HANDLE_VALUE) Then
mOpen = False
Else
mOpen = True
End If
'设置设备插拔通知
If CH341SetDeviceNotify(mIndex, vbNullString, AddressOf mPCH341_NOTIFY_ROUTINE) = False Then
MsgBox "设置设备插拔通知失败", vbExclamation
End If
enablebtn (mOpen)
bopen = CH341SetStream(mIndex, &H83) '设置同步串口流模式,高位在前
Label2.Caption = ""
Option3.Value = True
Option7.Value = True
Option15.Value = True
End Sub
Private Sub CH341_NOTIFY_ROUTINE_KeyUp(KeyCode As Integer, Shift As Integer) '设备插拔通知处理程序
mIndex = 0
iEventStatus = KeyCode '插拔事件
If (iEventStatus = CH341_DEVICE_ARRIVAL) Then ' 设备插入事件,CH341_DEVICE_ARRIVAL=3已经插入
If (CH341OpenDevice(mIndex) = INVALID_HANDLE_VALUE) Then 'INVALID_HANDLE_VALUE=-1
MsgBox "打开设备失败!", vbOK
mOpen = False
Else
mOpen = True '打开成功
End If
ElseIf (iEventStatus = CH341_DEVICE_REMOVE) Then ' 设备拔出事件,CH341_DEVICE_REMOVE=0已经拔出
CH341CloseDevice (mIndex)
mOpen = False
End If
enablebtn (mOpen) '设备打开,按钮可用,设备没打开,按钮禁用
End Sub
Public Sub enablebtn(ByVal bEnable As Boolean) 'bEnable=true :各窗体按钮可用 ;=false:enable:各窗体按钮禁用
With Form1
.Command1.Enabled = bEnable
.Frame1.Enabled = bEnable
.Frame2.Enabled = bEnable
.Frame3.Enabled = bEnable
If (bEnable = True) Then '窗体标题显示
Form1.Caption = "USB已连接"
Else
Form1.Caption = "USB未连接"
End If
End With
End Sub