|
楼主 |
发表于 2016-8-23 20:41
|
显示全部楼层
附上代码:
#define PINA 2
#define PINB 3
#define PINC 4
#define PIND 5
#define PINE 6
#define PINF 7
#define PING 8
#define PINH 9
#define NUM_PIN 8
#define COM1 10
#define COM2 11
#define COM3 12
#define COM4 13
#define NUM_COM 4
int pinMap[NUM_PIN] = {PINA, PINB, PINC, PIND, PINE, PINF, PING, PINH};
int comMap[NUM_COM] = {COM4, COM3, COM2, COM1};
int digitalNum[10][8]=
{
{1,1,1,1,1,1,0,0},//0对应的电平
{0,1,1,0,0,0,0,0},//1对应的电平
{1,1,0,1,1,0,1,0},//2对应的电平
{1,1,1,1,0,0,1,0},//3对应的电平
{0,1,1,0,0,1,1,0},//4对应的电平
{1,0,1,1,0,1,1,0},//5对应的电平
{1,0,1,1,1,1,1,0},//6对应的电平
{1,1,1,0,0,0,0,0},//7对应的电平
{1,1,1,1,1,1,1,0},//8对应的电平
{1,1,1,1,0,1,1,0},//9对应的电平
};
int g_count = 0;
void digitalDisp(int numToDisp, int numOfDisp);//numToDisp:显示的数字,numOfDisp:显示的位置
void setup() {
// put your setup code here, to run once:
for(int i = 0; i < NUM_PIN; i++)
{
pinMode(pinMap[i], OUTPUT);
}
for(int j = 0; j < NUM_COM; j++)
{
pinMode(comMap[j], OUTPUT);
}
return;
}
void loop() {
// put your main code here, to run repeatedly:
int num =0;
int numOfTen =0;
int numOfHundred =0;
int numOfThousand =0;
int tmpCount = 0;
while(1)
{
tmpCount++;
num = g_count % 10;
numOfTen = (g_count / 10) %10;
numOfHundred = (g_count /100) %10;
numOfThousand = (g_count/1000)%10;
digitalDisp(num, 1);
delay(5);
digitalDisp(numOfTen, 2);
delay(5);
digitalDisp(numOfHundred, 3);
delay(5);
digitalDisp(numOfThousand, 4);
delay(5);
if(tmpCount > 50)
{
tmpCount = 0;
g_count++;
}
}
return;
}
void digitalDisp(int numToDisp, int numOfDisp)
{
if(((numToDisp > 9) || (numToDisp <0))
||((numOfDisp > 4) || (numOfDisp <1)))
{
return;
}
for(int i = 0; i < NUM_COM; i++)
{
digitalWrite(comMap[i], HIGH);
}
digitalWrite(comMap[numOfDisp-1], LOW);
for(int j = 0; j < NUM_PIN; j++)
{
if(digitalNum[numToDisp][j])
{
digitalWrite(pinMap[j], HIGH);
}
else
{
digitalWrite(pinMap[j], LOW);
}
}
return;
} |
|