导航: 老古网老古论坛XMOS公共讨论区XMOS开源项目区单片机程序设计嵌入式系统广告区域
→请教:请各位大哥,这是我写的一个 键盘程序 (ps/2),可[npsgsys]

 *第38453篇: 请教:请各位大哥,这是我写的一个 键盘程序 (ps/2),可是不正确!

  
楼 主:npsgsys 2006年3月13日13:55
 请教:请各位大哥,这是我写的一个 键盘程序 (ps/2),可是不正确!
/**********************************************************************
这是我自己做的一个键盘(相当于标准的 PC 键盘,插在 PC 的 PS/2 口)
现在我的这个键盘还只有一个按钮,我希望按下这个按钮就相当于在标准的
PC 键盘上按下 "w" ,这个按钮接在 P3.3 上,当按下这个按钮,就
把 P3.3 拉低,外部中断1 的下降沿触发。


下面是 PS/2 协议:
我推荐下面的过程发送一个单一字节从仿真键盘/鼠标到主机
1) 等待Clock = high
2) 延时 50 微秒
3) Clock s 仍旧为 high?
No—到第1 步
4) Data = high?
No—放弃 (并且从主机读取字节)
5) 延迟 20 毫秒 (=40 微秒 to the time Clock is pulled low in sending the start bit.)
6) 输出起始位 (0) \ 在发送所有这些位的每一位后
7) 输出 8 个数据位 >   测试时钟确认主机是否把它拉低了
8) 输出校验位 / 这说明主机要放弃这次传送
9) 输出停止位 (1)
10) 延迟30 毫秒 (=50 微秒 from the time Clock is released in sending the stop bit)

************************************************************************/

#include "reg52.h"
#include "intrins.h"

sbit P2_0 = P2^2; //数据线
sbit P2_5 = P2^5; //时钟线
sbit P3_3 = P3^3; //外部中断

static unsigned char IntNum = 0; //中断次数计数
static unsigned char KeyV; //键值

int Check_status( ); //检查状态
void SendMessage( ); //向 PC 机的 PS/2 口发送数据
void Send_Start_Bit( ); //发送起始位
void Send_Data_Bit( ); //发送数据位
void Send_Check_Bit( );//发送效验位
void Send_End_Bit( );//发送停止位


void Delay50us( ); //延时 50 微秒
void Delay20ms( ); //延时 20 毫秒
void Delay30ms( ); //延时 30 毫秒


void main()
{
  P2_5 = 1; //把时钟线置为高电平
  P2_0 = 1; //把数据线置为高电平
  
  IT1 = 1; //设外部中断1为下降沿触发
  EA = 1;  //开中断
  EX1 = 1; //开中断
  while(1)
  { }
}

void Delay50us( ) //延时 50 微妙
{
    int i;
    for (i=0;i  <50;i++)
    {
         _nop_();
    }
}    
         
void Delay20ms( ) //延时 20 毫秒
{
    int i,j;
    for (i=0;i  <200;i++)
    {
        for (j=0;j  <100;j++)
            {
                _nop_();
            }    
    } 
}    

void Delay30ms( ) //延时 30 毫秒
{
    int i,j;
    for (i=0;i  <300;i++)
    {
        for (j=0;j  <100;j++)
            {
                _nop_();
            }    
    } 
}    

void Send_Start_Bit( ) //发送起始位
{
    P2_0 = 0 ; //起始位的值
    P2_5 = 0;  //发送起始位
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    P2_5 = 1; //上拉
}

void Send_Data_Bit( ) //发送数据位
{
    int i ;//循环 8 次
    unsigned char Strings = 0x44 ;  //要发送的数据
    unsigned char Temp ;// 这个变量得到的是要发送的位
    
  for (i=0;i  <8;i++)
      {
          Temp = Strings & 0x01;
        P2_0 = Temp ; //发送位的值
          _nop_();
            _nop_();
            _nop_();
            _nop_();
            _nop_();
          P2_5 = 0;     //发送数据位
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          P2_5 = 1;      //上拉
          Strings = Strings >  >   1; //右移一位
      }
}

        
void Send_Check_Bit( )//发送效验位
{
  P2_0 = 1 ; //效验位的值
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    P2_5 = 0;  //发送效验位
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    P2_5 = 1; //上拉
}


void Send_End_Bit( ) //发送停止位
{
  P2_0 = 1 ; //停止位的值
    _nop_();
    _nop_();
    _nop_();
    _nop_();    
    _nop_();
    P2_5 = 0;  //发送停止位
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    P2_5 = 1; //上拉
}              
          

    
void Keyboard_out(void) interrupt 2  //按下接在 P3.3 的按钮

    int IsOrNoFlag = 1 ;//时钟线此时是否为高的标志 ,如果为高则把其置为 0 
    
    while(IsOrNoFlag)
          {
            if ( P2_5 == 1) //如果此时时钟线为高
                {
                  IsOrNoFlag = 0; //时钟线为高,则置标志为 0
                  Delay50us( ); //延时 50 微妙
                  if ( P2_0 == 1 ) //如果此时数据线为高 
                      {
                        Delay20ms( ); //延时 20 毫秒
                        Send_Start_Bit( ); //输出起始位
                        Send_Data_Bit( ); //发送数据位
                        Send_Check_Bit( );//发送效验位
                        Send_End_Bit( ); //发送结束位
                        Delay30ms( ); //延时 30 毫秒
                      }
                 }      
       }
             
}             
              


请教:
启动 windows 系统后,打开写字板,然后按下这个按钮,可是不显示任何字符啊?
各位大哥,我的程序是错误在哪里啊?

还有硬件方面,我的 RST/Vpd 引脚 是用导线和电源的正级连接起来的,
我的这个电路板不是用 PC 机的电源供电,而是用我自己的外部电源。

还有,我没有看们狗的。是不是我的硬件这样连接不行啊?


谢谢!

此主题相关图片如下:
按此在新窗口浏览图片


>>>>>>对该主题发表你的看法

本主题贴数1,分页: [第1页]


[上一篇主题]:各位大侠,小妹急需单片机控制U盘/硬盘的程序!!拜托了,给指点一下吧!!

[下一篇主题]:请问arm9视频采集显示