unsigned long time1=0;
unsigned long time2=0;
int seg7_pin[]={2,3,4,5,6,7,8};
char seg7_table[]={~0xfe,~0xfd,~0xfb,~0xf7,~0xef,~0xdf,~0xff};
int scan_line[]={9,10,11,12};
int bcd[]={6,6,6,6};
int one_times=0;
int cnt=-1;
int i;
int j;
void setup()
{
int a,b;
for(a=0;a<7;a++)
{
pinMode(seg7_pin[a],OUTPUT);
}
for(b=0;b<4;b++)
{
pinMode(scan_line[b],OUTPUT);
}
}
//------------------------------
void outport(byte data)
{
int x;
for(x=0;x<7;x++)
{
if(data%2==1)
digitalWrite(seg7_pin[x],HIGH);
else
digitalWrite(seg7_pin[x],LOW);
data=data/2;
}
}
//-------------------------------
void initial()
{
if(millis()-time1>5)
{
time1=millis();
outport(seg7_table[bcd[j]]);
digitalWrite(scan_line[j],HIGH);delay(1);
digitalWrite(scan_line[j],LOW);
if(j==4)
j=0;
else
j++;
}
if(millis()-time2>500)
{
time2=millis();
if(cnt==12)
cnt=0;
else
cnt++;
if(cnt==0){bcd[3]=0,bcd[2]=6,bcd[1]=6,bcd[0]=6;}
if(cnt==1){bcd[3]=6,bcd[2]=0,bcd[1]=6,bcd[0]=6;}
if(cnt==2){bcd[3]=6,bcd[2]=6,bcd[1]=0,bcd[0]=6;}
if(cnt==3){bcd[3]=6,bcd[2]=6,bcd[1]=6,bcd[0]=0;}
if(cnt==4){bcd[3]=6,bcd[2]=6,bcd[1]=6,bcd[0]=1;}
if(cnt==5){bcd[3]=6,bcd[2]=6,bcd[1]=6,bcd[0]=2;}
if(cnt==6){bcd[3]=6,bcd[2]=6,bcd[1]=6,bcd[0]=3;}
if(cnt==7){bcd[3]=6,bcd[2]=6,bcd[1]=3,bcd[0]=6;}
if(cnt==8){bcd[3]=6,bcd[2]=3,bcd[1]=6,bcd[0]=6;}
if(cnt==9){bcd[3]=3,bcd[2]=6,bcd[1]=6,bcd[0]=6;}
if(cnt==10){bcd[3]=4,bcd[2]=6,bcd[1]=6,bcd[0]=6;}
if(cnt==11){bcd[3]=5,bcd[2]=6,bcd[1]=6,bcd[0]=6;}
}
}
void loop()
{
if(one_times==0)
{
initial();
if(time2>=20500)//500*12=6000*3再去調整數據,看您要停在那
{
for(i=0;i<4;i++)
{
digitalWrite(scan_line[i],LOW);
}
one_times=1;
}
}
}
沒有留言:
張貼留言