Arduino入门到精通第32课:四位数码管实验(组图)

点击505  回复0  分享1


    Arduino 入门到精通 例程17
    这次我们进行的实验是使用arduino驱动一块共阴四位数码管。驱动数码管限流电阻肯定是必不可少的,限流电阻有两种接法,一种是在d1-d4阳极接,总共接4颗。这种接法好处是需求电阻比较少,但是会产生每一位上显示不同数字亮度会不一样,1最亮,8最暗。另外一种接法就是在其他8个引脚上接,这种接法亮度显示均匀,但是用电阻较多。本次实验使用8颗220Ω电阻(因为没有100Ω电阻,所以使用220Ω的代替,100欧姆亮度会比较高)。
    IMGP0048_调整大小.JPG
    4位数码管总共有12个引脚,小数点朝下正放在面前时,左下角为1,其他管脚顺序为逆时针旋转。左上角为最大的12号管脚。
    IMGP0049_调整大小.JPG
    下图为数码管的说明手册
    5643b.jpg

    下面是硬件连接图
    Snap1.jpg
    下面是程序;

    //显示1234
    //设置阴极接口
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;
    int g = 7;
    int dp = 8;
    //设置阳极接口
    int d4 = 9;
    int d3 = 10;
    int d2 = 11;
    int d1 = 12;
    //设置变量
    long n = 1230;
    int x = 100;
    int del = 55; //此处数值对时钟进行微调

    void setup()
    {
    pinMode(d1, OUTPUT);
    pinMode(d2, OUTPUT);
    pinMode(d3, OUTPUT);
    pinMode(d4, OUTPUT);
    pinMode(a, OUTPUT);
    pinMode(b, OUTPUT);
    pinMode(c, OUTPUT);
    pinMode(d, OUTPUT);
    pinMode(e, OUTPUT);
    pinMode(f, OUTPUT);
    pinMode(g, OUTPUT);
    pinMode(dp, OUTPUT);
    }
    /////////////////////////////////////////////////////////////
    void loop()
    {
    Display(1, 1);
    Display(2, 2);
    Display(3, 3);
    Display(4, 4);

    }
    ///////////////////////////////////////////////////////////////
    void WeiXuan(unsigned char n)//
    {
    switch(n)
    {
    case 1:
    digitalWrite(d1,LOW);
    digitalWrite(d2, HIGH);
    digitalWrite(d3, HIGH);
    digitalWrite(d4, HIGH);
    break;
    case 2:
    digitalWrite(d1, HIGH);
    digitalWrite(d2, LOW);
    digitalWrite(d3, HIGH);
    digitalWrite(d4, HIGH);
    break;
    case 3:
    digitalWrite(d1,HIGH);
    digitalWrite(d2, HIGH);
    digitalWrite(d3, LOW);
    digitalWrite(d4, HIGH);
    break;
    case 4:
    digitalWrite(d1, HIGH);
    digitalWrite(d2, HIGH);
    digitalWrite(d3, HIGH);
    digitalWrite(d4, LOW);
    break;
    default :
    digitalWrite(d1, HIGH);
    digitalWrite(d2, HIGH);
    digitalWrite(d3, HIGH);
    digitalWrite(d4, HIGH);
    break;
    }
    }
    void Num_0()
    {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, LOW);
    digitalWrite(dp,LOW);
    }
    void Num_1()
    {
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    digitalWrite(dp,LOW);
    }
    void Num_2()
    {
    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 Num_3()
    {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, HIGH);
    digitalWrite(dp,LOW);
    }
    void Num_4()
    {
    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 Num_5()
    {
    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 Num_6()
    {
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
    digitalWrite(dp,LOW);
    }
    void Num_7()
    {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    digitalWrite(dp,LOW);
    }
    void Num_8()
    {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
    digitalWrite(dp,LOW);
    }
    void Num_9()
    {
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
    digitalWrite(f, HIGH);
    digitalWrite(g, HIGH);
    digitalWrite(dp,LOW);
    }
    void Clear() //清屏
    {
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    digitalWrite(f, LOW);
    digitalWrite(g, LOW);
    digitalWrite(dp,LOW);
    }
    void pickNumber(unsigned char n)//选数
    {
    switch(n)
    {
    case 0:Num_0();
    break;
    case 1:Num_1();
    break;
    case 2:Num_2();
    break;
    case 3:Num_3();
    break;
    case 4:Num_4();
    break;
    case 5:Num_5();
    break;
    case 6:Num_6();
    break;
    case 7:Num_7();
    break;
    case 8:Num_8();
    break;
    case 9:Num_9();
    break;
    default:Clear();
    break;
    }
    }
    void Display(unsigned char x, unsigned char Number)//显示x为坐标,Number为数字
    {
    WeiXuan(x);
    pickNumber(Number);
    delay(1);
    Clear() ; //消影
    }

     

    把下面代码复制下载到控制板中,看看效果图。

    实验结果为数码管显示1234 ,
    注意:连接的时候需要耐心,认真,不要连错任何线,当出现乱码时,要先检查线路。
    谢谢!



    【相关精彩阅读】
    Arduino入门到精通第31课:数码管显示实验(组图)
    http://tieba.aiwenwen.com/tie/126556.html
    Arduino入门到精通第30课:火焰报警实验(组图)
    http://tieba.aiwenwen.com/tie/126555.html

    Arduino入门到精通第32课:四位数码管实验(组图)


    发表回复

    申请加精
    加大 减小

    * 验证码,看不清楚?请点击刷新验证码 看不清楚?请点击刷新验证码

    每位会员第一次回帖审核成功可获5分[积分规则]
    关闭
    爱问问贴吧郑重承诺:我们将尽一切努力,坚决杜绝低俗信息,为广大网友创造一个绿色、健康、和谐的网络空间。同时,我们也真诚的希望广大网友对我们的工作进行监督。请点击:关于爱问问整治低俗内容的公告