Option Explicit
‘使用するAPIの宣言
Public Declare PtrSafe Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)
Private Declare PtrSafe Function FT_Open Lib “FTD2XX.DLL” (ByVal intDeviceNumber As Integer, ByRef lngHandle As Long) As Long
Private Declare PtrSafe Function FT_Close Lib “FTD2XX.DLL” (ByVal lngHandle As Long) As Long
Private Declare PtrSafe Function FT_ResetDevice Lib “FTD2XX.DLL” (ByVal lngHandle As Long) As Long
Private Declare PtrSafe Function FT_SetBitMode Lib “FTD2XX.DLL” (ByVal lngHandle As Long, ByVal ucMask As Byte, ByVal ucEnable As Byte) As Long
Private Declare PtrSafe Function FT_Write Lib “FTD2XX.DLL” (ByVal lngHandle As Long, ByRef WritBuffer As Any, ByVal lngBufferSize As Long, ByRef lngBytesWritten As Long) As Long
Private Declare PtrSafe Function FT_Read Lib “FTD2XX.DLL” (ByVal lngHandle As Long, ByRef ReraBuffer As Any, ByVal lngBufferSize As Long, ByRef lngBytesReturned As Long) As Long
‘温度と湿度をグローバル変数に設定
Dim thm As Double
Dim hum As Double
Dim log_tim As Date
Dim actv_row As Integer
Dim PassTime As Integer
Public Sub log_start()
Dim actv_cell As String
‘セルB2に経過時間(分)を記載
Range(“B2”).Value = “経過時間(分)”
‘セルC2に温度(℃)を記載
Range(“C2”).Value = “温度(℃)”
‘セルD2に湿度(%)を記載
Range(“D2”).Value = “湿度(%)”
‘取得データを表示させる行位置の変数初期化
actv_row = 3
‘経過時間の変数初期化
PassTime = 0
‘現在時刻を設定
log_tim = Now
Call log
End Sub
Sub log()
Dim actv_cell As String
‘FT2232から温度と湿度を取得
Call ft2232_get
‘経過時間をセルに表示
actv_cell = “B” + Mid(Str(actv_row), 2)
Range(actv_cell).Value = PassTime
‘FT2232から取得した温度と湿度をセルに表示
‘温度表示
actv_cell = “C” + Mid(Str(actv_row), 2)
Range(actv_cell).Value = thm
‘湿度表示
actv_cell = “D” + Mid(Str(actv_row), 2)
Range(actv_cell).Value = hum
‘取得データを表示させる行位置をインクリメント
actv_row = actv_row + 1
‘経過時間の変数をインクリメント
PassTime = PassTime + 1
‘次回取得時刻を算出設定
log_tim = log_tim + TimeValue(“00:01:00″)
‘次回測定時間にタイマー設定
Application.OnTime EarliestTime:=log_tim, Procedure:=”log”
End Sub
Sub ft2232_get()
Dim lngHandle As Long
Dim ret As Long
Dim data As Long
Dim data2 As Long
Dim InputBuffer(100) As Byte
Dim OutputBuffer(100) As Byte
Dim rsp(4) As Byte
‘FT2232のオープンとウインドウハンドルの取得
ret = FT_Open(0, lngHandle)
‘FT2232リセット
ret = FT_ResetDevice(lngHandle)
‘タイムウエイト
Sleep (100)
‘FT2232リセット
ret = FT_SetBitMode(lngHandle, 0, 0)
‘FT2232をMPSSEモードに設定11
ret = FT_SetBitMode(lngHandle, 0, 2)
‘タイムウエイト
Sleep (100)
‘MPSSEの設定
‘/ループバック無効
OutputBuffer(0) = &H85
‘Adaptive clockingをOFF
OutputBuffer(1) = &H97
‘3 phase data clocking有効
OutputBuffer(2) = &H8C
‘クロック分周の設定コマンド
OutputBuffer(3) = &H86
‘クロック分周の値設定
OutputBuffer(4) = &H1F
OutputBuffer(5) = &H0
‘バッファに設定したコマンドを送信
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘Set Data bits LowByteコマンド(GPIO下位バイトの設定)
OutputBuffer(0) = &H80
‘SDAとSCLをHigh
OutputBuffer(1) = &H3
‘SDAとSCLを出力設定
OutputBuffer(2) = &H3
‘バッファに設定したコマンドを送信 16
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘FT2232MPSSE初期設定完了**************************************************
‘タイムウエイト
Sleep (100)
‘HDC1000の設定
‘スタートコンディション発行
‘SCL,SDAをHigh SCL,SDAを出力ポート設定
OutputBuffer(0) = &H80
OutputBuffer(1) = &H3
OutputBuffer(2) = &H3
‘SDAをLow
OutputBuffer(3) = &H80
OutputBuffer(4) = &H1
OutputBuffer(5) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘デバイスアドレス(0b1000 0000)送信
OutputBuffer(0) = &H11
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
OutputBuffer(3) = &H80
‘SCLをLow
OutputBuffer(4) = &H80
OutputBuffer(5) = &H0
OutputBuffer(6) = &H1
‘ACK受信、SDAはHigh-z(入力ポート設定)
‘SDAをHigh-z
OutputBuffer(7) = &H80
OutputBuffer(8) = &H2
OutputBuffer(9) = &H1
‘SCLをHigh
OutputBuffer(10) = &H80
OutputBuffer(11) = &H1
OutputBuffer(12) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 13, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘ポインタアドレス0x02送信
OutputBuffer(0) = &H11
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
OutputBuffer(3) = &H2
‘SCLをLow
OutputBuffer(4) = &H80
OutputBuffer(5) = &H0
OutputBuffer(6) = &H3
‘ACK受信、SDAはHigh-z(入力ポート設定)
‘SDAをHigh-z
OutputBuffer(7) = &H80
OutputBuffer(8) = &H2
OutputBuffer(9) = &H1
‘SCLをHigh
OutputBuffer(10) = &H80
OutputBuffer(11) = &H1
OutputBuffer(12) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 13, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘HDC1000に設定値を書込、温度湿度14ビット、温度と湿度を同時に計測
‘送信データは0x10 00 2バイト送信
‘上位バイト0x10送信
OutputBuffer(0) = &H11
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
OutputBuffer(3) = &H10
‘SCLをLow
OutputBuffer(4) = &H80
OutputBuffer(5) = &H0
OutputBuffer(6) = &H3
‘ACK受信、SDAはHigh-z(入力ポート設定)
‘SDAをHigh-z
OutputBuffer(7) = &H80
OutputBuffer(8) = &H2
OutputBuffer(9) = &H1
‘SCLをHigh
OutputBuffer(10) = &H80
OutputBuffer(11) = &H1
OutputBuffer(12) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 13, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘下位バイト0x00送信
OutputBuffer(0) = &H11
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
OutputBuffer(3) = &H0
‘SCLをLow
OutputBuffer(4) = &H80
OutputBuffer(5) = &H0
OutputBuffer(6) = &H3
‘ACK受信、SDAはHigh-z(入力ポート設定)
‘SDAをHigh-z
OutputBuffer(7) = &H80
OutputBuffer(8) = &H2
OutputBuffer(9) = &H1
‘SCLをHigh
OutputBuffer(10) = &H80
OutputBuffer(11) = &H1
OutputBuffer(12) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 13, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘ストップコンディション発行
‘SCL,SDAをLow SCL,SDAを出力ポート設定
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H3
‘SCLをHigh
OutputBuffer(3) = &H80
OutputBuffer(4) = &H1
OutputBuffer(5) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘タイムウエイト
Sleep (15)
‘SDAをHigh
OutputBuffer(0) = &H80
OutputBuffer(1) = &H3
OutputBuffer(2) = &H3
‘SCLをHigh,SDAをHigh-z
OutputBuffer(3) = &H80
OutputBuffer(4) = &H1
OutputBuffer(5) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘タイムウエイト
Sleep (15)
‘HDC1000計測開始**********************************
‘スタートコンディション発行
‘SCL,SDAをHigh SCL,SDAを出力ポート設定
OutputBuffer(0) = &H80
OutputBuffer(1) = &H3
OutputBuffer(2) = &H3
‘SDAをLow
OutputBuffer(3) = &H80
OutputBuffer(4) = &H1
OutputBuffer(5) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘デバイスアドレス(0b1000 0000)送信
OutputBuffer(0) = &H11
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
OutputBuffer(3) = &H80
‘SCLをLow
OutputBuffer(4) = &H80
OutputBuffer(5) = &H0
OutputBuffer(6) = &H3
‘ACK受信、SDAはHigh-z(入力ポート設定)
‘SDAをHigh-z
OutputBuffer(7) = &H80
OutputBuffer(8) = &H2
OutputBuffer(9) = &H1
‘SCLをHigh
OutputBuffer(10) = &H80
OutputBuffer(11) = &H1
OutputBuffer(12) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 13, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘ポインタアドレス0x00送信
OutputBuffer(0) = &H11
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
OutputBuffer(3) = &H0
‘SCLをLow
OutputBuffer(4) = &H80
OutputBuffer(5) = &H0
OutputBuffer(6) = &H3
‘ACK受信、SDAはHigh-z(入力ポート設定)
‘SDAをHigh-z
OutputBuffer(7) = &H80
OutputBuffer(8) = &H2
OutputBuffer(9) = &H1
‘SCLをHigh
OutputBuffer(10) = &H80
OutputBuffer(11) = &H1
OutputBuffer(12) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 13, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘HDC1000計測値取得**********************************
‘スタートコンディション発行
‘SCL,SDAをHigh SCL,SDAを出力ポート設定
OutputBuffer(0) = &H80
OutputBuffer(1) = &H3
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘SDAをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H1
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘デバイスアドレス(0b1000 0001)送信
OutputBuffer(0) = &H11
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
OutputBuffer(3) = &H81
‘SCLをLow
OutputBuffer(4) = &H80
OutputBuffer(5) = &H2
OutputBuffer(6) = &H1
‘ACK受信、SDAはHigh-z(入力ポート設定)
‘SDAをHigh-z
OutputBuffer(7) = &H80
OutputBuffer(8) = &H2
OutputBuffer(9) = &H1
‘SCLをHigh
OutputBuffer(10) = &H80
OutputBuffer(11) = &H1
OutputBuffer(12) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 13, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘ADD=0(温度)上位バイト取得
‘SDAをHigh-z
OutputBuffer(0) = &H80
OutputBuffer(1) = &H2
OutputBuffer(2) = &H1
‘1バイト(8ビット)データ取得
OutputBuffer(3) = &H20
OutputBuffer(4) = &H0
OutputBuffer(5) = &H0
‘ACK送信
‘ACK送信、SDAをLow
OutputBuffer(6) = &H80
OutputBuffer(7) = &H0
OutputBuffer(8) = &H3
‘SCLをHigh
OutputBuffer(9) = &H80
OutputBuffer(10) = &H1
OutputBuffer(11) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 12, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H3
‘SDAをHigh-z
OutputBuffer(3) = &H80
OutputBuffer(4) = &H0
OutputBuffer(5) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘タイムウエイト
Sleep (15)
‘SDA受信データを取得
ret = FT_Read(lngHandle, InputBuffer(0), 1, data2)
rsp(0) = InputBuffer(0)
‘ADD=0(温度)下位バイト取得
‘1バイト(8ビット)データ取得
OutputBuffer(0) = &H20
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
‘ACK送信
‘ACK送信、SDAをLow
OutputBuffer(3) = &H80
OutputBuffer(4) = &H0
OutputBuffer(5) = &H3
‘SCLをHigh
OutputBuffer(6) = &H80
OutputBuffer(7) = &H1
OutputBuffer(8) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 9, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H3
‘SDAをHigh-z
OutputBuffer(3) = &H80
OutputBuffer(4) = &H0
OutputBuffer(5) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘タイムウエイト
Sleep (15)
‘SDA受信データを取得
ret = FT_Read(lngHandle, InputBuffer(0), 1, data2)
rsp(1) = InputBuffer(0)
‘ADD=1(湿度)上位バイト取得
‘1バイト(8ビット)データ取得
OutputBuffer(0) = &H20
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
‘ACK送信
‘ACK送信、SDAをLow
OutputBuffer(3) = &H80
OutputBuffer(4) = &H0
OutputBuffer(5) = &H3
‘SCLをHigh
OutputBuffer(6) = &H80
OutputBuffer(7) = &H1
OutputBuffer(8) = &H3
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 9, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H3
‘SDAをHigh-z
OutputBuffer(3) = &H80
OutputBuffer(4) = &H0
OutputBuffer(5) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 6, data)
‘タイムウエイト
Sleep (15)
‘SDA受信データを取得
ret = FT_Read(lngHandle, InputBuffer(0), 1, data2)
rsp(2) = InputBuffer(0)
‘ADD=1(湿度)下位バイト取得
‘1バイト(8ビット)データ取得
OutputBuffer(0) = &H20
OutputBuffer(1) = &H0
OutputBuffer(2) = &H0
‘NACK送信
‘SDAをHigh
OutputBuffer(3) = &H80
OutputBuffer(4) = &H0
OutputBuffer(5) = &H1
‘SCLをHigh
OutputBuffer(6) = &H80
OutputBuffer(7) = &H1
OutputBuffer(8) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 9, data)
‘タイムウエイト
Sleep (15)
‘SCLをLow
OutputBuffer(0) = &H80
OutputBuffer(1) = &H0
OutputBuffer(2) = &H1
‘FT2232Write設定
ret = FT_Write(lngHandle, OutputBuffer(0), 3, data)
‘タイムウエイト
Sleep (15)
‘SDA受信データを取得
ret = FT_Read(lngHandle, InputBuffer(0), 1, data2)
rsp(3) = InputBuffer(0)
‘I2C受信データから温度換算
thm = rsp(0)
thm = thm * 256
thm = thm + rsp(1)
thm = thm / 65536
thm = thm * 165
thm = thm – 40
thm = Application.RoundDown(thm, 1)
‘I2C受信データから湿度換算
hum = rsp(2)
hum = hum * 256
hum = hum + rsp(3)
hum = hum / 65536
hum = hum * 100
hum = Application.RoundDown(hum, 1)
ret = 0
‘FT2232クローズ
ret = FT_Close(lngHandle)
End Sub