ArduinoC语言&嵌入式每天一点单片机知识

(8)趣味单片机新玩法-多个数码管扫描显示

2021-03-20  本文已影响0人  想啥做啥

我们先来看第一个效果,数码管以秒计数显示

动态效果图



老套路,我们接下来进行分解,我们首先第一步就是让每个数码管都可控,不知道怎么使用74HC595的可以参考之前写的文章

代码:

/* Main.ino file generated by New Project wizard
 *
 * Created:   周六 3月 20 2021
 * Processor: Arduino Uno
 * Compiler:  Arduino AVR (Proteus)
 */

// Peripheral Configuration Code (do not edit)
//---CONFIG_BEGIN---
#pragma GCC push_options
#pragma GCC optimize ("Os")

#include <core.h> // Required by cpu
#include <cpu.h>

#pragma GCC pop_options

// Peripheral Constructors
CPU &cpu = Cpu;

void peripheral_setup () {
}

void peripheral_loop() {
}
//---CONFIG_END---

int STCP_Pin = 6;
int SHCP_Pin = 4;
int DS_Pin = 5; 

 char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void setup () {
peripheral_setup();
// TODO: put your setup code here, to run once:
    pinMode(STCP_Pin,OUTPUT);
    pinMode(SHCP_Pin,OUTPUT);
    pinMode(DS_Pin,OUTPUT);

}

void loop() {
peripheral_loop();


// TODO: put your main code here, to run repeatedly:

    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xDF);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[0]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(500);


    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xEF);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[1]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(500);

    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xF7);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[2]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(500);

    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFB);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[3]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(500);

    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFD);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[4]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(500);

    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFE);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[5]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(500);

}

我们接下来把速度提一提,延时改成50ms


感觉还是有点慢,我们再提点速度,改成10ms


但是,感觉显示有点乱,我们尝试在每个数码管显示过后给它关闭,在电视机邻域有个专业的名词叫消隐
    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xDF);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[0]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(10);
    // 消隐
    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
    digitalWrite(STCP_Pin,HIGH); 
    delay(1);

我们再来看下效果,是不是没有乱码了,之前因为没有关闭,由于人眼的余晖效应,会造成乱码

然后我们稍微再提点速度≥50Hz的刷新率,6个数码管,每个耗时3ms,这样整个下来的总时间是18ms,基本上就不会看到闪烁。

    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xDF);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[0]);
    digitalWrite(STCP_Pin,HIGH); 
    delay(2);
    // 消隐
    digitalWrite(STCP_Pin,LOW); 
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
    shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
    digitalWrite(STCP_Pin,HIGH); 
    delay(1);

18ms刷新的效果,基本看不到闪烁的变化

文章开头效果总代码:

/* Main.ino file generated by New Project wizard
 *
 * Created:   周六 3月 20 2021
 * Processor: Arduino Uno
 * Compiler:  Arduino AVR (Proteus)
 */

// Peripheral Configuration Code (do not edit)
//---CONFIG_BEGIN---
#pragma GCC push_options
#pragma GCC optimize ("Os")

#include <core.h> // Required by cpu
#include <cpu.h>

#pragma GCC pop_options

// Peripheral Constructors
CPU &cpu = Cpu;

void peripheral_setup () {
}

void peripheral_loop() {
}
//---CONFIG_END---

int STCP_Pin = 6;
int SHCP_Pin = 4;
int DS_Pin = 5; 

 char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void setup () {
peripheral_setup();
// TODO: put your setup code here, to run once:
    pinMode(STCP_Pin,OUTPUT);
    pinMode(SHCP_Pin,OUTPUT);
    pinMode(DS_Pin,OUTPUT);

}


int count = 0;
int sec = 0;
void loop() {
peripheral_loop();

    count++;
    if(count>50)
    {
        count=0;
        sec++;
        if(sec>10)sec=0;
    }
    // TODO: put your main code here, to run repeatedly:

        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xDF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[sec]);
        digitalWrite(STCP_Pin,HIGH); 
        delay(2);
        // 消隐
        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
        digitalWrite(STCP_Pin,HIGH); 
        delay(1);


        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xEF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[sec]);
        digitalWrite(STCP_Pin,HIGH); 
        delay(2);
        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
        digitalWrite(STCP_Pin,HIGH); 
        delay(1);

        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xF7);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[sec]);
        digitalWrite(STCP_Pin,HIGH); 
        delay(2);
        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
        digitalWrite(STCP_Pin,HIGH); 
        delay(1);

        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFB);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[sec]);
        digitalWrite(STCP_Pin,HIGH); 
        delay(2);
        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
        digitalWrite(STCP_Pin,HIGH); 
        delay(1);

        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFD);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[sec]);
        digitalWrite(STCP_Pin,HIGH); 
        delay(2);
        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
        digitalWrite(STCP_Pin,HIGH); 
        delay(1);

        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFE);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, table[sec]);
        digitalWrite(STCP_Pin,HIGH); 
        delay(2);
        digitalWrite(STCP_Pin,LOW); 
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0xFF);
        shiftOut(DS_Pin,SHCP_Pin,MSBFIRST, 0x00);
        digitalWrite(STCP_Pin,HIGH); 
        delay(1);
}

总结

复杂的原理来自多年实验的总结,同样也来自生活实践。

上一篇 下一篇

猜你喜欢

热点阅读