[STM32F1] 谁能帮忙看看这个TM1628的程序为什么数码管不亮

[复制链接]
4571|9
 楼主| xiedu 发表于 2018-3-26 14:39 | 显示全部楼层 |阅读模式
#include "TM1628.h"

#define DIO_H HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15, GPIO_PIN_SET)
#define DIO_L HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15, GPIO_PIN_RESET)
#define CLK_H HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14, GPIO_PIN_SET)
#define CLK_L HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14, GPIO_PIN_RESET)
#define STB_H HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_SET)
#define STB_L HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_RESET)

uint8_t const ucTabBrightness[]={
                                                                                        0x80,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f
                                };//显示亮度表
uint8_t const ucTabCode[]=
{
        0x3F, // 0
        0x06, // 1
        0x5B, // 2
        0x4F, // 3
        0x66, // 4
        0x6D, // 5
        0x7D, // 6
        0x07, // 7
        0x7F, // 8
        0x6F, // 9        
        0xFF,//全亮         
        0x00,//全灭         
};

void WriteByte(uint8_t w_data)      //写一字节数据
{

    uint8_t i;
        for(i=0;i<8;i++)
        {
               
        if(w_data&0x01)
                DIO_H;
        else
                DIO_L;
          CLK_L;
       
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
          
                CLK_H;
          __nop();
          __nop();
                w_data=w_data >> 1;        
        }

}               

//发送控制命令
void send_command(uint8_t com)
{
        STB_H;
        __nop();
        __nop();
        __nop();
        __nop();
        __nop();
        STB_L;
        WriteByte(com);
}

//== 数码管显示 flag :1->TM1628芯片1显示   2->TM1628芯片2显示
//== Brighttness :亮度调节 级别 0-8
void TM1628_Display()   //写多字节数据
{
    uint8_t i,data;
       
        send_command(0x03);//设置显示模式,7位10段模式
        send_command(0x40);//设置数据命令,采用地址自动加1模式
//        send_command(0x44);
        send_command(0xc8);//设置显示地址,从00H开始
//        send_command(0xc0|(data));
//        WriteByte(Svalue);
//        send_command(0xc0|(data+1));
//        WriteByte(0);
        for(i=0;i<7;i++)                                        //7个地址
                        {
                                WriteByte(ucTabCode[i]);
                                WriteByte(0);
      }
                          WriteByte(0x8f); //开显示,亮度为10/16
                          STB_H;
}

void DisplayTest(void)
{
        TM1628_Display();
}
 楼主| xiedu 发表于 2018-3-26 14:40 | 显示全部楼层
我只是让四个数码管亮
 楼主| xiedu 发表于 2018-3-26 14:45 | 显示全部楼层
三个IO口用的推挽输出
mmuuss586 发表于 2018-3-26 14:53 | 显示全部楼层
用示波器看下时序;
好像CLK太快了
 楼主| xiedu 发表于 2018-3-26 17:27 | 显示全部楼层
mmuuss586 发表于 2018-3-26 14:53
用示波器看下时序;
好像CLK太快了

中间我都延时了,网上的都没有延时
weiyuliang 发表于 2018-3-29 21:28 | 显示全部楼层

  1. /****************************************Copyright (c)****************************************************
  2. **
  3. ** Copyright (c) 2017 by Kirin co.,ltd. All Rights Reserved.
  4. **
  5. **--------------File Info---------------------------------------------------------------------------------
  6. ** File name         : bsp_tm1620.c
  7. ** Last modified Date: 2017年08月19日
  8. ** Last Version      : Ver 1.0
  9. ** Description             :
  10. **
  11. **------------------------------------------------------------------------------------------------------
  12. ** Created by        : wyl
  13. ** Created date      : 2017年08月19日
  14. ** Version           : Ver 1.0
  15. ** Description       : The original version 初始版本
  16. **
  17. **------------------------------------------------------------------------------------------------------
  18. ** Modified by       :
  19. ** Modified date     :
  20. ** Version           :
  21. ** Description       :
  22. **
  23. ********************************************************************************************************/


  24. /************************************************************************
  25. -------------------------------------------------------------------------
  26. **Debug switch 调试选项
  27. -------------------------------------------------------------------------
  28. ************************************************************************/

  29. /************************************************************************
  30. -------------------------------------------------------------------------
  31. **Include File 包含文件
  32. -------------------------------------------------------------------------
  33. ************************************************************************/
  34. #include <bsp.h>

  35. #include "bsp_tm1620.h"

  36. /************************************************************************
  37. -------------------------------------------------------------------------
  38. **Macro Define 宏定义
  39. -------------------------------------------------------------------------
  40. ************************************************************************/


  41. #define TM1620B_RCC_DIO     RCC_APB2Periph_GPIOB
  42. #define TM1620B_PORT_DIO         GPIOB
  43. #define TM1620B_PIN_DIO         GPIO_Pin_3

  44. #define TM1620B_RCC_CLK     RCC_APB2Periph_GPIOB
  45. #define TM1620B_PORT_CLK         GPIOB
  46. #define TM1620B_PIN_CLK         GPIO_Pin_4

  47. #define TM1620B_RCC_STB     RCC_APB2Periph_GPIOB
  48. #define TM1620B_PORT_STB         GPIOB
  49. #define TM1620B_PIN_STB         GPIO_Pin_5


  50. #define DIO1_LOW        (TM1620B_PORT_DIO->BRR = TM1620B_PIN_DIO)
  51. #define DIO1_HIGH       (TM1620B_PORT_DIO->BSRR = TM1620B_PIN_DIO)

  52. #define DIO_IS_HIGH()        ((TM1620B_PORT_DIO->IDR & TM1620B_PIN_DIO) != 0)

  53. #define STB1_LOW        (TM1620B_PORT_STB->BRR = TM1620B_PIN_STB)
  54. #define STB1_HIGH       (TM1620B_PORT_STB->BSRR = TM1620B_PIN_STB)

  55. #define CLK1_LOW        (TM1620B_PORT_CLK->BRR = TM1620B_PIN_CLK)
  56. #define CLK1_HIGH       (TM1620B_PORT_CLK->BSRR = TM1620B_PIN_CLK)

  57.    
  58. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

  59. #define   WRITE_MODE   0x40    /* 写数据模式 */
  60. #define   READ_MODE    0x42    /* 读数据模式  读取按键 */


  61. /* 显示命令设置 */
  62. #define   DIS_CMD_00    0x00    /* 4位9段 */
  63. #define   DIS_CMD_01    0x01    /* 5位8段 */
  64. #define   DIS_CMD_10    0x02    /* 6位7段 */
  65. #define   DIS_CMD_11    0x03    /* 7位6段 */


  66. #define   AUTO_ADDR_MODE    0x40    /* 地址自动加1 */
  67. #define   FIXED_ADDR_MODE   0x44    /* 固定地址 */

  68. /* 显示控制命令设置 */
  69. #define   LIGHT_LEVE_OFF    0x80    /* 显示关闭 */
  70. #define   LIGHT_LEVE_ON     0x88    /* 显示开启 */

  71. #define   LIGHT_LEVE_1      0x80    /* 设置脉冲宽度为 1/16 */
  72. #define   LIGHT_LEVE_2      0x81    /* 设置脉冲宽度为 2/16 */
  73. #define   LIGHT_LEVE_3      0x82    /* 设置脉冲宽度为 4/16 */
  74. #define   LIGHT_LEVE_4      0x83    /* 设置脉冲宽度为 10/16 */
  75. #define   LIGHT_LEVE_5      0x84    /* 设置脉冲宽度为 11/16 */
  76. #define   LIGHT_LEVE_6      0x85    /* 设置脉冲宽度为 12/16 */
  77. #define   LIGHT_LEVE_7      0x86    /* 设置脉冲宽度为 13/16 */
  78. #define   LIGHT_LEVE_8      0x87    /* 设置脉冲宽度为 14/16 */


  79. /* 地址命令设置 */
  80. #define   ADDR_0      0xC0      /*  00H */
  81. #define   ADDR_1      0xC1      /*  01H */
  82. #define   ADDR_2      0xC2      /*  02H */
  83. #define   ADDR_3      0xC3      /*  03H */
  84. #define   ADDR_4      0xC4      /*  04H */
  85. #define   ADDR_5      0xC5      /*  05H */
  86. #define   ADDR_6      0xC6      /*  06H */
  87. #define   ADDR_7      0xC7      /*  07H */
  88. #define   ADDR_8      0xC8      /*  08H */
  89. #define   ADDR_9      0xC9      /*  09H */
  90. #define   ADDR_10     0xCA      /*  0aH */
  91. #define   ADDR_11     0xCB      /*  0bH */
  92. #define   ADDR_12     0xCC      /*  0cH */
  93. #define   ADDR_13     0xCD      /*  0dH */
  94. #define   ADDR_14     0xCE      /*  0eH */
  95. #define   ADDR_15     0xCF      /*  0fH */


  96. /************************************************************************
  97. -------------------------------------------------------------------------
  98. **Struct Define 结构体定义
  99. -------------------------------------------------------------------------
  100. ************************************************************************/

  101. /************************************************************************
  102. -------------------------------------------------------------------------
  103. **Prototype Declare 函数原型声明
  104. -------------------------------------------------------------------------
  105. ************************************************************************/

  106. /************************************************************************
  107. -------------------------------------------------------------------------
  108. **Global Variable Declare 全局变量声明
  109. -------------------------------------------------------------------------
  110. ************************************************************************/

  111. /************************************************************************
  112. -------------------------------------------------------------------------
  113. **File Static Variable Define Static 变量定义部分
  114. -------------------------------------------------------------------------
  115. ************************************************************************/

  116. /************************************************************************
  117. -------------------------------------------------------------------------
  118. **Function Define  函数定义
  119. -------------------------------------------------------------------------
  120. ************************************************************************/

  121. /*********************************************************************************************************
  122. ** Function name : TM1638_Init
  123. ** Input Para         :
  124. ** Output Para         :
  125. ** Created by    : wyl
  126. ** Created Date  : 2017年08月19日
  127. ** Descriptions         : 数码管初始化
  128. **
  129. ** -------------------------------------------------------------------------------------------------------
  130. ** Modified by   :
  131. ** Modified Date :
  132. ** Descriptions         :
  133. **
  134. ** -------------------------------------------------------------------------------------------------------
  135. **********************************************************************************************************/

  136. void bSP_TM1638Init(void)      
  137. {
  138.         GPIO_InitTypeDef  GPIO_InitStructure;
  139.         
  140.         /* 打开GPIO时钟 */
  141.         RCC_APB2PeriphClockCmd(TM1620B_RCC_DIO, ENABLE);
  142.         RCC_APB2PeriphClockCmd(TM1620B_RCC_CLK, ENABLE);
  143.         RCC_APB2PeriphClockCmd(TM1620B_RCC_STB, ENABLE);

  144.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   /* 输出频率 */
  145.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    //开漏输出可以在不用改变成输入的情况下读取IO的电平
  146.         GPIO_InitStructure.GPIO_Pin = TM1620B_PIN_DIO ;
  147.         GPIO_Init(TM1620B_PORT_DIO,&GPIO_InitStructure);

  148.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   /* 输出频率 */
  149.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           /* 推挽输出 */
  150.         GPIO_InitStructure.GPIO_Pin = TM1620B_PIN_CLK ;
  151.         GPIO_Init(TM1620B_PORT_CLK,&GPIO_InitStructure);

  152.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   /* 输出频率 */
  153.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           /* 推挽输出 */
  154.         GPIO_InitStructure.GPIO_Pin = TM1620B_PIN_STB;
  155.         GPIO_Init(TM1620B_PORT_STB,&GPIO_InitStructure);

  156. }


  157. /* DIO2设置输入模式 */
  158. void DIO_Set_Input(void)
  159. {
  160.     GPIO_InitTypeDef  GPIO_InitStructure;

  161.     GPIO_InitStructure.GPIO_Pin = TM1620B_PIN_DIO;
  162.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;           /* 输入上拉模式 */
  163.         GPIO_Init(TM1620B_PORT_DIO,&GPIO_InitStructure);
  164. }

  165. /* DIO2设置输出模式 */
  166. void DIO_Set_Output(void)
  167. {
  168.     GPIO_InitTypeDef  GPIO_InitStructure;

  169.     GPIO_InitStructure.GPIO_Pin = TM1620B_PIN_DIO;
  170.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  171.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           /* 改为推挽输出  */
  172.         GPIO_Init(TM1620B_PORT_DIO,&GPIO_InitStructure);
  173. }




  174. /* 读数据函数 */
  175. u8 Read_Byte_TM1638(void)                  
  176. {
  177.     u8 temp = 0;
  178.     u8 i;
  179.        
  180.     for (i = 0; i < 8; i++)
  181.     {
  182.         CLK1_LOW;
  183.         
  184.         temp >>= 1;
  185.         
  186.         CLK1_HIGH;
  187.         
  188.         if(DIO_IS_HIGH())
  189.         {
  190.             temp |= 0x80;
  191.         }
  192.     }

  193.     return temp;
  194.    
  195. }


  196. /* 读取按键 */
  197. u32 Read_Key_Value(void)
  198. {
  199.     u32 l_u32AtCmdRet = 0;
  200.    
  201.         u8 l_u8key_buff[4], i;
  202.                
  203.         STB1_LOW;

  204.         /* 读取数据 模式 */
  205.         Write_Byte_TM1638(READ_MODE);
  206.        
  207.         /* 拉高数据线 */
  208.         DIO1_HIGH;

  209.     /* 设置DIO 输入上拉模式 */
  210.         DIO_Set_Input();

  211.     /* 获取 BYTE1—BYTE3 */
  212.     for (i = 0; i < 3; i++)
  213.     {
  214.         l_u8key_buff[i] = Read_Byte_TM1638(); //读取按键的4个BYTE
  215.     }
  216.    
  217.     /* 设置DIO 推挽输出模式 */
  218.     DIO_Set_Output();
  219.    
  220.     STB1_HIGH;
  221.    
  222.     l_u32AtCmdRet = ( (l_u8key_buff[2]<<16)
  223.                     +(l_u8key_buff[1]<<8)
  224.                     +l_u8key_buff[0] );
  225.   
  226.     return l_u32AtCmdRet;
  227.    
  228. }



  229. /*********************************************************************************************************
  230. ** Function name : WriteByteTo_TM1638
  231. ** Input Para         :
  232. ** Output Para         :
  233. ** Created by    : wyl
  234. ** Created Date  : 2017年08月19日
  235. ** Descriptions         : 实现一个字节的写入
  236. **
  237. ** -------------------------------------------------------------------------------------------------------
  238. ** Modified by   :
  239. ** Modified Date :
  240. ** Descriptions         :
  241. **
  242. ** -------------------------------------------------------------------------------------------------------
  243. **********************************************************************************************************/

  244. void Write_Byte_TM1638(u8 byte)
  245. {
  246.     u8 i;

  247.         STB1_LOW;
  248.        
  249.     for (i = 0; i < 8; i++)
  250.     {
  251.         CLK1_LOW;

  252.         /* 最小100ns */
  253.         //delayUS(1);
  254.         
  255.         if ((byte & 0x01) == 1)
  256.         {
  257.             DIO1_HIGH;
  258.         }
  259.         else
  260.         {
  261.             DIO1_LOW;
  262.         }

  263.         CLK1_HIGH;

  264.         /* 最小100ns */
  265.         //delayUS(1);  
  266.         
  267.         byte >>= 1;
  268.     }   
  269.    
  270. }

  271. /*********************************************************************************************************
  272. ** Function name : WriteCmdTo_TM1638
  273. ** Input Para         :
  274. ** Output Para         :
  275. ** Created by    : wyl
  276. ** Created Date  : 2017年08月19日
  277. ** Descriptions         : 发送命令的函数
  278. **
  279. ** -------------------------------------------------------------------------------------------------------
  280. ** Modified by   :
  281. ** Modified Date :
  282. ** Descriptions         :
  283. **
  284. ** -------------------------------------------------------------------------------------------------------
  285. **********************************************************************************************************/

  286. void WriteCmdTo_TM1638(u8 cmd)
  287. {
  288.     STB1_HIGH;
  289.     CLK1_HIGH;
  290.    // delayUS(2);
  291.     DIO1_HIGH;

  292.     Write_Byte_TM1638(cmd);
  293. }


  294. void Write_Buffer_TM1638(u8 *p_buffer, u8 buffer_len)
  295. {
  296.     u8 i = 0;

  297.     /* 显示命令设置 4位9段 */
  298.         WriteCmdTo_TM1638(DIS_CMD_01);  
  299.        
  300.     /* 数据命令 自动地址增加模式 */
  301.         WriteCmdTo_TM1638(AUTO_ADDR_MODE);  

  302.         /* 显示地址 00H单元开始 */
  303.         WriteCmdTo_TM1638(ADDR_0);  

  304.     /* 地址自动累加 */

  305.     for(i=0; i<buffer_len;i++)
  306.     {
  307.         Write_Byte_TM1638(p_buffer[i]);
  308.     }

  309.         /* 亮度调节 */
  310.     WriteCmdTo_TM1638(LIGHT_LEVE_ON|LIGHT_LEVE_7);
  311.    

  312.     STB1_HIGH;   
  313. }





  314. /**********************************END**********************************/


weiyuliang 发表于 2018-3-29 21:28 | 显示全部楼层
楼主用这个代码测试一下,我之前用的F103驱动LED指示灯的,测试程序没有问题
android2 发表于 2018-3-30 21:03 | 显示全部楼层
是不是一闪而过,没有视觉停留啊
数据总线 发表于 2018-6-16 08:52 | 显示全部楼层
weiyuliang 发表于 2018-3-29 21:28
楼主用这个代码测试一下,我之前用的F103驱动LED指示灯的,测试程序没有问题 ...

程序写得挺好的,比较有条理性,程序我验证过了,没有问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

138

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部