題目:獨(dú)立按鍵p1.0和p1.1 P1.1鍵為數(shù)字加1鍵 P1.0為數(shù)字減1鍵
本文引用地址:http://m.butianyuan.cn/article/201611/320186.htm 使LED顯示數(shù)據(jù)從000到999之間一次加一或減一
解決方案:(C語(yǔ)言編程)
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar bai,shi,ge,num;
uint shu;
sbit uk= P1^1;
sbit dk= P1^0;
sbit le=P3^6;
void delay(uint a);
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 init()
{
shu=0;
bai=0;
shi=0;
ge=0;
}
void main()
{
init();
while(1){
if(uk==0){
delay(5);
if(uk==0){
if(shu==999)
shu=0;
shu++;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
}
while(!uk) ;
}
if(dk==0){
delay(5);
if(dk==0){
if(shu==0)
shu=999;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
shu--;
}
while(!dk) ;
}
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--);
}
評(píng)論