数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

搜索
查看: 342|回复: 5

[other] TM1629显示程序

[复制链接]
发表于 2025-5-23 12:56:09 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x
本帖最后由 amsl 于 2025-5-23 13:05 编辑

使用的STM32单片机,
  1. //头文件内容
  2. #ifndef __TM1629D_H__

  3. #include "stm32f1xx_hal_gpio.h"
  4. #include "gpio.h"

  5. __inline void delayus(uint32_t us)
  6. {
  7.         uint32_t i = 10;
  8.         while(us--) {
  9.                 i=10;
  10.                 while(i--);
  11.         }
  12. }

  13. __inline void tm1629_dio_0()  { HAL_GPIO_WritePin(GPIOB, PIN11, 0); delayus(1); }
  14. __inline void tm1629_dio_1()  { HAL_GPIO_WritePin(GPIOB, PIN11, 1); delayus(1); }
  15. __inline void tm1629_clk_0()  { HAL_GPIO_WritePin(GPIOB, PIN12, 0); delayus(1); }
  16. __inline void tm1629_clk_1()  { HAL_GPIO_WritePin(GPIOB, PIN12, 1); delayus(1); }
  17. __inline void tm1629_stb_0()  { HAL_GPIO_WritePin(GPIOB, PIN13, 0); delayus(1); }
  18. __inline void tm1629_stb_1()  { HAL_GPIO_WritePin(GPIOB, PIN13, 1); delayus(1); }

  19. #endif

  20. //C源代码内容
  21. #include "tm1629.h"
  22. #include "stm32f1xx_hal.h"

  23. const uint8_t DT[] = { //显示的数字编码
  24. //[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [A], [-]
  25.   0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x40
  26. };

  27. //写入一个字节
  28. void tm1629_write_byte(uint8_t dat)
  29. {
  30.         int i;
  31.         for (i=0; i<8; i++)
  32.         {
  33.                 tm1629_clk_0();
  34.                 if (dat & 0x01) {
  35.                         tm1629_dio_1();
  36.                 } else {
  37.                         tm1629_dio_0();
  38.                 }
  39.                 tm1629_clk_1();
  40.                 dat >>= 1;
  41.         }
  42. }

  43. //设定LED数码管的显示亮度
  44. //libht: 0~7 //亮度取值范围
  45. void tm1629_light(uint8_t level)
  46. {
  47.         if (level > 7)
  48.                 level = 7;
  49.         tm1629_stb_0();
  50.         tm1629_write_byte(0x88 + level);
  51.         tm1629_stb_1();
  52. }

  53. //在指定的数码管上写入数据,8个数码管映射为映射为0~7
  54. //addr:0~7
  55. void tm1629_display(uint8_t addr, uint8_t dat, uint8_t dot)
  56. {        
  57.         tm1629_stb_0();
  58.         tm1629_write_byte(0x44);
  59.         tm1629_stb_1();
  60.         tm1629_stb_0();
  61.         tm1629_write_byte(0xC0 | (addr*2));
  62.         if (dot) {
  63.                 tm1629_write_byte(DT[dat] | 0x80); //display decimal point
  64.         } else {
  65.                 tm1629_write_byte(DT[dat]);
  66.         }
  67.         tm1629_stb_1();
  68. }

  69. //连续写8个数码管
  70. void tm1629_write_8byte(uint8_t *dats)
  71. {
  72.         for (int i=0; i<8; i++) {
  73.                 tm1629_display(i, *dats++, 0);
  74.         }
  75. }


复制代码

 楼主| 发表于 2025-5-23 13:07:38 | 显示全部楼层
  1. //应用
  2. uint8_t ledbuf[8]={0,0,0,0,0,0,0,1};
  3. voie main(void)
  4. {
  5. //init code
  6. //...

  7. tm1629_light(0); //Set LEDs lightness
  8. tm1629_write_8byte(ledbuf); //default display value: 00000001
  9. //前两个数码管改为1.2
  10. tm1629_display(0, 1, 1);
  11. tm1629_display(1, 2, 0);
  12. //end init.
  13. while(1){;}

  14. }
复制代码

打赏

参与人数 1家元 +16 收起 理由
261307853 + 16 謝謝分享

查看全部打赏

回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-5-23 15:44:59 | 显示全部楼层
//用法
uint8_t ledbuf[8]={0,0,0,0,0,0,0,1}; //显示缓存
void main(void)
{
    //init...
    tm1629_light(0); //Set LEDs lightness
    tm1629_write_8byte(ledbuf); //default display value: 00000001
    //...
    //修改前两位为1.2
    tm1629_display(0, 1, 1); //第一个数码管显示为1,显示小数点
    tm1629_display(1, 2, 0); //第二个数码管显示为2
    //
    while(1) {
    }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-5-24 09:47:21 | 显示全部楼层
本帖最后由 amsl 于 2025-5-24 10:47 编辑
dcc20250209 发表于 2025-5-23 17:29
正在研究TM1639,正好学习学习。

看数据手册TM1639和TM1629貌似是一样的架构,只是砍掉了SEG5~SEG8引脚
可以用下面的代码试试,没有硬件测试,盲写的,


//在指定的数码管上写入数据,8个数码管映射为映射为0~7
//addr:0~7
void tm1639_display(uint8_t addr, uint8_t dat, uint8_t dot)
{        
    tm1639_stb_0();
    tm1639_write_byte(0x44);
    tm1639_stb_1();
    tm1639_stb_0();
    tm1639_write_byte(0xC0 | (addr*2));
        //low 4bit at the first byte
        tm1639_write_byte(DT[dat]);
        //high 4bit at the second byte
    if (dot) { //display decimal point
        tm1639_write_byte((DT[dat] | 0x80)>>4);
    } else {
        tm1639_write_byte(DT[dat]>>4);
    }
    tm1639_stb_1();
}

主要是修改上面两个函数,其他函数可以只改个名字

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2025-7-23 10:25 , Processed in 0.452401 second(s), 16 queries , Redis On.

Powered by Discuz!

© 2006-2025 MyDigit.Net

快速回复 返回顶部 返回列表