PS键盘信号接收程序

#include <iom8v.h>
#include 
<macros.h>

/*-----------------*
* PC0 ------ DATA  *
* PC3 ------ CLOCK *
* PD0~7 --- LED1~8 *
*-----------------
*/


#define PC0          0
#define PC1          1
#define PC2          2
#define PC3          3
#define PC4          4
#define PC5          5
#define PC6          6
#define PC7          7
#define CLOCK_HIGH (PORTC |= BIT(PC3))        //设置时钟为高电平
#define CLOCK_LOW (PORTC &= ~BIT(PC3))        //设置时钟为低电平
#define CUR_CLOCK (PINC & BIT(PC3) ? BIT(0) : 0x00)        //获取当前时钟状态
#define CUR_DATA (PINC & BIT(PC0) ? BIT(0) : 0x00)        //获取当前数据状态
#define LED_DATA PORTD        //定义LED灯接口


UINT8 Get_A_Bit()        
//获取当前键盘正在发送的一位
{
    
while(CUR_CLOCK == 0x00)    //跳过低电平
    {;}
    
while(CUR_CLOCK != 0x00)    //跳过高电平,等待下降沿
    {;}
    
    
return CUR_DATA;    //返回当前数据值
}


void main()
{
    UINT8 chData 
= 0;        //存值
    UINT8 chCount = 0;        //循环控制
    
    Device_Init();
    CLOCK_LOW;                
//抑制键盘输出
    
    
while(1)
    
{    
        CLOCK_HIGH;            
//开始允许键盘输出
        
        Get_A_Bit();        
//获取开始位
        
        chData 
= 0x00;        //初始化数据
        for(chCount = 0; chCount < 8; chCount++)    //连续获取八位数据并保存
        {
            chData 
|= Get_A_Bit() << chCount;        //数据从低位开始传输
        }

        
        LED_DATA 
= ~chData;            //置反以便LED灯显示
        
        
for(chCount = 0; chCount < 2; chCount++)    //丢弃最后两位
        {
            Get_A_Bit();
        }

        
        CLOCK_LOW;            
//键盘停止输出
    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值