您的位置 首页 基础

单片机学习——独立按键之三

题目:按下16个矩阵键盘依次在数码管上显示1-16的平方。如按下第一个显示1,第二个显示4解决方案:(C语言编程)includereg52h>incl

标题:按下16个矩阵键盘顺次在数码管上显现1-16的平方。如按下第一个显现1,第二个显现4…
解决方案:(C言语编程)
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar bai,shi,ge,num,temp;
uint shu;
sbit le=P3^6;
void delay(uint a);
void dis();
void display(uchar ba,uchar sh,uchar g);
uchar code tabledu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0x86,0xc7,0xc7,0xc0,0xff};
uchar code tablewe[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void main()
{
while(1){
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x7e:shu=16 ;break;
case 0xbe:shu=15 ;break;
case 0xde:shu=14 ;break;
case 0xee:shu=13 ;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
shu=shu*shu;
}
}
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
display(bai,shi,ge);
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x7d:shu=12 ;break;
case 0xbd:shu=11 ;break;
case 0xdd:shu=10 ;break;
case 0xed:shu=9 ;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
shu=shu*shu;
}
}
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
display(bai,shi,ge);
P1=0xfb;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x7b:shu=8 ;break;
case 0xbb:shu=7 ;break;
case 0xdb:shu=6 ;break;
case 0xeb:shu=5 ;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
shu=shu*shu;
}
}
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
display(bai,shi,ge);
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0x77:shu=4 ;break;
case 0xb7:shu=3 ;break;
case 0xd7:shu=2 ;break;
case 0xe7:shu=1 ;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
shu=shu*shu;
}
}
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
display(bai,shi,ge);
}
}
void display(uchar ba,uchar sh,uchar g)
{
le=1;
P2=tablewe[1];
le=0;
le=1;
P0=tabledu[ba];
le=0;
delay(5);
le=1;
P2=tablewe[2];
le=0;
le=1;
P0=tabledu[sh];
le=0;
delay(5);
le=1;
P2=tablewe[3];
le=0;
le=1;
P0=tabledu[g];
le=0;
delay(5);
}
void delay(uint a) {
uint i,j;
for(j=a;j>0;j–)
for(i=250;i>0;i–);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/jichu/260989.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部