⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ps2.h

📁 单片机开发,键盘口通信驱动,发送数据到主机
💻 H
字号:
/***具体详见《PS/2技术参考》***/
#include "reg51.h"

#define LOW  0
#define HIGH 1

sbit  G_CLOCK=P3^3;
sbit  G_DATA=P3^2;
sbit  Enable_Keyboard=P0^6;

/**********************************************************************
***          微秒级延时,延时时间根据晶振的具体值给予设定            ***
**********************************************************************/
void Delay_Nus(unsigned char x);

/**********************************************************************
***********************************************************************
***********************************************************************
***   发送一位(bit)到主机,发送的过程如下:                           ***
***   1):设置/复位数据位                                            ***
***   2):延时大约20微秒                                             ***
***   3):把时钟线置为低电平                                         ***
***   4):延时大约40微秒                                             ***
***   5):释放时钟线-把时钟线置为高电平                             ***
***   6):延时大约20微秒                                             ***
***********************************************************************
***********************************************************************
**********************************************************************/
void Send_Bit_To_Host(bit x);

/**********************************************************************
***********************************************************************
***********************************************************************
***   发送一个字节(byte)到主机,发送过程如下:                        ***
***   1):等待时钟线为高电平(Clock = High)                           ***
***   2):延时50微秒                                                 ***
***   3):判断时钟线是还仍为高电平,否则转到1)                        ***
***   4):数据线是否为高(Data = High),若不为高则放弃发送;并转到      ***
***      接收主机数据函数                                           ***
***   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.)                                    ***
**********************************************************************************************************************************************
**********************************************************************/
void Send_Byte_To_Host(unsigned char x);

/**********************************************************************
***********************************************************************
***********************************************************************
***   发送一个数据到主机,发送过程如下:                              ***
***   1):发送该数据的接通码到主机                                   ***
***   2):发送该数据的断开码到主机                                   ***
***********************************************************************
***********************************************************************
**********************************************************************/
void Send_Data_To_Host(unsigned char x);



/******************************程序代码:******************************/

void Delay_Nus(unsigned char x)
{			    
   while(--x);   
}

void Send_Bit_To_Host(bit x)
{
   if(x==1)
      G_DATA=1;
   else
      G_DATA=0;
   Delay_Nus(30);
   G_CLOCK=LOW;
   Delay_Nus(68);
   G_CLOCK=HIGH;
   Delay_Nus(30);
}

void Send_Byte_To_Host(unsigned char x)
{
   unsigned char i,temp,temp_bit;
   bit flag_check=1;
   temp=x;
   G_CLOCK=1;
   do
   {
      while(G_CLOCK!=HIGH || G_DATA!=HIGH);
	  Delay_Nus(80);
   }while(!G_CLOCK || !G_DATA);
   G_DATA=1;
   if(G_CLOCK==HIGH && G_DATA==HIGH)
   {
      Delay_Nus(63);
	  Send_Bit_To_Host(0);
	  for(i=0;i<8;i++)
	  {
	     temp_bit=temp&0x01;
		 Send_Bit_To_Host(temp_bit);
		 if(temp_bit==0x01)
		 {
		    flag_check=!flag_check;
		 }
		 temp=temp>>1;
	  }
	  Send_Bit_To_Host(flag_check);
	  Send_Bit_To_Host(1);
	  Delay_Nus(46);
   }
}

void Send_Data_To_Host(unsigned char x)
{
   Enable_Keyboard=0;
   Send_Byte_To_Host(x);
   Send_Byte_To_Host(0xf0);
   Send_Byte_To_Host(x);
   Enable_Keyboard=1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -