新聞中心

EEPW首頁 > 嵌入式系統 > 設計應用 > 單片機學習——獨立按鍵之三

單片機學習——獨立按鍵之三

作者: 時間:2016-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--);
}


關鍵詞: 單片機獨立按

評論


技術專區(qū)

關閉