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};
// 亮a 亮b 亮c 亮d 亮e 亮f
int scan_line[] = {9, 10};
char bcd[] = {6, 6, 6, 6}; //一開機,先清除畫面
int i, j = 0, cnt;
//-----------------------------------------------------------------------------
void setup()
{
for (i = 0; i < 8; i++)
pinMode(seg7_pin[i], OUTPUT);//資料端
for (i = 0; i < 4; i++)
pinMode(scan_line[i], OUTPUT);//掃瞄端
}
//-------------------------------------------------------------------------------
void outport(byte data)
{
for (int j = 0; j < 7; j++) {
if (data % 2 == 1) // 取出 data 的最低位元
digitalWrite(seg7_pin[j], LOW);
else
digitalWrite(seg7_pin[j], HIGH);
data = data / 2; // 除2,進行下一位元的處理
}
}
//-----------------------------------------------------------------------------
void loop()
{
if (millis() - time1 > 5) //超過0.05秒掃瞄scan_line及秀字
{
outport(seg7_table[bcd[j]]);
digitalWrite(scan_line[j], HIGH);
delay(1);
digitalWrite(scan_line[j], LOW);
if (j == 2)//scan_line[0]第9支腳 scan_line[1]第10支腳
j = 0;//掃瞄是從右到左
else
j++;
}
if (millis() - time2 > 1000) //超過1秒,計數值加1
{
time2 = millis();
cnt++;
if (cnt > 7)
cnt = 0;
}//兩個七段幾乎同時顯示,因為速度很快
if (cnt == 0) { bcd[1] = 0; bcd[0] = 6 ; }//a 亮(十位數) a不亮(個位數) 因為點亮是從右2到右1所以index=1先寫,index=0後寫
if (cnt == 1) { bcd[1] = 6; bcd[0] = 0 ; }
if (cnt == 2) { bcd[1] = 6; bcd[0] = 1 ; }
if (cnt == 3) { bcd[1] = 6; bcd[0] = 2 ; }
if (cnt == 4) { bcd[1] = 6; bcd[0] = 3 ; }
if (cnt == 5) { bcd[1] = 3; bcd[0] = 6 ; }
if (cnt == 6) { bcd[1] = 4; bcd[0] = 6 ; }
if (cnt == 7) { bcd[1] = 5; bcd[0] = 6 ; }
}
沒有留言:
張貼留言