微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机学习——独立按键之三

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

时间:11-23 来源:互联网 点击:
题目:按下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--);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top