欢迎来到天天文库
浏览记录
ID:19237705
大小:52.00 KB
页数:12页
时间:2018-09-30
《用51单片机制作ps2接口的工业小键盘》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、用51单片机制作ps2接口的工业小键盘经过我的验证,是完全可行的。#include#include#defineucharunsignedchar#defineSEM_FULL1#defineSEM_EMPTY0ucharbdataps2char;ucharbdatakeystatus;ucharbdataparity_buf;ucharbdatas_buf,r_buf;uchardatalast_s;sbitdate=P3^0;sbitclock=P3^2;sbitkey0=P2^0;sb
2、itkey1=P2^1;sbitkey2=P2^2;sbitkey3=P2^3;sbitkey4=P2^4;sbitkey5=P2^5;sbitkey6=P2^6;sbitkey7=P2^7;sbits_buf0=s_buf^0;sbitr_buf7=r_buf^7;sbitparity_buf0=parity_buf^0;bitps2_tx_sem=SEM_EMPTY;bitps2_sendbyte(ucharc);voiddelayus(ucharus){while(--us);}voiddelayms(ucharms){uc
3、hari;while(ms--){for(i=0;i<120;i++);}}bitparity(){bitPARITY=1;uchari;for(i=0;i<8;i++){PARITY^=parity_buf0;parity_buf=parity_buf>>1;}returnPARITY;}voidps2_tx_sem_take(){ps2_tx_sem0:EA=0;if(ps2_tx_sem==SEM_FULL){EA=1;delayus(30);delayus(30);gotops2_tx_sem0;}ps2_tx_sem=S
4、EM_FULL;EA=1;return;}voidps2_tx_sem_give(){EA=0;ps2_tx_sem=SEM_EMPTY;EA=1;return;}voidps2_clk_check(){ps_tx_sem:EA=0;if(clock==0)//ifPS2_CLKlow,waitfor50us{EA=1;delayus(30);gotops_tx_sem;}EA=1;return;}voidps2_send(uchardat){ps2_tx_sem_take();ps2_clk_check();if(date==1
5、)ps2_sendbyte(dat);ps2_tx_sem_give();}ucharps2_recbyte()//Returnreceicedata,Errorreturn0{uchari;bitPARITY;r_buf=0;while(!clock);//WaitKBCLKHighdelayus(10);if(date)//KBDATAshoudbelow,meanreceivedstartbit{ps2_send(0xfe);//ifKBDATAisHigh,returnandsenderrorstatusdate(0xfe
6、)return0;}delayus(30);clock=0;//DevicecontroltheKBCLKsignalfor(i=0;i<8;i++)//RisingedgeofKBCLK,writedataintor_buf7{delayus(30);clock=1;delayus(10);if(!clock)return0;//ifKBCLKpulldown,meanHostcancelledthissendingr_buf7=date;if(i!=7)r_buf=r_buf>>1;delayus(10);clock=0;}d
7、elayus(30);clock=1;delayus(10);if(!clock)return0;//ifKBCLKpulldown,meanHostcancelledthissendingPARITY=date;//Receiveoddparitybitdelayus(10);clock=0;delayus(30);clock=1;delayus(10);//ReceiveStopbit,shouldbehighif(!date)//Otherwise,senderrorstatusdate(0xfe){ps2_send(0xf
8、e);return0;}date=0;//SendACKbit,meanhavereceivedatadonedelayus(10);clock=0;delayus(30);clock=1;//SetKBCLKandKBDATAhighdelayu
此文档下载收益归作者所有