#include <AT89X51.H>
void delay(int);
void setNo(char,char);
void setP0Number(char,char);
void displayNumber(int,char);
void main()
{
int jsq=0;
char i,j;
while(1)
{
displayNumber(jsq,4);
if(P3_2==0)
{
jsq+=1;
while(P3_2==0)displayNumber(jsq,4);
}
if(P3_4==0)
{
jsq-=1;
while(P3_4==0)displayNumber(jsq,4);
}
if(P3_5==0)
{
jsq*=2;
while(P3_5==0)displayNumber(jsq,4);
}
if(P3_6==0)
{
jsq/=2;
while(P3_6==0)displayNumber(jsq,4);
}
if(P3_7==0)
{
jsq=0;
while(P3_7==0)displayNumber(jsq,4);
}
if(P3_5==0)
{
jsq=9999;
while(P3_5==0)displayNumber(jsq,4);
}
//for(i=1;i<=1;i++){
// setNo(i);setP0Number(i,0);delay(100);}
}
return;
}
void displayNumber(int Number,char point)
{
int i;
/*for(i=1;i<=4;i++)
{
setNo(i);
setP0Number(11,0);
delay(5);
}*/
//return;
/*Number%=10000;
if(full!=0) //display all zero
{
for(i=1;i<=4;i++)
{
setNo(i);
setP0Number(0,0);
delay(2);
}
}*/
if(Number<0) //ABS(Number)
Number=-Number;
Number%=10000;
//thousand
setP0Number(11,0);
setP0Number((char)(Number/1000),(point==1?1:0));
setNo(1,1);
delay(1);
setNo(1,0);
Number%=1000;
//hundred
setP0Number(11,0);
setP0Number((char)(Number/100),(point==1?1:0));
setNo(2,1);
delay(1);
setNo(2,0);
Number%=100;
//ten
setP0Number(11,0);
setP0Number((char)(Number/10),(point==1?1:0));
setNo(3,1);
delay(1);
setNo(3,0);
Number%=10;
//one
setP0Number(11,0);
setP0Number((char)(Number),(point==1?1:0));
setNo(4,1);
delay(1);
setNo(4,0);
}
void setP0Number(char Number, char pointHere)
{
char numt[12]={0xa0, 0xbb, 0x62, 0x2a, 0x39, 0x2c, 0x24, 0xba, 0x20, 0x28, 0xdf, 0xff};
// 0 1 2 3 4 5 6 7 8 9 . off
P0=numt[Number];
if(pointHere != 0)
{
P0 &= numt[10];
}
}
void setNo(char No, char set)
{
P2_4=P2_5=P2_6=P2_7=0;
switch(No)
{
case 1:
P2_4=set;
break;
case 2:
P2_5=set;
break;
case 3:
P2_6=set;
break;
case 4:
P2_7=set;
break;
default:
P2_4=P2_5=P2_6=P2_7=set;
}
}
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<500;j++);
}
}
缩进消失了, 代码丑陋, 请谅解。
发表评论