各位大佬,我想通过8255的A口接键盘控制B,C口的高低电平,该怎么改程序?,各位大佬,我想通过8255的A口接键盘控制B,C口的高低电平(例如,第一个按键按下后,PB0变为低电平,再按一下,PB0变为高电平),我应该怎么该程序
- #include<reg52.h>
- #include<absacc.h>
- #define PA XBYTE[0x7cff] //A口地址定义
- #define PB XBYTE[0x7dff] //B口地址定义
- #define PC XBYTE[0x7eff] //C口地址定义
- #define COM XBYTE[0x7fff] //控制地址定义
- void DelayM(unsigned int x) //延时子程序
- {
- unsigned char t;
- while(x–)
- {
- for(t=0;t<120;t++);
- }
- }
- unsigned char keyscan(void) //键盘扫描
- {
- unsigned temp,keynum;
- PA=0xfe;
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- DelayM(20);
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=PA;
- switch(temp)
- {
- case 0xee:keynum=0;PB=0xfe;break;
- case 0xde:keynum=1;PB=0xfd;break;
- case 0xbe:keynum=2;PB=0xfb;break;
- case 0x7e:keynum=3;PB=0xf7;break;
- }
- while((temp&0xf0)!=0xf0)
- {
- temp=PA;
- }
- }
- }
- PA=0xfd;
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- DelayM(20);
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=PA;
- switch(temp)
- {
- case 0xed:keynum=4;PB=0xef;break;
- case 0xdd:keynum=5;PB=0xdf;break;
- case 0xbd:keynum=6;PB=0xbf;break;
- case 0x7d:keynum=7;PB=0x7f;break;
- }
- while((temp&0xf0)!=0xf0)
- {
- temp=PA;
- }
- }
- }
- PA=0xfb;
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- DelayM(20);
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=PA;
- switch(temp)
- {
- case 0xeb:keynum=8;PC=0xfe;break;
- case 0xdb:keynum=9;PC=0xfd;break;
- case 0xbb:keynum=10;PC=0xfb;break;
- case 0x7b:keynum=11;PC=0xf7;break;
- }
- while((temp&0xf0)!=0xf0)
- {
- temp=PA;
- }
- }
- }
- PA=0xf7;
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- DelayM(20);
- temp=PA;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- temp=PA;
- switch(temp)
- {
- case 0xe7:keynum=12;PC=0xef;break;
- case 0xd7:keynum=13;PC=0xdf;break;
- case 0xb7:keynum=14;PC=0xbf;break;
- case 0x77:keynum=15;PC=0x7f;break;
- }
- while((temp&0xf0)!=0xf0)
- {
- temp=PA;
- }
- }
- }
- }
- void main()
- {
- COM=0x90; //方式0,A口为输入,B,C口为输出
- PA=0xff;
- PB=0xff;
- PC=0xff;
- while(1)
- {
- keyscan();
- }
- }
复制代码