📄 mainmodule.bas
字号:
Attribute VB_Name = "MainModule"
Option Explicit
Public TimeD As Long
Public SCL As Integer
Public EN As Integer
Public EN_Level As Integer
Public Scl_Level As Integer
Public SDO As Integer
Public Sdo_Level As Integer
Public SDI As Integer
Public Sdi_Level As Integer
Public LPTAddressBase As Integer
Public ICStr(10) As String
'StatusAddress=Base+1
'ControlAddress=Base+2
'LPT1 Base=&H378
Public LPTOutData As Integer
Public Declare Function Inp Lib "inpout32.dll" _
Alias "Inp32" (ByVal PortAddress As Integer) As Integer
Public Declare Sub Out Lib "inpout32.dll" _
Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)
'Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" (ByVal LPTAddress As Integer) As Integer
'Public Declare Function Out Lib "inpout32.dll" Alias "Out32" (ByVal LPTAddressBase As Integer, ByVal OutData As Integer) As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub SetBit(ByVal BitPos As Integer, ByVal Level As Integer)
Dim t As Integer
t = BitPos Mod 8
t = 2 ^ t
LPTOutData = LPTOutData Mod 256
LPTOutData = 2 * t * (LPTOutData \ (2 * t)) + (LPTOutData Mod t) + Level * t
Out LPTAddressBase, LPTOutData
End Sub
Public Sub I_Start()
SetSCL
Delay 5
SetSDA
Delay 5
ClrSDA
Delay 5
ClrSCL
Delay 5
End Sub
Public Sub Delay(ByVal L As Long)
Dim i, j As Long
For i = 0 To L
For j = 0 To TimeD
Next j
Next i
End Sub
Public Sub I_Stop()
ClrSCL
Delay 5
ClrSDA
Delay 5
SetSCL
Delay 5
SetSDA
Delay 5
End Sub
Public Function I_Send(ByVal d As Integer) As Boolean
Dim i, t As Integer
t = d Mod &H100
For i = 0 To 7
If (t \ &H80) = 1 Then
SetSDA
Else
ClrSDA
End If
t = (t * 2) Mod &H100
I_Clock
Delay 5
Next i
SetSDA
Delay 5
I_Send = Not I_Clock
End Function
Public Sub SetSDA()
SetBit SDO, Sdo_Level Mod 2
End Sub
Public Sub SetSCL()
SetBit SCL, Scl_Level Mod 2
End Sub
Public Sub ClrSDA()
SetBit SDO, 1 - (Sdo_Level Mod 2)
End Sub
Public Sub ClrSCL()
SetBit SCL, 1 - (Scl_Level Mod 2)
End Sub
Public Function I_Clock() As Boolean
Delay 5
SetSCL
Delay 10
I_Clock = ReadSDA
Delay 5
ClrSCL
Delay 5
End Function
Public Function ReadSDA() As Boolean
Dim t, t1 As Integer
Dim r As Boolean
t = Inp(LPTAddressBase + 1)
t1 = SDI Mod 8
t1 = 2 ^ t1
t = t \ t1
t = t Mod 2
If t = 1 Then
r = True
Else
r = False
End If
If (Sdi_Level Mod 2) = 1 Then
ReadSDA = r
Else
ReadSDA = Not r
End If
End Function
Public Function I_Receive() As Byte
Dim i, Res As Byte
Res = 0
ClrSCL
Delay 5
SetSDA
Delay 5
For i = 0 To 7
Res = Res * 2
If I_Clock Then
Res = Res + 1
End If
Next i
I_Receive = Res
End Function
Public Sub I_NoAck()
SetSDA
Delay 5
I_Clock
End Sub
Public Sub I_Ack() 'Low Level is ACK
ClrSDA
Delay 5
I_Clock
End Sub
Public Sub WriteBIntoByteAddr(ByVal addr As Integer, ByVal SubAddr As Integer, ByVal d As Byte)
I_Stop
I_Start
I_Send addr
I_Send SubAddr
I_Send d
I_Stop
End Sub
Public Sub WriteBIntoWordAddr(ByVal addr As Integer, ByVal SubAddrH As Integer, ByVal SubAddrL As Integer, ByVal d As Integer)
I_Stop
I_Start
I_Send addr
I_Send SubAddrH
I_Send SubAddrL
I_Send d
I_Stop
End Sub
Public Sub WriteAIntoByteAddr(ByVal addr As Integer, ByVal SubAddr As Integer, ByVal n As Byte, ByRef d() As Integer)
Dim i As Byte
I_Stop
I_Start
I_Send addr
I_Send SubAddr
For i = 1 To n
I_Send d(i)
Next i
I_Stop
End Sub
Public Sub WriteAIntoWordAddr(ByVal addr As Integer, ByVal SubAddrH As Integer, ByVal SubAddrL As Integer, ByVal n As Byte, ByRef d() As Integer)
Dim i As Byte
I_Stop
I_Start
I_Send addr
I_Send SubAddrH
I_Send SubAddrL
For i = 1 To n
I_Send d(i)
Next i
I_Stop
End Sub
Public Sub Delay_ms(ByVal n As Long)
Sleep n
End Sub
Public Sub Main()
'Init
TimeD = 5
ICStr(0) = "24C00"
ICStr(1) = "24C01"
ICStr(2) = "24C02"
ICStr(3) = "24C04"
ICStr(4) = "24C08"
ICStr(5) = "24C16"
ICStr(6) = "24C32"
ICStr(7) = "24C64"
ICStr(8) = "24128"
ICStr(9) = "24256"
ICStr(10) = "24512"
frmLoadWR24.Show
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -