int seg7[] = { 2, 3, 4, 5, 6, 7, 8};
int table[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
int i, j;
int val=0;
int KeyData;
int num = 0;
int delaytime = 500;
int sw = 9;
void setup()
{
pinMode(sw, INPUT);
for (i = 0; i < 7; i++)
{
pinMode(seg7[i], OUTPUT);
}
}
void loop()
{
KeyData = digitalRead(sw);
if (KeyData == LOW)
{
delay(20);
while(digitalRead(sw)==LOW);
num++;
}
if (num % 2 == 0)
{
val++;
if (val > 9)
val = 0;
}
else
{
val--;
if (val < 0)
val = 9;
}
if(val>9)val=0;
out(~table[val]);
delay(delaytime);//我的板子是共陰極+npn電晶體
}
void out(byte data)
{
for (j = 0; j < 7; j++)
{
if (data % 2 == 1)
digitalWrite(seg7[j], HIGH);//共陰極,1亮
else
digitalWrite(seg7[j], LOW);
data = data / 2;
}
}
沒有留言:
張貼留言