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) ? ? ? ? '將輸入的十六進制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)
bopen = CH341StreamI2C(mIndex, 4, buffer, 1, iBuff)
Sleep (50)
'=======================
' ? 讀AD
'=======================
Call mStrtoVal("900091", buffer, 3) ? ? ?'將輸入的十六進制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)
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已打開則關(guān)閉
Unload Me ? ?'關(guān)閉窗體
End Sub
Private Sub Form_Load()
mIndex = 0
hopen = CH341OpenDevice(mIndex)
If (hopen = INVALID_HANDLE_VALUE) Then
? ? mOpen = False
Else
? ? mOpen = True
End If
'設(shè)置設(shè)備插拔通知
If CH341SetDeviceNotify(mIndex, vbNullString, AddressOf mPCH341_NOTIFY_ROUTINE) = False Then
? ? MsgBox "設(shè)置設(shè)備插拔通知失敗", vbExclamation
End If
enablebtn (mOpen)
bopen = CH341SetStream(mIndex, &H83) ? ? ? ? ? ? ? ? ? ?'設(shè)置同步串口流模式,高位在前
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) ?'設(shè)備插拔通知處理程序
? ? mIndex = 0
? ? iEventStatus = KeyCode '插拔事件
? ? If (iEventStatus = CH341_DEVICE_ARRIVAL) Then ' 設(shè)備插入事件,CH341_DEVICE_ARRIVAL=3已經(jīng)插入
? ? ? ? If (CH341OpenDevice(mIndex) = INVALID_HANDLE_VALUE) Then ? ? 'INVALID_HANDLE_VALUE=-1
? ? ? ? ? ? MsgBox "打開設(shè)備失敗!", vbOK
? ? ? ? ? ? mOpen = False
? ? ? ? Else
? ? ? ? ? ? mOpen = True ?'打開成功
? ? ? ? End If
? ? ElseIf (iEventStatus = CH341_DEVICE_REMOVE) Then ' 設(shè)備拔出事件,CH341_DEVICE_REMOVE=0已經(jīng)拔出
? ? ? ? CH341CloseDevice (mIndex)
? ? ? ? mOpen = False
? ? End If
? ? enablebtn (mOpen) '設(shè)備打開,按鈕可用,設(shè)備沒打開,按鈕禁用
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 '窗體標(biāo)題顯示
? ? ? ? Form1.Caption = "USB已連接"
? ? Else
? ? ? ? Form1.Caption = "USB未連接"
? ? End If
? End With
End Sub