/*
将键盘的值以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;
}
模块化固然是好,只是现在实力没有,只能一步一步来了
一周热门 更多>