unsigned long time1 = 0;
int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};
int weight[] = {64, 32, 16, 8, 4, 2, 1};
unsigned char seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99,
~0x92, ~0x82, ~0xf8, ~0x80, ~0x90,
~0x86, ~0xAF
};
unsigned char scan_line_pin[] = {9, 10, 11, 12}; //掃描線從com0個位數開始掃描
//3 2 1 0所以每認-1即依續scan個十百千
int dip_sw[] = {13, 14, 15, 16, 17, 18, 19};
int num_val;
int cnt_val = 0;
int value;
unsigned int number;
int i;
int j;
void setup()
{
Serial.begin(9600);
for (int g = 0; g < 5; g++)
{
pinMode(dip_sw[g], INPUT);
}
for (int m = 0; m < 4; m++)
{
pinMode(scan_line_pin[m], OUTPUT);
//digitalWrite(scan_line_pin[m], LOW);
}
for (int s = 0; s < 7; s++)
{
pinMode(seg7_pin[s], OUTPUT);
digitalWrite(seg7_pin[s], LOW);
}
}
//----------------------------------
void outport(byte data)//呼叫個別數字副程式
{
for (int k = 0; k < 7; k++)
{
if (data % 2 == 1)//整數部分連除法,從a開始取到g
digitalWrite(seg7_pin[k], HIGH);
else
digitalWrite(seg7_pin[k], LOW);
data = data / 2;//每除完一次除2
}
}
void loop()
{
//num_val = cnt_val;//因為每認數字不同,所以要有兩個變數一個為原有的,一個為新加1的數值
number = 0;
for (int ii = 0; ii < 7; ii++)//7個dip,如果數字用錯會出現亂碼
{
value = digitalRead(dip_sw[ii]);
if (value == HIGH)
{
number = number + weight[ii];
if (number > 120)
{
for (int s = 0; s < 10; s++)//此數字要去測,外圈掃描個10次
{
for (int q = 0; q < 50; q++)//內圈掃瞄個50次
{
digitalWrite(scan_line_pin[2], HIGH); outport(seg7_table[10]);
//Serial.print("seg7_table[10]="); Serial.println(seg7_table[10]);善用Serial除錯
delay(1); digitalWrite(scan_line_pin[2], LOW);
digitalWrite(scan_line_pin[1], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[1], LOW);
digitalWrite(scan_line_pin[0], HIGH); outport(seg7_table[11]); delay(1); digitalWrite(scan_line_pin[0], LOW);
}
}
}
}
}
//Serial.print("number="); Serial.println(number); delay(1000);
for ( i = 0; i < 4; i++) //scan從index=0 9 10 11 12
{
for ( j = 0; j < 7; j++)//此loop只是在跑7段顯示器的腳位
{
outport(seg7_table[number % 10]);//呼叫副程式,先取mod 10 餘數,從個位數開始取,
// Serial.println(j);debug結束後要把Serial刪掉,否則會有閃樂現象,因為他會佔用時間
// Serial.println(num_val);
}//即個位數的com0高電位,送字
digitalWrite(scan_line_pin[i], HIGH); delay(1);//亮個位數2ms 七段顯示數字,再十位數,七段顯示數字
digitalWrite(scan_line_pin[i], LOW);//在把個十百千
number = number / 10;//取餘數完再除10
}
}
沒有留言:
張貼留言