/*
将键盘的值以1200bps发送到计算机,在计算机上显示。
*/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar flag,temp,num,a;
void init()
{
TMOD=0x20; //
timer1 mode2 8bit reload
TH1=0xE8; //1200bps
TL1=0xE8;
TR1=1; //open timer1
REN=1; //admit serial receive data
SM0=0; //10bit asynchronous
SM1=1;
EA=1;
ES=1; //open serial
}
void delay(uchar t) //delay function
{
uint i;
for(t;t>0;t--)
{
for(i=115;i>0;i--);
}
}
uchar keyscan();
void main()
{
init();
while(1)
{
ES=0;
//怎样让串口只输出一个字符?
SBUF=keyscan();
while(!TI);
TI=0;
ES=1;
}
}
uchar keyscan()
{
P3=0xfe; //将P3第一口拉低
temp=P3;
temp=temp&0xf0; //检测P3口
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee: num=1;
break;
case 0xde: num=2;
break;
case 0xbe: num=3;
break;
case 0x7e: num=4;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd; //将P3第2口拉低
temp=P3;
temp=temp&0xf0; //检测P3口
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed: num=5;
break;
case 0xdd: num=6;
break;
case 0xbd: num=7;
break;
case 0x7d: num=8;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb; //将P3第3口拉低
temp=P3;
temp=temp&0xf0; //检测P3口
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb: num=9;
break;
case 0xdb: num=10;
break;
case 0xbb: num=11;
break;
case 0x7b: num=12;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7; //将P3第4口拉低
temp=P3;
temp=temp&0xf0; //检测P3口
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7: num=13;
break;
case 0xd7: num=14;
break;
case 0xb7: num=15;
break;
case 0x77: num=16;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
return num;
}
嗯,提醒到我了,我在主函数中加了一条while语句,虽然能单个显示了,但是有时候不正确
void main()
{
init();
while(1)
{
ES=0;
while(keyscan()==keyscan());
SBUF=keyscan();
while(!TI);
TI=0;
ES=1;
}
}
1.num 既然是全局变量,感觉没必要return吧。
2.keyscan函数中按键弹起后flag置1,无按键置0
3. out函数只是检测是否输出完成
4.main中while改动较大
- /*
- 将键盘的值以1200bps发送到计算机,在计算机上显示。
- */
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar flag,temp,num,a,num_temp;
- void init()
- {
- TMOD=0x20; //timer1 mode2 8bit reload
- TH1=0xE8; //1200bps
- TL1=0xE8;
- TR1=1; //open timer1
- REN=1; //admit serial receive data
- SM0=0; //10bit asynchronous
- SM1=1;
- EA=1;
- ES=1; //open serial
- }
- void delay(uchar t) //delay function
- {
- uint i;
- for(t;t>0;t--)
- {
- for(i=115;i>0;i--);
- }
- }
- void out(uchar ch)
- {
- SBUF=ch;
- while(!TI);
- TI=0;
- }
- uchar keyscan();
- void main()
- {
- init();
- while(1)
- {
- keyscan();
- if(flag==1)
- {
- ES=0;
- flag=0;
- if(num<10)
- out(num+48); //output 1-9
- else
- out(num+55); //output A-F
- out('
');
- out('
');
- ES=1;
- }
- }
- }
- void keyscan()
- {
- P3=0xfe; //将P3第一口拉低
- temp=P3;
- temp=temp&0xf0; //检测P3口
- while(temp!=0xf0)
- {
- delay(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xee: num=1;
- break;
- case 0xde: num=2;
- break;
- case 0xbe: num=3;
- break;
- case 0x7e: num=4;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- flag=1; //表示有按键
- }
- }
- P3=0xfd; //将P3第2口拉低
- temp=P3;
- temp=temp&0xf0; //检测P3口
- while(temp!=0xf0)
- {
- delay(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xed: num=5;
- break;
- case 0xdd: num=6;
- break;
- case 0xbd: num=7;
- break;
- case 0x7d: num=8;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- flag=1; //表示有按键
- }
- }
- P3=0xfb; //将P3第3口拉低
- temp=P3;
- temp=temp&0xf0; //检测P3口
- while(temp!=0xf0)
- {
- delay(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xeb: num=9;
- break;
- case 0xdb: num=10;
- break;
- case 0xbb: num=11;
- break;
- case 0x7b: num=12;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- flag=1; //表示有按键
- }
- }
- P3=0xf7; //将P3第4口拉低
- temp=P3;
- temp=temp&0xf0; //检测P3口
- while(temp!=0xf0)
- {
- delay(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xe7: num=13;
- break;
- case 0xd7: num=14;
- break;
- case 0xb7: num=15;
- break;
- case 0x77: num=16;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- flag=1; //表示有按键
- }
- }
- flag=0; //表示无按键
- }
复制代码评分
查看全部评分
一周热门 更多>