int seg7_pin[] = {2,3,4,5,6,7,8};
int scan_line[] = { 9,10,11,12};
int buzzer =14;
int seg7_table[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 };
int i,j; // 迴圈用的變數
int times=0;
//--------------------------------------------------------------------------------------
void OutPort(byte data)
{
for (j=0; j<7; j++) {
if (data % 2==1) // 取出 dat 的最低位元
digitalWrite(seg7_pin[j], LOW); // 若為 1 代表該段要亮, 輸出高準位
else
digitalWrite(seg7_pin[j], HIGH); // 若為 0 代表該段要滅, 輸出低準位
data=data/2; // 除2,進行下一位元的處理
}
}
//-------------------------------------------------------------------------------------------
void initialization()
{
// for(int c=0;c<4;c++)
// {
// digitalWrite(scan_line[c],LOW);
// }
for(int a=0;a<3;a++)
{
for(int b=0;b<50;b++)
{
OutPort(seg7_table[2]);//顯示千位數:2
digitalWrite(scan_line[0], HIGH); delay(5);
digitalWrite(scan_line[0], LOW);//com1
OutPort(seg7_table[0]);//顯示百位數:0
digitalWrite(scan_line[1], HIGH); delay(5);
digitalWrite(scan_line[1], LOW); //com2
OutPort(seg7_table[2]);//顯示十位數:2
digitalWrite(scan_line[2], HIGH); delay(5);
digitalWrite(scan_line[2], LOW);//com3
OutPort(seg7_table[1]);//顯示個位數:1
digitalWrite(scan_line[3], HIGH); delay(5);
digitalWrite(scan_line[3], LOW); //com4
}
delay(2000);
}
tone(buzzer,500,500);delay(1000);
tone(buzzer,500,500);delay(1000);
tone(buzzer,500,500);delay(1000);
}
//----------------------------------------------------------------------------------------
void setup()
{
pinMode(buzzer,OUTPUT);
pinMode(13,OUTPUT); //此處沒設定輸出,led會微亮不會全亮
for (i=0; i<7; i++)
{
pinMode(seg7_pin[i], OUTPUT);
}
for (i=0; i<4; i++)
{
pinMode(scan_line[i], OUTPUT);
digitalWrite(scan_line[i], LOW); // 掃描端電晶體 OFF
}
}
//--------------------------------------------------------------------------------------------
void loop()
{
if(times==0)
{
times=1;
initialization();
}
digitalWrite(13,HIGH);delay(1000);
digitalWrite(13,LOW);delay(1000);
}
沒有留言:
張貼留言