单片机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2949|回复: 0
收起左侧

单片机PS/2协议的应用——PS/2的键盘程序

[复制链接]
daming 发表于 2014-12-30 23:40 | 显示全部楼层 |阅读模式
/*

     PS/2协议的应用:(常见的有PS/2接口鼠标、键盘等)用PS/2的键盘控制单片机,用字符型液晶显示器1602简单

                                        显示键盘的输入值

                                     由于at89s52单片机的工作频率过低,不易扫描更多键值


*/

  1. #include"reg52.h"
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. sbit psda=P3^0;
  5. uchar keyvalue;
  6. uint keynum;
  7. uchar BIT;
  8. uchar temp;
  9. uint j=0;
  10. uchar  table1[3];
  11. sbit lcden=P2^6;
  12. sbit lcdrs=P2^4;
  13. sbit lcdrw=P2^5;
  14. sbit dula=P3^7;
  15. uchar num;
  16. uchar code table2[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'`',        //0-15
  17.       0,0,0,0,0,'Q','1',0,0,0,'Z','S','A','W','2',0,        //16-31
  18.       0,'C','X','D','E','4','3',0,0,0x20,'V','F','T','R','5',0,      //32-47
  19.       0,'N','B','H','G','Y','6',0,0,0,'M','J','U','7','8',0, //48-63
  20.       0,',','K','I','O','0','9',0,0,'.','/','L',';','P','-',0,//64-79
  21.       0,0,0,0,'[','=',0,0,0,0,0,']',0,'|',0,0,               //80-95
  22.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                //96-111
  23.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,         //112-127
  24.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,              //128-143
  25.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,           //144-159
  26.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,          //160-175
  27.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,         //176-191
  28.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,       //192-207
  29.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,          //208-223
  30.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        //224-239
  31.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0          //240-255
  32.      };
  33. void delay(uint z)
  34. {
  35. uint x,y;
  36. for(x=z;x>0;x--)
  37.   for(y=11;y>0;y--);
  38. }
  39. void write_com(uchar com)
  40. {
  41. lcdrs=0;
  42. P0=com;
  43. delay(5);
  44. lcden=1;
  45. delay(10);
  46. lcden=0;
  47. delay(10);

  48. }
  49. void write_data(uchar _data)
  50. {
  51. lcdrs=1;
  52. P0=_data;
  53. delay(5);
  54. lcden=1;
  55. delay(10);
  56. lcden=0;
  57. delay(10);
  58. }
  59. void _init_()
  60. {
  61. IT0=1;//外部中断下降沿触发
  62. EX0=1;//开外部0中断
  63. EA=1;//开总中断
  64. dula=1;
  65. lcdrw=0;
  66. write_com(0x01);
  67. write_com(0x38);//指令(001 DL N F 00)表示:DL=1,8数据位;N=1,位移宽度为2倍;F=1,5*10;F=0,5*7;
  68. write_com(0x0f);//指令(00001DCB)表示:D=1,开显示;C=1,显示光标;B=1,光标闪烁
  69. write_com(0x06);//指令(000001[I/D]S)表示:N=1,地址加一;[I/D]=0,地址减一;S=1,开整屏移动
  70. write_com(0x80);//第一行:0x80+(字符序号【0--f】);第二行:0xc0+(字符序号【0--f】)

  71. }void main()
  72. {
  73. _init_();
  74. while(1)
  75. {
  76.   if(keynum==3)
  77.   {
  78.    keynum=0;
  79.    temp=table1[0];
  80.    if((j%32>0)&&(j%16==0))
  81.    {
  82.     write_com(0xc0);
  83.    }
  84.    if((j%32==0)&&(j%16==0))
  85.    {write_com(0x80);}
  86.    write_data(table2[temp]);
  87.    j++;
  88.   }
  89.   

  90. }
  91. }
  92. void int0 () interrupt 0
  93. {
  94. if((BIT>0)&&(BIT<9))
  95. {
  96.   keyvalue=keyvalue>>1;
  97.   if(psda)
  98.    keyvalue=keyvalue|0x80;
  99. }
  100. BIT++;
  101. if(BIT>10)
  102. {
  103.   BIT=0;
  104.   table1[keynum]=keyvalue;
  105.   keynum++;

  106. }

  107. }
复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|单片机论坛 |51Hei单片机16群 联系QQ:125739409;技术交流QQ群7344883

Powered by 单片机教程网

快速回复 返回顶部 返回列表