【外设】1位数码管-Arduino中文社区 - Powered by Discuz! Archiver

jiayouxiaoniu 发表于 2016-8-22 21:21

【外设】1位数码管

一、共有两种8段显示器:共阳极、共阴极。
1、共阳极:将a,b,c,d,e,f,g,h设置为高电平,数码管段就会亮。http://www.arduino.cn/forum.php?mod=image&aid=22009&size=300x300&key=e7617bf44067c998&nocache=yes&type=fixnone
2、共阴极:将a,b,c,d,e,f,g,h设置为低电平,数码管段就会亮。
二、电路连接:http://www.arduino.cn/forum.php?mod=image&aid=22010&size=300x300&key=e36fc026fc1bf7e5&nocache=yes&type=fixnone

中间的连接是GND或5V,引脚从低到高:c=4,h=5,b=6,a=7,f=8,g=9,e=10,d=11.



jiayouxiaoniu 发表于 2016-8-23 20:11

附上代码:
#define NUM_DIGITAL 8 //码管的总段数
#define PINA 5
#define PINB 4
#define PINC 2
#define PIND 9
#define PINE 8
#define PINF 6
#define PING 7
#define PINH 3
int pinMap = {PINA,PINB,PINC,PIND,PINE,PINF,PING,PINH};//每个码管对应的引脚号

void digitalZero();
void digitalOne();
void digitalTwo();
void digitalThree();
void digitalFour();
void digitalFive();
void digitalSix();
void digitalSeven();
void digitalEight();
void digitalNine();
void digitalDot();
void digitalDisplayNum(int num);
void digitalReset();

void setup() {
// put your setup code here, to run once
for(int i = 0; i< NUM_DIGITAL; i++)
{
    pinMode(pinMap, OUTPUT);
}
digitalReset();
}

void loop() {
// put your main code here, to run repeatedly:
while(1)
{
    for(int i = 0; i <= 10; i++)
    {
      digitalReset();
      digitalDisplayNum(i);
      delay(1000);
    }
}
}

void digitalZero()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);
digitalWrite(PIND, HIGH);
digitalWrite(PINE, HIGH);
digitalWrite(PINF, HIGH);

return;
}
void digitalOne()
{
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);

return;
}
void digitalTwo()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINB, HIGH);
digitalWrite(PING, HIGH);
digitalWrite(PINE, HIGH);
digitalWrite(PIND, HIGH);

return;
}
void digitalThree()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);
digitalWrite(PIND, HIGH);
digitalWrite(PING, HIGH);

return;
}
void digitalFour()
{
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);
digitalWrite(PINF, HIGH);
digitalWrite(PING, HIGH);

return;
}
void digitalFive()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINC, HIGH);
digitalWrite(PIND, HIGH);
digitalWrite(PINF, HIGH);
digitalWrite(PING, HIGH);

return;
}
void digitalSix()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINC, HIGH);
digitalWrite(PIND, HIGH);
digitalWrite(PINE, HIGH);
digitalWrite(PINF, HIGH);
digitalWrite(PING, HIGH);

return;
}
void digitalSeven()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);

return;
}
void digitalEight()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);
digitalWrite(PIND, HIGH);
digitalWrite(PINE, HIGH);
digitalWrite(PINF, HIGH);
digitalWrite(PING, HIGH);

return;
}
void digitalNine()
{
digitalWrite(PINA, HIGH);
digitalWrite(PINB, HIGH);
digitalWrite(PINC, HIGH);
digitalWrite(PIND, HIGH);
digitalWrite(PINF, HIGH);
digitalWrite(PING, HIGH);
}
void digitalDot()
{
digitalWrite(PINH, HIGH);

return;
}
void digitalDisplayNum(int num)
{
switch(num)
{
    case 0:
    {
      digitalZero();
      break;
    }
    case 1:
    {
      digitalOne();
      break;
    }
    case 2:
    {
      digitalTwo();
      break;
    }
    case 3:
    {
      digitalThree();
      break;
    }
    case 4:
    {
      digitalFour();
      break;
    }
    case 5:
    {
      digitalFive();
      break;
    }
    case 6:
    {
      digitalSix();
      break;
    }
    case 7:
    {
      digitalSeven();
      break;
    }
    case 8:
    {
      digitalEight();
      break;
    }
    case 9:
    {
      digitalNine();
      break;
    }
    case 10:
    {
      digitalDot();
      break;
    }   
    default:
      break;
}
return;
}
void digitalReset()
{
for(int i = 0; i < NUM_DIGITAL; i++)
{
    digitalWrite(pinMap, LOW);
}
return;
}

lehere 发表于 2018-4-2 23:36

我也说一句没有注释啊!差评

lb1810620061 发表于 2019-2-17 10:27

lehere 发表于 2018-4-2 23:36
我也说一句没有注释啊!差评

建议多看看C语言基础,这个其实就是运用了函数   以及case选择语句
页: [1]
查看完整版本: 【外设】1位数码管