⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c_pca9535.bas

📁 VB6, paralell port, I2C, parse intel HEX
💻 BAS
字号:
Attribute VB_Name = "I2C_PCA9535"
Option Explicit

' public declaration of port values
Type PortsType
    P0 As Integer
    P1 As Integer
End Type

Public Ports As PortsType



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   write and read settings of PCA9535 ports
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Public Function DirPort0(ByVal DevNum As Byte, ByVal PinDir As String) As Boolean
On Error GoTo greska
' "0" pin set as output
' "1" pin set as high imedance input pin
DirPort0 = I2C.I2C_Send_Byte(DevNum, "00000110", PinDir)
Exit Function
greska:
DirPort0 = False
MsgBox "DirPort0: " & Err.Number & Err.Description
End Function

Public Function DirPort1(ByVal DevNum As Byte, ByVal PinDir As String) As Boolean
On Error GoTo greska
' "0" pin set as output
' "1" pin set as high imedance input pin
DirPort1 = I2C.I2C_Send_Byte(DevNum, "00000111", PinDir)
Exit Function
greska:
DirPort1 = False
MsgBox "DirPort1: " & Err.Number & Err.Description
End Function

Public Function DirPorts(ByVal DevNum As Byte, ByVal PinDir0 As String, ByVal PinDir1 As String) As Boolean
On Error GoTo greska
' "0" pin set as output
' "1" pin set as high imedance input pin
DirPorts = I2C.I2C_Send_2_Bytes(DevNum, "00000110", PinDir0, PinDir1)
Exit Function
greska:
DirPorts = False
MsgBox "SetPorts: " & Err.Number & Err.Description
End Function

Public Function PolPort0(ByVal DevNum As Byte, ByVal PinPol As String) As Boolean
On Error GoTo greska
' "0" pin set as is on input
' "1" pin set as inverted input
PolPort0 = I2C.I2C_Send_Byte(DevNum, "00000100", PinPol)
Exit Function
greska:
PolPort0 = False
MsgBox "PolPort0: " & Err.Number & Err.Description
End Function

Public Function PolPort1(ByVal DevNum As Byte, ByVal PinPol As String) As Boolean
On Error GoTo greska
' "0" pin set as is on input
' "1" pin set as inverted input
PolPort1 = I2C.I2C_Send_Byte(DevNum, "00000101", PinPol)
Exit Function
greska:
PolPort1 = False
MsgBox "PolPort1: " & Err.Number & Err.Description
End Function

Public Function PolPorts(ByVal DevNum As Byte, ByVal PinPol0 As String, ByVal PinPol1 As String) As Boolean
On Error GoTo greska
' "0" pin set as is on input
' "1" pin set as inverted input
PolPorts = I2C.I2C_Send_2_Bytes(DevNum, "00000100", PinPol0, PinPol1)
Exit Function
greska:
PolPorts = False
MsgBox "PolPorts: " & Err.Number & Err.Description
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   write and read binary string values to PCA9535 ports
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Public Function WritePort0(ByVal DevNum As Byte, ByVal PortVal As String) As Boolean
On Error GoTo greska
WritePort0 = I2C.I2C_Send_Byte(DevNum, "00000010", PortVal)
Exit Function
greska:
WritePort0 = False
MsgBox "WritePort0: " & Err.Number & Err.Description
End Function

Public Function WritePort1(ByVal DevNum As Byte, ByVal PortVal As String) As Boolean
On Error GoTo greska
WritePort1 = I2C.I2C_Send_Byte(DevNum, "00000011", PortVal)
Exit Function
greska:
WritePort1 = False
MsgBox "WritePort1: " & Err.Number & Err.Description
End Function

Public Function WritePorts(ByVal DevNum As Byte, ByVal PortVal0 As String, ByVal PortVal1 As String) As Boolean
On Error GoTo greska
WritePorts = I2C.I2C_Send_2_Bytes(DevNum, "00000010", PortVal0, PortVal1)
Exit Function
greska:
WritePorts = False
MsgBox "WritePorts: " & Err.Number & Err.Description
End Function

Public Function ReadPort0(ByVal DevNum As Byte) As String
On Error GoTo greska
ReadPort0 = I2C.I2C_Read_Byte(DevNum, "00000000")
Exit Function
greska:
ReadPort0 = "00000000"
MsgBox "ReadPort0: " & Err.Number & Err.Description
End Function

Public Function ReadPort1(ByVal DevNum As Byte) As String
On Error GoTo greska
ReadPort1 = I2C.I2C_Read_Byte(DevNum, "00000001")
Exit Function
greska:
ReadPort1 = "00000000"
MsgBox "ReadPort1: " & Err.Number & Err.Description
End Function

Public Function ReadPorts(ByVal DevNum As Byte) As String
On Error GoTo greska
ReadPorts = I2C.I2C_Read_2_Bytes(DevNum, "00000000")
Exit Function
greska:
ReadPorts = "0000000000000000"
MsgBox "ReadPorts: " & Err.Number & Err.Description
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   write and read integer values to PCA9535 ports
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Public Function WritePort0Val(ByVal DevNum As Byte, ByVal PortNumVal As Byte) As Boolean
On Error GoTo greska
Dim PortVal As String
PortVal = Bitwise.IntValToBinStr(PortNumVal)
WritePort0Val = I2C.I2C_Send_Byte(DevNum, "00000010", PortVal)
Exit Function
greska:
WritePort0Val = False
MsgBox "WritePort0Val: " & Err.Number & Err.Description
End Function

Public Function WritePort1Val(ByVal DevNum As Byte, ByVal PortNumVal As Byte) As Boolean
On Error GoTo greska
Dim PortVal As String
PortVal = Bitwise.IntValToBinStr(PortNumVal)
WritePort1Val = I2C.I2C_Send_Byte(DevNum, "00000011", PortVal)
Exit Function
greska:
WritePort1Val = False
MsgBox "WritePort1: " & Err.Number & Err.Description
End Function

Public Function WritePortsVal(ByVal DevNum As Byte, ByVal PortNumVal0 As Byte, ByVal PortNumVal1 As Byte) As Boolean
On Error GoTo greska
Dim PortVal0 As String
Dim PortVal1 As String
PortVal0 = Bitwise.IntValToBinStr(PortNumVal0)
PortVal1 = Bitwise.IntValToBinStr(PortNumVal1)
WritePortsVal = I2C.I2C_Send_2_Bytes(DevNum, "00000010", PortVal0, PortVal1)
Exit Function
greska:
WritePortsVal = False
MsgBox "WritePorts: " & Err.Number & Err.Description
End Function

Public Function ReadPort0Val(ByVal DevNum As Byte) As Byte
On Error GoTo greska
Dim str As String
str = I2C.I2C_Read_Byte(DevNum, "00000000")
ReadPort0Val = Bitwise.BinStrToIntVal(str)
Exit Function
greska:
ReadPort0Val = -1
MsgBox "ReadPort0Val: " & Err.Number & Err.Description
End Function

Public Function ReadPort1Val(ByVal DevNum As Byte) As Byte
On Error GoTo greska
Dim str As String
str = I2C.I2C_Read_Byte(DevNum, "00000001")
ReadPort1Val = Bitwise.BinStrToIntVal(str)
Exit Function
greska:
ReadPort1Val = -1
MsgBox "ReadPort1Val: " & Err.Number & Err.Description
End Function

Public Function ReadPortsVal(ByVal DevNum As Byte) As PortsType
On Error GoTo greska
Dim str As String
Dim PortNumVal0 As Integer
Dim PortNumVal1 As Integer
str = I2C.I2C_Read_2_Bytes(DevNum, "00000000")
PortNumVal0 = Bitwise.BinStrToIntVal(Left(str, 8))
PortNumVal0 = Bitwise.BinStrToIntVal(Right(str, 8))
ReadPortsVal.P0 = PortNumVal0
ReadPortsVal.P1 = PortNumVal1
Exit Function
greska:
ReadPortsVal.P0 = -1
ReadPortsVal.P1 = -1
MsgBox "ReadPortsVal: " & Err.Number & Err.Description
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   write and read P0 and P1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Public Function WriteP0() As Boolean
On Error GoTo greska
WriteP0 = WritePort0Val(DeviceNumber, Ports.P0)
Exit Function
greska:
WriteP0 = False
MsgBox "WriteP0: " & Err.Number & Err.Description
End Function

Public Function WriteP1() As Boolean
On Error GoTo greska
WriteP1 = WritePort1Val(DeviceNumber, Ports.P1)
Exit Function
greska:
WriteP1 = False
MsgBox "WriteP1: " & Err.Number & Err.Description
End Function

Public Function WritePs() As Boolean
On Error GoTo greska
WritePs = WritePortsVal(DeviceNumber, Ports.P0, Ports.P1)
Exit Function
greska:
WritePs = False
MsgBox "WritePs: " & Err.Number & Err.Description
End Function

Public Function ReadP0() As Byte
On Error GoTo greska
ReadP0 = ReadPort0Val(DeviceNumber)
Exit Function
greska:
ReadP0 = -1
MsgBox "ReadP0: " & Err.Number & Err.Description
End Function

Public Function ReadP1() As Byte
On Error GoTo greska
ReadP1 = ReadPort1Val(DeviceNumber)
Exit Function
greska:
ReadP1 = -1
MsgBox "ReadP1: " & Err.Number & Err.Description
End Function

Public Function ReadPs() As PortsType
On Error GoTo greska
ReadPs = ReadPortsVal(DeviceNumber)
Exit Function
greska:
ReadPs.P0 = -1
ReadPs.P1 = -1
MsgBox "ReadPs: " & Err.Number & Err.Description
End Function


Public Function ChangeP0(ByVal pin As Integer, valp As Byte) As Boolean
On Error GoTo greska
If valp = 1 Then
Ports.P0 = Ports.P0 Or pin
Else
Ports.P0 = Ports.P0 And (&HFF - pin)
End If
ChangeP0 = True
Exit Function
greska:
ChangeP0 = False
MsgBox "ChangeP0: " & Err.Number & Err.Description
End Function

Public Function ChangeP1(ByVal pin As Integer, valp As Byte) As Boolean
On Error GoTo greska
If valp = 1 Then
Ports.P1 = Ports.P1 Or pin
Else
Ports.P1 = Ports.P1 And (&HFF - pin)
End If
ChangeP1 = True
Exit Function
greska:
ChangeP1 = False
MsgBox "ChangeP1: " & Err.Number & Err.Description
End Function

Public Function SetValP0(ByVal valp As Byte) As Boolean
On Error GoTo greska
Ports.P0 = valp
SetValP0 = True
Exit Function
greska:
SetValP0 = False
MsgBox "SetValP0: " & Err.Number & Err.Description
End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -