|
我用595數碼管做了顯示程序,總是不正常,哪位老師能幫我指點一上程序好下:
#include<reg52.h>
unsigned int date;
unsigned char n0,n1,n2,n3,n4,n5,n7,s,sec;
unsigned char step1; unsigned int num,t0,start;
unsigned char step2,step3,step0,nn,step4,step5;
unsigned char code table[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code table[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90sbit x1=P0^1;
sbit x2=P0^2;
sbit x3=P0^3;
sbit x4=P0^4;
sbit x5=P0^5;
sbit x6=P0^6;
sbit x7=P0^7;
sbit x0=P0^0;
sbit y0=P1^3;
sbit out1=P1^1;
sbit out2=P1^2;
sbit LOAD=P2^7;
sbit SCLK=P2^6; //時鐘
sbit SDK=P2^5; //數據
void display();
void hc595(unsigned char word);
void main ()
{ t0=123;
date=5;
display();
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
EA=1;
TR0=1;
ET0=1;
step0=1;
while(1)
{P0=0xff;
if(x0==0)
{for(nn=0;nn>1000;nn++);
if(x0==0)
n0=1;
}
else n0=0;
if(x1==0)
{for(nn=0;nn>1000;nn++);
if(x1==0)
n1=1;
else n1=0;}
if(x2==0)
{for(nn=0;nn>1000;nn++);
if(x2==0)
n2=1;
else n2=0;
}
if(x3==0)
{for(nn=0;nn>1000;nn++);
if(x3==0)
n3=1;
}
else
n3=0;
if(x4==0)
{for(nn=0;nn>1000;nn++);
if(x4==0)
n4=1;}
else n4=0;
if(x7==0)
{for(nn=0;nn>1000;nn++);
if(x7==0)
n7=1;
else n7=0;}
if (step0==1)
{ display();
}
if(((step0&n0)|step1)&!n7)//起動時
{step1=1;step0=0;y0=0;sec=0;}
else step1=0;
if (((step1&n1)|step2)&!step5) //下到位
{step2=1;step1=0; //橫進
out1=0;
y0=1;}
if(((step2&&n2)|step3)&&!step4) //橫進到位
{step3=1;
if(date==0)
y0=1;
else
y0=0;
if(date<num)
date++;
else
{y0=1;
out1=1;
step5=1;
}
if(step5&!n5==1)
{out2=1;
if(n5==1)
{out2=0;
step0=1;
}
}
}
if((step3|step4)&n0&step1)
{ step4=1;
if(n5==1)
out1=0;
}
}
}
void timer() interrupt 1
{TH0=0x3c;
TL0=0xb0;
s++;
if(s>1)
{s=0;
if(step1==1)
{sec++;
display();
if(sec>t0)
{out2=0;
sec=0; }
}
if((n3==1)&&(t0<999) )
{ t0++;
}
if((t0>1)&(n4==1))
{t0--;
}
display();
}
}
//*串行輸出一個字節子函數
void hc595(unsigned char out_data)
{unsigned char i,temp;
for(i=0;i<8;i++)
{ SCLK=0; // //先拉低,為后面的上升沿做準備
temp=out_data&0x80;//按位與
if(temp==0x80) //如果數據等于080
SDK=1; //SDK高電平
else SDK=0; //否則低電平
out_data=out_data<<1;//數據左移一位
SCLK=1;//CLK高電平
}
}
//**顯示子函數**//
void display()
{ LOAD=1;
LOAD=0;
hc595(table[date]);
hc595(table[t0/100]);
hc595(table[(t0%10)/10]);
hc595(table[t0%100]);
LOAD=1;
;
LOAD=0;
}
|
|