' 定義變量
Dim Row(4) As Byte ' 定義一個長度為4的字節數組,用于存儲每行的掃描碼
Dim Ar(4) As Byte ' 定義一個長度為4的字節數組,用于存儲發送給1-Wire設備的命令和數據
Dim a As Byte ' 定義一個字節變量a,用于臨時存儲數據
Dim Key As Byte ' 定義一個字節變量Key,用于存儲檢測到的按鍵值
Dim Rowlus As Byte ' 定義一個字節變量Rowlus,用于循環遍歷每一行
1wwrite Ar(1), 4 ' 向1-Wire設備寫入命令和數據
Ar(1) = 1wread(2) ' 從1-Wire設備讀取響應(通常是按鍵的標識)
A = Ar(2) And &B00011110 ' 提取響應中的按鍵標識位
' 根據響應判斷按鍵值
Select Case A
Case 30: Key = 0 ' 無按鍵按下
Case 28:
' 根據行號確定具體的按鍵值
If Rowlus = 1 Then Key = 16
If Rowlus = 2 Then Key = 9
If Rowlus = 3 Then Key = 8
If Rowlus = 4 Then Key = 7
Case 26:
If Rowlus = 1 Then Key = 15
If Rowlus = 2 Then Key = 6
If Rowlus = 3 Then Key = 5
If Rowlus = 4 Then Key = 4
Case 22:
If Rowlus = 1 Then Key = 14
If Rowlus = 2 Then Key = 3
If Rowlus = 3 Then Key = 2
If Rowlus = 4 Then Key = 1
Case 14:
If Rowlus = 1 Then Key = 13
If Rowlus = 2 Then Key = 12
If Rowlus = 3 Then Key = 11
If Rowlus = 4 Then Key = 10
Case Else: Key = 0 ' 其他情況視為無按鍵按下
End Select
' 如果檢測到按鍵,則退出循環
If Key > 0 Then Exit For
Next Rowlus
Return ' 返回主程序