1.设计:
这个是通过数码管的段选来实现的;
***NOTICE:
要十分注意的是:数码管是有共阴或是共阳之分的,所以正负极不要接反了,不然,arduino的开发板会发烫;
2.代码:(其中用已经封装好的函数,如果要显示相应的数字的话,就直接调用就可以了)
int a = 7;
int b = 6;
int c = 5;
int e= 11;
int d = 10;
int f =8;
int g = 9;
int dp = 4;
void led_code_init(void) //这个是相应的初始化函数;
{
int a = 7;
int b = 6;
int c = 5;
int e= 11;
int d = 10;
int f =8;
int g = 9;
}
void setup() {
led_code_init();
pinMode(a,OUTPUT);
pinMode(c,OUTPUT);
pinMode(b,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(dp,OUTPUT);
}
void num_one(void)
{
led_code_init();
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}
void num_two(void)
{
led_code_init();
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(g,HIGH);
digitalWrite(e,HIGH);
digitalWrite(d,HIGH);
}
void num_three(void)
{
led_code_init();
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(g,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
}
void num_four(void)
{
led_code_init();
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}
void num_five(void)
{
led_code_init();
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
}
void num_six(void)
{
led_code_init();
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(e,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
}
void num_seven(void)
{
led_code_init();
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}
void num_eight(void)
{
led_code_init();
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(e,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
digitalWrite(b,HIGH);
}
void num_nine(void)
{
led_code_init();
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
// digitalWrite(e,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
digitalWrite(b,HIGH);
}
void loop() {
/* digitalWrite(a,HIGH);//按照这个顺序可以顺时针亮
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);*/
//num_one();
//num_two();
//num_three();
// num_four();
//num_five();
//num_six();
//num_seven();
//num_eight();
//led_code_init();
num_nine();
}