C51单片机-按下K0至K7,显示按键标号;按下K8至K15,显示按键标号的立方(4x4矩阵键盘电路,1个数码管)

keil uVision4界面:

#include<reg51.h>
#define uchar unsigned char 
unsigned char num;
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};    //共阳数码管段码,接P0口
                  // 0    1     2       3   4     5      6       7    8    9     10  11       12    13    14      15   16     */
unsigned char tab8[]={0x92,0xf9,0xa4};//512
unsigned char tab9[]={0xf8,0xa4,0x90};//729
unsigned char tab10[]={0xf9,0xc0,0xc0,0xc0};//1000
unsigned char tab11[]={0xf9,0xb0,0xb0,0xf9};//1331
unsigned char tab12[]={0xf9,0xf8,0xa4,0x80};//1728
unsigned char tab13[]={0xa4,0xf9,0x90,0xf8};//2197
unsigned char tab14[]={0xa4,0xf8,0x99,0x99};//2744
unsigned char tab15[]={0xb0,0xb0,0xf8,0x92};//3375
unsigned char hang[]={0xfe,0xfd,0xfb,0xf7};    //行扫描
unsigned char hanglie[]={0xee,0xde,0xbe,0x7e,
                         0xed,0xdd,0xbd,0x7d,
                         0xeb,0xdb,0xbb,0x7b,
                         0xe7,0xd7,0xb7,0x77};    //行列
uchar code seg1[]={0x08};//  个位                       //接P2口
uchar code seg2[]={0x02,0x04,0x08};//百位
uchar code seg3[]={0x01,0x02,0x04,0x08};//千位          
void delay(int x) //延时函数
{
  int i,j;
  for(i=x;i>0;i--)
  for(j=120;j>0;j--);
}
void shu0()    //0
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0xc0;
     P2=seg1[b]; 
     delay(5);
     P2=0x00; //消影
  }
}
void shu1()    //1
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0xf9;
     P2=seg1[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu2()    //2
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0xa4;
     P2=seg1[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu3()    //3
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0xb0;
     P2=seg1[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu4()    //4
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0x99;
     P2=seg1[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu5()    //5
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0x92;
     P2=seg1[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu6()    //6
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0x82;
     P2=seg1[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu7()    //7
{
  int b;
  for(b=0;b<1;b++)
  {
     P0=0xf8;
     P2=seg1[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu8()    //8的立方=512
{
  int b;
  for(b=0;b<3;b++)
  {
     P0=tab8[b];
     P2=seg2[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu9()    //9的立方=729
{
  int b;
  for(b=0;b<3;b++)
  {
     P0=tab9[b];
     P2=seg2[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu10()    //10的立方=1000
{
  int b;
  for(b=0;b<4;b++)
  {
     P0=tab10[b];
     P2=seg3[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu11()    //11的立方=1331
{
  int b;
  for(b=0;b<4;b++)
  {
     P0=tab11[b];
     P2=seg3[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu12()    //12的立方=1728
{
  int b;
  for(b=0;b<4;b++)
  {
     P0=tab12[b];
     P2=seg3[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu13()    //13的立方=2197
{
  int b;
  for(b=0;b<4;b++)
  {
     P0=tab13[b];
     P2=seg3[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu14()    //14的立方=2744
{
  int b;
  for(b=0;b<4;b++)
  {
     P0=tab14[b];
     P2=seg3[b];
     delay(5);
     P2=0x00; //消影
  }
}
void shu15()    //15的立方=3375
{
  int b;
  for(b=0;b<4;b++)
  {
     P0=tab15[b];
     P2=seg3[b];
     delay(5);
     P2=0x00; //消影
  }
}
void key()
{
   int a;
   for(a=0;a<4;a++)
   {
     P1=hang[a];
     if((P1&0xf0)!=0xf0)//判断有无按键按下
     {
        switch(P1)        //第一种写法
        {
          case 0xee:shu0();break;
          case 0xde:shu1();break;
          case 0xbe:shu2();break;
          case 0x7e:shu3();break;//扫描第一行
          case 0xed:shu4();break;
          case 0xdd:shu5();break;
          case 0xbd:shu6();break;
          case 0x7d:shu7();break;// 扫描第二行    //按下K0-K7时,显示按键标号
          case 0xeb:shu8();break;     //512
          case 0xdb:shu9();break;     //729
          case 0xbb:shu10();break;     //1000
          case 0x7b:shu11();break;//扫描第三行      //1331
          case 0xe7:shu12();break;     //1728
          case 0xd7:shu13();break;     //2197
          case 0xb7:shu14();break;     //2744
          case 0x77:shu15();break; //扫描第四行    //按下K8-K15时,显示按键标号的立方    //3375                 
         }
    /*     for(b=0;b<16;b++)        //第二种写法
         if(P1==hanglie[b])
         num=b;
    */
    }
  }
}
void main()
{
   num=16;
   while(1)
   {
   key();
   P0=tab[num];
   }
}

proteus仿真界面:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值