Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71305
  • 博文数量: 27
  • 博客积分: 2015
  • 博客等级: 大尉
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 19:36
文章分类

全部博文(27)

文章存档

2011年(1)

2010年(5)

2009年(16)

2008年(5)

我的朋友

分类: C/C++

2009-04-16 15:32:46

//=====================================================================
//                         4*4键盘扫描测试程序
//硬件连接: 4*4键盘的8个引脚接51的P1口
//扫描返回值为0—15,串口收到的值为a—p  
//=====================================================================

#include

#define uint unsigned int
#define uchar unsigned char

uchar Key_Get()
{
    uchar temp;
uchar a;

    a=0xff;
    P1=0xfe;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xfe;temp=P1&0xf0; a=0x00; }
if(temp==0xd0) while(temp==0xd0){ P1=0xfe;temp=P1&0xf0; a=0x04; }
if(temp==0xb0) while(temp==0xb0){ P1=0xfe;temp=P1&0xf0; a=0x08; }
if(temp==0x70) while(temp==0x70){ P1=0xfe;temp=P1&0xf0; a=0x0c; }

P1=0xfd;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xfd;temp=P1&0xf0; a=0x01; }
if(temp==0xd0) while(temp==0xd0){ P1=0xfd;temp=P1&0xf0; a=0x05; }
if(temp==0xb0) while(temp==0xb0){ P1=0xfd;temp=P1&0xf0; a=0x09; }
if(temp==0x70) while(temp==0x70){ P1=0xfd;temp=P1&0xf0; a=0x0d; }

P1=0xfb;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xfb;temp=P1&0xf0; a=0x02; }
if(temp==0xd0) while(temp==0xd0){ P1=0xfb;temp=P1&0xf0; a=0x06; }
if(temp==0xb0) while(temp==0xb0){ P1=0xfb;temp=P1&0xf0; a=0x0a; }
if(temp==0x70) while(temp==0x70){ P1=0xfb;temp=P1&0xf0; a=0x0e; }

P1=0xf7;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xf7;temp=P1&0xf0; a=0x03; }
if(temp==0xd0) while(temp==0xd0){ P1=0xf7;temp=P1&0xf0; a=0x07; }
if(temp==0xb0) while(temp==0xb0){ P1=0xf7;temp=P1&0xf0; a=0x0b; }
if(temp==0x70) while(temp==0x70){ P1=0xf7;temp=P1&0xf0; a=0x0f; }

return(a);   
}

//=====================================================================

void init_serial()    //初始化串口
{
   TMOD=0x20;    //定时器T1使用工作方式2
   TH1=250;     //设置初值
   TH0=250;
   TR1=1;      //开始计时
   PCON=0x80;     //SMOD=1;
   SCON=0x50;     //工作方式1,波特率9600bit/s,允许接收
   TI=1;
}

void main()
{
uchar key;
init_serial();
IE=0; //屏蔽中断
    while(1)
{
    key=Key_Get();
    if(key!=0xff)
    {
       key+=97;
       if(TI==1)
       {
      SBUF=key;
      TI=0;
       }
    }
}
}

阅读(966) | 评论(0) | 转发(0) |
0

上一篇:DS18b20 驱动程序

下一篇:双机通讯1

给主人留下些什么吧!~~