1、P0口按下某个键P2口上的数码管显示按键的号
(1)
#include<>
Code char smg[8]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07};
main()
{
int i=8,x;
P2=0x00;
while(1)
{
x=P0;
switch(x)
{
case 0xfe:{i=0;break;}
case 0xfd:{i=1;break;}
case 0xfb:{i=2;break;}
case 0xf7:{i=3;break;}
case 0xef:{i=4;break;}
case 0xdf:{i=5;break;}
case 0xbf:{i=6;break;}
case 0x7f:{i=7;break;}
}
P2=smg[i];
while(P0==0xff);
}
}
(2)种方法
#include<>
Unsigned/char smg[8]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07};
main()
{
unsigned char i;
P2=0x00;
While(1)
{
i=P1;
If {i=0xfe;P2=smg[0];}
Else if {i=0xfd;P2=smg[1];}
Else if {i=0xfb;P2=smg[2];}
Else if {i=0xf7;P2=smg[3];}
Else if {i=0xef;P2=smg[4];}
Else if {i=0xdf;P2=smg[5];}
Else if {i=0xbf;P2=smg[6];}
Else if {i=0x7f;P2=smg[7];}
}
}
2、
#include<>
sbit P07=P0^7;
void main (void)
{
unsigned i;
P1=0x00;
i=0x01;
P07=1;
while(1)
{
if(P07==0)
{
P1=i;
i<<=1;
while(P07==0);
if(i==0x80)
{
P1=i;
i=0x01;}
}
}
}
3、四位BCD码数码管显示(静态)
#include<>
main()
{
int i,j;
char g,s,b,q;
for(i=1;i<=9999;i++)
{
q=i/1000;
b=i%1000/100;
s=i%100/10;
g=i/10;
s<<=4;
q<<=4;
P2=s+g;
P3=b+q;
j=4000;while(j--);
}
}
4、四位一体LED数码管显示(动态)
#include<>
main()
{
int i,j;
codechar smg[10]={0x3f,0x06,0x5b,0x4f,
单片机实验 来自淘豆网m.daumloan.com转载请标明出处.