单片机按键按行(列)在数码管上显示0~F

这个是没有加小灯的代码

第一个代码是按行操作

#include<reg52.h>
sbit duan=P2^6;
sbit wei=P2^7;
sbit fm=P2^3;
char code  leddata[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}  ;

 void display(int num)     
{
P0=leddata[num];
duan=1;duan=0;
P0=0x00;  //设置其全亮   0000  0000		
wei=1;wei=0;
}
main()
{
while(1)//死循环
{
P3=0xfe;   //开始扫描第一行;
if(P3!=0xfe)
{
if(P3==0xee) display(0);
if(P3==0xde) display(1);
if(P3==0xbe) display(2);
if(P3==0x7e) display(3);
	}
	P3=0xfd;   //扫描第二行
if(P3!=0xfd)
{
if(P3==0xed) display(4);
if(P3==0xdd) display(5);
if(P3==0xbd) display(6);
if(P3==0x7d) display(7);
}
    P3=0xfb;    //扫描第三行;
   if(P3!=0xfb)
{
if(P3==0xeb) display(8);
if(P3==0xdb) display(9);
if(P3==0xbb) display(10);
if(P3==0x7b) display(11);
}
	  P3=0xf7;  //扫描第四行;
   if(P3!=0xf7)
{
if(P3==0xe7) display(12);
if(P3==0xd7) display(13);
if(P3==0xb7) display(14);
if(P3==0x77) display(15);
}
}
}

第二段代码是按列操作(并且添加了流水灯的操作)

#include<reg52.h>
sbit duan=P2^6;
sbit wei=P2^7;
sbit fm=P2^3;
char code  leddata[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}  ;



 void display(int num)  
{
P0=leddata[num];
duan=1;duan=0;
P0=0x00;  //设置其全亮   0000  0000
wei=1;wei=0;
}
delay(int xms)
{
int i,j;
for(i=xms;i>=0;i--)
for(j=112;j>=0;j--);
}
main()
{
while(1)//死循环
{

P3=0xef;   //开始扫描第一lie;	1110 1111
if(P3!=0xef)
{
if(P3==0xee) {display(0); 
 fm=0;
delay(500);
fm=1;
delay(500);	 }

if(P3==0xed) {display(1);
fm=0;
delay(500);
fm=1;
delay(500);	  }

if(P3==0xeb){ display(2);
fm=0;
delay(500);
fm=1;
delay(500);	}

if(P3==0xe7) {display(3);
fm=0;
delay(500);
fm=1;
delay(500);			}

	}
	P3=0xdf;   //扫描第二 lie  1101 1111

if(P3!=0xdf)
{
if(P3==0xde)
{ display(4);
fm=0;
delay(500);
fm=1;
delay(500);	 }
if(P3==0xdd)
{ display(5);
fm=0;
delay(500);
fm=1;
delay(500);	 }
if(P3==0xdb) 
{display(6);
fm=0;
delay(500);
fm=1;
delay(500);	 }
if(P3==0xd7) 
{display(7);
fm=0;
delay(500);
fm=1;
delay(500);	  }
}
P3=0xbf;     //1011 1111

if(P3!=0xbf)
{
if(P3==0xbe) 
{display(8);
fm=0;
delay(500);
fm=1;
delay(500);	}
if(P3==0xbd) 
{display(9);
fm=0;
delay(500);
fm=1;
delay(500);	 }
if(P3==0xbb)
{ display(10);
fm=0;
delay(500);
fm=1;
delay(500);	 }
if(P3==0xb7)
 {display(11);
fm=0;
delay(500);
fm=1;
delay(500);	}
}
	 
	  P3=0x7f; 

   if(P3!=0x7f)
{
if(P3==0x7e)
 {display(12);
fm=0;
delay(500);
fm=1;
delay(500);	}
if(P3==0x7d) 
{display(13);
fm=0;
delay(500);
fm=1;
delay(500);}	
if(P3==0x7b)
{ display(14);
fm=0;
delay(500);
fm=1;
delay(500);	}
if(P3==0x77) 
{display(15);
fm=0;
delay(500);
fm=1;
delay(500);	}
}
}
}

}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值