//用一顆7seg接Vcc
byte seg7_table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
byte seg7pin[] = {2, 3, 4, 5, 6, 7, 8};
byte sw = 10;
byte count = 0;
byte number;
byte val=0;
byte add=0;
unsigned long time = 0;
void setup()
{
int i;
for (i = 0; i < 7; i++)
{
pinMode(seg7pin[i], OUTPUT);
}
pinMode(sw, INPUT);
}
void loop()
{
byte i;
val = digitalRead(sw);
if (val == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for (i = 0; i < 8; i++)
{
if (bitRead(~seg7_table[number % 10], i))
digitalWrite(seg7pin[i], HIGH);
else
digitalWrite(seg7pin[i], LOW);
}
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 9)count = 0;
}
else
{
count--;
if (count < 0)count = 9;
}
}
}
=====================================
//用七段共陰極來寫
byte seg7_table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
byte seg7pin[] = {2, 3, 4, 5, 6, 7, 8};
byte sw = 10;
int count = 0;//不可以設為byte, 否則計數有問題
int number;
byte val=0;
byte add=0;
byte com[1]={0};
unsigned long time = 0;
void setup()
{
int i;
for (i = 0; i < 7; i++)
{
pinMode(seg7pin[i], OUTPUT);
}
pinMode(sw, INPUT);
pinMode(com[0],OUTPUT);
}
void loop()
{
byte i,com_scan;
val = digitalRead(sw);
if (val == LOW)
{
delay(20);
while (digitalRead(sw) == LOW);
add++;
}
number = count;
for(com_scan=0;com_scan<1;com_scan++)//掃描的觀念寫
{
for (i = 0; i < 8; i++)
{
if (bitRead(~seg7_table[number % 10], i))
digitalWrite(seg7pin[i], HIGH);
else
digitalWrite(seg7pin[i], LOW);
}
}
digitalWrite(com[0],HIGH);delay(5);
digitalWrite(com[0],LOW);
if (millis() - time >= 1000)
{
time = millis();
if (add % 2 == 0)
{
count++;
if (count > 9)count = 0;
}
else
{
count--;
if (count < 0)count = 9;
}
}
}
沒有留言:
張貼留言