原帖由
A670521546
发自:dev.eefocus.com
------------------------------------------------------------------------------------------------------------------------------------------
零基础,从最简单的开始入手,入门,版主也发了一篇数码管显示的教程,用的是共阳极数码管
我用的是共阴极数码管,在程序上也有点不同 如果没有把握,请一定加入限流电阻,我这个没有加限流电阻,所以显得很亮,应该是要加入限流电阻的,养成好习惯
int a=2;
int b=3;
int c=4;
int d=5;
int e=6;
int f=7;
int g=8;
int dp=9;
void digital_0(void) //x显示0
{
unsigned char i;
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
for(i=2;i<8;i++)
{
digitalWrite(i,HIGH);
}
}
void digital_1(void) //显示1
{
unsigned char i;
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
for(i=5;i<10;i++)
{
digitalWrite(i,LOW);
}
}
void digital_2(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void digital_3(void)
{
unsigned char i;
digitalWrite(dp,LOW);
digitalWrite(g,HIGH);
digitalWrite(f,LOW);
digitalWrite(e,LOW);
for(i=2;i<6;i++)
{
digitalWrite(i,HIGH);
}
}
void digital_4(void)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void digital_5(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void digital_6(void)
{
unsigned char i;
digitalWrite(a,LOW);
digitalWrite(b,LOW);
for(i=4;i<9;i++)
{
digitalWrite(i,HIGH);
}
digitalWrite(dp,LOW);
}
void digital_7(void)
{
unsigned char i;
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
for(i=5;i<10;i++)
{
digitalWrite(i,LOW);
}
}
void digital_8(void)
{
unsigned char i;
for(i=2;i<9;i++)
{
digitalWrite(i,HIGH);
}
digitalWrite(dp,LOW);
}
void digital_9(void)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void setup()
{
int i;
for(i=2;i<10;i++)
pinMode(i,OUTPUT);
}
void loop()
{
digital_0();
delay(1000);
digital_1();
delay(1000);
digital_2();
delay(1000);
digital_3();
delay(1000);
digital_4();
delay(1000);
digital_5();
delay(1000);
digital_6();
delay(1000);
digital_7();
delay(1000);
digital_8();
delay(1000);
digital_9();
delay(1000);
}
不知道大家有没有更好的写法,让程序变得更简洁,欢迎讨论
|