Skip to content

Instantly share code, notes, and snippets.

@7m4mon
Created March 18, 2020 13:53
Show Gist options
  • Save 7m4mon/3c4d2bae0d1095e38ee9c90e74fca63f to your computer and use it in GitHub Desktop.
Save 7m4mon/3c4d2bae0d1095e38ee9c90e74fca63f to your computer and use it in GitHub Desktop.
' サンハヤトの Sample_MMCP2112_MMTXS03 からGPIO4を操作する部分を抜き出し
' 割と忠実にExcel VBAに移植してみた。
' 2020/03/18 7M4MON
' 動作環境 Windows10 64Bit + Excel 2010 32bit + Sunhayato MM-CP2112A
' HID USB to SMBus Master Bridge USB・I2C(SMBus)変換モジュール
' *** 準備 ***
' * SLABHIDDevice.dll と SLABHIDtoSMBus.dll を C:\Windows\SysWOW64 にコピーしておく
' * SLABCP2112.bas をインポートして、オブジェクト名を [CP2112_DLL] にする
Sub cp2112_gpio4_output_set()
Dim vid As Integer
Dim pid As Integer
Dim numDevices As Long
Dim onOff As Integer
Dim led_data As Byte
Dim rtnval As Integer
Dim connectedDevice As Long
vid = &H10C4 '(CP2112デフォルトVID)
pid = &HEA90 '(CP2112デフォルトPID)
' 未接続 → 接続
numDevices = 0
CP2112_DLL.HidSmbus_GetNumDevices numDevices, vid, pid
Debug.Print "numDevices " & numDevices
' 1台だけ接続されていることをチェック
If numDevices < 1 Then
MsgBox ("MM-CP2112が接続されていません。")
Exit Sub
ElseIf numDevices > 1 Then
MsgBox ("MM-CP2112が複数接続されています。接続は1台までです。")
Exit Sub
End If
' デバイスポインター取得
CP2112_DLL.HidSmbus_Open connectedDevice, 0, vid, pid
' CP2112初期設定
CP2112_DLL.HidSmbus_SetSmbusConfig connectedDevice, 100000, &H2, 0, 100, 100, 0, 2 ' オートレスポンス=無効
CP2112_DLL.HidSmbus_SetGpioConfig connectedDevice, &HBF, &H0, &H6, &HFF ' GPIO.4=Output, GPIO.6=Input, TXT=Enable, RXT=Enable
CP2112_DLL.HidSmbus_WriteLatch connectedDevice, &HFF, &HFF
' GPIO4 LED点灯・消灯
onOff = InputBox("GPIO4 LED点灯・消灯", , 1)
If onOff = 1 Then
led_data = 0
Else
led_data = &H10
End If
rtnval = CP2112_DLL.HidSmbus_WriteLatch(connectedDevice, led_data, &H10)
MsgBox ("結果を確認:" & rtnval)
If (rtnval <> CP2112_DLL.HID_SMBUS_SUCCESS) Then
MessageBox.Show ("接続エラー HidSmbus_WriteLatch ERR " & rtnval)
End If
' CP2112の接続を解除する
CP2112_DLL.HidSmbus_Close connectedDevice
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment