标题: ds2408—一根线的4x4键盘 [打印本页]

作者: taotie    时间: 2024-10-26 23:29
标题: ds2408—一根线的4x4键盘


$regfile = "m8def.dat" ' 定义一个配置文件名称  
$crystal = 8000000 ' 定义晶振频率为8MHz  
$BAUD = 9600 ' 定义波特率为9600  

Config 1wire = Portc.5 ' 配置1-Wire通信在Portc的第5脚上  

' 定义变量  
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,用于循环遍历每一行  

' 主循环,不断读取键盘按键  
do  
    gosub Readkeypad ' 调用Readkeypad子程序读取按键  
    print key ' 打印按键值  
loop  

' Readkeypad子程序:读取键盘按键  
Readkeypad:

    ' 初始化Row数组,每个元素都是一个二进制掩码,用于激活对应的键盘行  
    Row(1) = &B01111111   
    Row(2) = &B10111111     
    Row(3) = &B11011111   
    Row(4) = &B11111110   

    ' 遍历每一行  
    For Rowlus = 1 To 4   

        1wreset  ' 重置1-Wire总线  

        ' 准备发送给1-Wire设备的命令和数据  
        Ar(1) = &HCC  ' 跳过ROM命令  
        Ar(2) = &H5A   ' 匹配ROM命令的一部分(此处用于检测按键,实际可能不发送完整匹配命令)  
        Ar(3) = Row(Rowlus)  ' 当前行的激活码  
        Ar(4) = &HFF - Ar(3) ' 计算校验码  

        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 ' 返回主程序


      
   



作者: 两仪式    时间: 2024-10-27 08:03
这芯片太贵了。。。搜到的都是2 30
作者: szb314    时间: 2024-10-28 08:14
两仪式 发表于 2024-10-27 08:03
这芯片太贵了。。。搜到的都是2 30

买一片1块钱的stc 一样可以




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1