方法1
int i;
int j;
int count = 0;
int number;
unsigned long time = 0;
int table7seg[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
int seg7[] = { 2, 3, 4, 5, 6, 7, 8};//a~g
const int com[] = {14, 15, 16, 17}; //com0-com3
void setup()
{
for (i = 0; i < 7; i++)
pinMode(seg7[i], OUTPUT);
for (i = 0; i < 4; i++)
{
pinMode(com[i], OUTPUT);
digitalWrite(com[i], LOW);
}
}
void loop()
{
number = count;
for (i = 0; i <= 3; i++)
{
for (j = 0; j < 8; j++)
{
if (bitRead(table7seg[number % 10], j))
digitalWrite(seg7[j], LOW);
else
digitalWrite(seg7[j], HIGH);
}
digitalWrite(com[i], HIGH);
delay(5);
digitalWrite(com[i], LOW);
number = number / 10;
if (millis() - time >= 50)
{
time = millis();
count = count + 1;
if (count > 9999)
count = 0;
}
}
}
方法2
int i;
int j;
int count=0;
int number;
unsigned long time=0;
int num[10]={
B11000000, //0
B11111001, //1
B10100100, //2
B10110000, //3
B10011001, //4
B10010010, //5
B10000010, //6
B11111000, //7
B10000000, //8
B10010000}; //9
const int seg[]={2,3,4,5,6,7,8,9}; //abcdefgp
const int com[]={14,15,16,17}; //com0-com3
void setup()
{
for(i=0;i<8;i++)
pinMode(seg[i],OUTPUT);
for(i=0;i<4;i++)
{
pinMode(com[i],OUTPUT);
digitalWrite(com[i],HIGH);
}
}
void loop()
{
number=count;
for(i=0;i<=3;i++)
{
for(j=0;j<8;j++)
{
if(bitRead(num[number%10],j))
digitalWrite(seg[j],LOW);
else
digitalWrite(seg[j],HIGH);
}
digitalWrite(com[i],HIGH);
delay(5);
digitalWrite(com[i],LOW);
number=number/10;
if(millis()-time>=1000)
{
time=millis();
count=count+1;
if(count>9999)
count=0;
}
}
}
沒有留言:
張貼留言