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

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

【外设】4位数码管

一、也分为共阴极和共阳极4位数码管:本文讲的是共阴极。使用时也需要220欧电阻。http://www.arduino.cn/forum.php?mod=image&aid=22033&size=300x300&key=7b37efea07420189&nocache=yes&type=fixnone
二、4位数码管共有12个引脚,上下各6个,从左下角逆时针1-12。1、12(左起第四位)--9(左起第三位)--8(左起第二位)--6(左起第一位)位为COM口,指示第几个数字,如果该要写该数字,就将其他几个COM口写为HIGH,要写的数字口设置为LOW。2、a--11,b--7,c--4,d--2,e--1,f--10,g--5,h--3http://www.arduino.cn/forum.php?mod=image&aid=22034&size=300x300&key=d02cbf132eddc755&nocache=yes&type=fixnone




jiayouxiaoniu 发表于 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 = {PINA, PINB, PINC, PIND, PINE, PINF, PING, PINH};
int comMap = {COM4, COM3, COM2, COM1};

int digitalNum=
{
{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, OUTPUT);
}

for(int j = 0; j < NUM_COM; j++)
{
    pinMode(comMap, 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, HIGH);
}
digitalWrite(comMap, LOW);

for(int j = 0; j < NUM_PIN; j++)
{
    if(digitalNum)
    {
       digitalWrite(pinMap, HIGH);
    }
    else
    {
       digitalWrite(pinMap, LOW);
    }
}

return;
}
页: [1]
查看完整版本: 【外设】4位数码管