unsigned long time1 = 0;
unsigned long time2 = 0;
int btn_pin=13;
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 }; //一開機,先清除畫面
int i, j = 0, cnt;
int read_status;//讀按鈕是高電位還是低電位
int odd_even=0;//每切換一次奇變偶,偶變奇
//-----------------------------------------------------------------------------
void setup()
{
pinMode(btn_pin,INPUT);
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()
{
read_status=digitalRead(btn_pin);
if(read_status==LOW)
{
delay(50);
while(digitalRead(btn_pin)==LOW);
odd_even++;
}
if (millis() - time1 > 5) //超過0.05秒掃瞄scan_line及秀字
{
outport(seg7_table[bcd[j]]);
digitalWrite(scan_line[j], HIGH);//exe j=0
delay(1);
digitalWrite(scan_line[j], LOW);
if (j == 1)//scan_line[0]第9支腳 scan_line[1]第10支腳
j = 0;//掃瞄是從右到左
else
j++;//會先執行j=0(scan_line com0) j=1(0+1=1再執行com1)又在exe scan_line=0
}
if (millis() - time2 > 1000) //超過1秒,計數值加1
{
time2 = millis();
if(odd_even%2==0)
{
if (cnt== 7)
cnt=0;
else
cnt++;
}
else
{
if (cnt==0)
cnt=7;
else
cnt--;
}
}//兩個七段幾乎同時顯示,因為速度很快, 使用一維陣列,總共要8次變化
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 ; }
}
沒有留言:
張貼留言