社区闲人 发表于 2024-1-11 13:59:12

推荐使用宏定义实现数码管显示数组




在STC提供的例程中有:
u8 code t_display[]={                     //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black-   H    J    K    L    N    o   P    U   t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //位码

很多新手对上面数组中的数字不太理解,使用宏定义实现数码管显示数组,就很好理解了。

   a
---
f| g |b   
---
e|   |c   
---   .h
   d

#define Sega    0x01        //Q0
#define Segb    0x02        //Q1
#define Segc    0x04        //Q2
#define Segd    0x08        //Q3
#define Sege    0x10        //Q4
#define Segf    0x20        //Q5
#define Segg    0x40        //Q6
#define Segh    0x80        //Q7

//共阳LED
#define Num0    ~(Sega + Segb + Segc + Segd + Sege + Segf       )
#define Num1    ~(         Segb + Segc                        )
#define Num2    ~(Sega + Segb + Segd + Sege + Segg            )
#define Num3    ~(Sega + Segb + Segc + Segd               + Segg)
#define Num4    ~(       Segb + Segc               + Segf + Segg)
#define Num5    ~(Sega      + Segc + Segd      + Segf + Segg)
#define Num6    ~(Sega      + Segc + Segd + Sege + Segf + Segg)
#define Num7    ~(Sega + Segb + Segc                            )
#define Num8    ~(Sega + Segb + Segc + Segd + Sege + Segf + Segg)
#define Num9    ~(Sega + Segb + Segc + Segd      + Segf + Segg)
#define Numblk~(0                                             )
#define Numfull ~(Sega + Segb + Segc + Segd + Sege + Segf + Segg + Segh)
#define Num_    ~(                                          Segg) //负号

/*
//共阴LED
#define Num0   (Sega + Segb + Segc + Segd + Sege + Segf       )
#define Num1   (         Segb + Segc                        )
#define Num2   (Sega + Segb + Segd + Sege + Segg            )
#define Num3   (Sega + Segb + Segc + Segd               + Segg)
#define Num4   (       Segb + Segc               + Segf + Segg)
#define Num5   (Sega      + Segc + Segd      + Segf + Segg)
#define Num6   (Sega      + Segc + Segd + Sege + Segf + Segg)
#define Num7   (Sega + Segb + Segc                            )
#define Num8   (Sega + Segb + Segc + Segd + Sege + Segf + Segg)
#define Num9   (Sega + Segb + Segc + Segd      + Segf + Segg)
#define Numblk   (0                                             )
#define Numfull(Sega + Segb + Segc + Segd + Sege + Segf + Segg + Segh)
#define Num_   (                                          Segg) //负号
*/

则数组定义:
//段码
u8 code Seg8_code =
{
        Num0 ,Num1 ,Num2 ,Num3 ,Num4 ,Num5 ,Num6 ,Num7 ,Num8 ,Num9,
        Numfull,Numblk,Num_
};



使用宏定义的好处:
1,方便PCB绘制,不必一定要Q0--A,Q1--B,Q2--C,Q3--D,Q4--D,Q5--E,Q6--F,Q7--H
例如,实际联线是:Q0--A,Q1--B,Q2--C,Q3--D,Q4--G,Q5--H,Q6--E,Q7--F

//Q0--A,Q1--B,Q2--C,Q3--D,Q4--G,Q5--H,Q6--E,Q7--F
#define Sega        0x01        //Q0
#define Segb        0x02        //Q1
#define Segc        0x04        //Q2
#define Segd        0x08        //Q3
#define Sege        0x40        //Q6
#define Segf        0x80        //Q7
#define Segg        0x10        //Q4
#define Segh        0x20        //Q5

同样,对HC595用到的位选码的联线也可以自由连接。

2,可以方便增加定义要显示的内容。

3,可以轻松应对米字形数码管

这个方法无论从代码可读性,扩展性,维护难易程度来说,都很理想。





_奶咖君_ 发表于 2024-1-11 14:12:03

是的,,而且就算是换了硬件接法 也只需要维护最底层的
#define Sega    0x01      //Q0
#define Segb    0x02      //Q1
#define Segc    0x04      //Q2
#define Segd    0x08      //Q3
#define Sege    0x10      //Q4
#define Segf    0x20      //Q5
#define Segg    0x40      //Q6
#define Segh    0x80      //Q7
这些宏定义就可以了。

之前做过一个用到段码屏的项目,就是这么玩得。

神农鼎 发表于 2024-1-11 16:50:26

感谢 硬核 建议 !!!





深圳国芯人工智能有限公司-工具软件 (stcai.com)





小涵子爸爸 发表于 2024-3-30 10:58:24

学习

xxxevery 发表于 2024-3-30 13:06:56

程序写规范了就是方便移植

垂柳工作室 发表于 2024-3-31 08:40:29

学习了,感觉真不错

DebugLab 发表于 2024-4-2 22:19:02

数码管也可以不是相同接法,比如四个数码管后两个倒过来用,就是8.8:8°8,a-a-d-d这么接,就能显示秒点冒号和℃了

DebugLab 发表于 2024-4-2 22:22:28

DebugLab 发表于 2024-4-2 22:19
数码管也可以不是相同接法,比如四个数码管后两个倒过来用,就是8.8:8°8,a-a-d-d这么接,就能显示秒点冒 ...

而且可以使用水银开关简易实现正放时间倒放温度的功能,我做过,还加了振动开关,用外部中断,白天数码管高亮度,夜间低亮度,拍一下触发振动开关变为高亮度10s
页: [1]
查看完整版本: 推荐使用宏定义实现数码管显示数组