搜索
查看: 1672|回复: 3

[原创] TM1638芯片驱动

[复制链接]

该用户从未签到

37

主题

551

帖子

62

蝴蝶豆

金牌会员

最后登录
2024-3-30
发表于 2019-4-25 10:44:10 | 显示全部楼层 |阅读模式
本帖最后由 songshiqun2010 于 2019-4-25 11:03 编辑

复制代码
1.头文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    tm1638.h
  4.   * @author  byron
  5.   * @version V1.0.0
  6.   * @date    18-March-2019
  7.   * @brief   This file contains all the functions prototypes for the
  8.   *          tm1638.c driver.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>© COPYRIGHT(c) 2019 sonshiqun1225@163.com</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of sonshiqun1225@163.com nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38. #ifndef _TM1638_H_
  39. #define _TM1638_H_

  40. #include <stdint.h>
  41. /** @addtogroup BSP
  42.   * @{
  43.   */

  44. /** @addtogroup Component
  45.   * @{
  46.   */
  47.    
  48. /** @defgroup TM1638
  49.   * @{
  50.   */

  51. /* Exported constants --------------------------------------------------------*/
  52. /** @defgroup TM1638_Exported_Constants TM1638 Exported Constants
  53.   * @{
  54.   */

  55. /** @defgroup TM1638_Command TM1638 Command
  56.   * @{
  57.   */
  58. #define TM1638_DATA_CMD                                        ((uint8_t)0x40U)
  59. #define TM1638_DISP_CMD                                        ((uint8_t)0x80U)
  60. #define TM1638_ADDR_CMD                                        ((uint8_t)0xC0U)

  61. #define TM1638_DATA_CMD_WRITE_GRAM_INC                ((uint8_t)0x40U)
  62. #define TM1638_DATA_CMD_WRITE_GRAM_FIX                ((uint8_t)0x44U)
  63. #define TM1638_DATA_CMD_READ_KEY                        ((uint8_t)0x42U)

  64. #define TM1638_DISP_CMD_OFF                                ((uint8_t)0x80U)
  65. #define TM1638_DISP_CMD_ON                                    ((uint8_t)0x88U)
  66. #define TM1638_DISP_CMD_LV0                                 ((uint8_t)0x88U)
  67. #define TM1638_DISP_CMD_LV1                                 ((uint8_t)0x89U)
  68. #define TM1638_DISP_CMD_LV2                                 ((uint8_t)0x8AU)
  69. #define TM1638_DISP_CMD_LV3                                 ((uint8_t)0x8BU)
  70. #define TM1638_DISP_CMD_LV4                                 ((uint8_t)0x8CU)
  71. #define TM1638_DISP_CMD_LV5                                 ((uint8_t)0x8DU)
  72. #define TM1638_DISP_CMD_LV6                                 ((uint8_t)0x8EU)
  73. #define TM1638_DISP_CMD_LV7                                 ((uint8_t)0x8FU)

  74. #define TM1638_ADDR_CMD_GRAM0                          ((uint8_t)0xC0U)
  75. #define TM1638_ADDR_CMD_GRAM1                          ((uint8_t)0xC1U)
  76. #define TM1638_ADDR_CMD_GRAM2                          ((uint8_t)0xC2U)
  77. #define TM1638_ADDR_CMD_GRAM3                          ((uint8_t)0xC3U)
  78. #define TM1638_ADDR_CMD_GRAM4                          ((uint8_t)0xC4U)
  79. #define TM1638_ADDR_CMD_GRAM5                          ((uint8_t)0xC5U)
  80. #define TM1638_ADDR_CMD_GRAM6                          ((uint8_t)0xC6U)
  81. #define TM1638_ADDR_CMD_GRAM7                          ((uint8_t)0xC7U)
  82. #define TM1638_ADDR_CMD_GRAM8                          ((uint8_t)0xC8U)
  83. #define TM1638_ADDR_CMD_GRAM9                          ((uint8_t)0xC9U)
  84. #define TM1638_ADDR_CMD_GRAM10                         ((uint8_t)0xCAU)
  85. #define TM1638_ADDR_CMD_GRAM11                         ((uint8_t)0xCBU)
  86. #define TM1638_ADDR_CMD_GRAM12                         ((uint8_t)0xCCU)
  87. #define TM1638_ADDR_CMD_GRAM13                         ((uint8_t)0xCDU)
  88. #define TM1638_ADDR_CMD_GRAM14                         ((uint8_t)0xCEU)
  89. #define TM1638_ADDR_CMD_GRAM15                         ((uint8_t)0xCFU)
  90. /**
  91.   * @}
  92.   */

  93. /** @defgroup TM1638_Status TM1638 Status
  94.   * @{
  95.   */

  96. #define  TM1638_STATUS_READ_ERROR            ((int32_t)-5)
  97. #define  TM1638_STATUS_WRITE_ERROR           ((int32_t)-4)
  98. #define  TM1638_STATUS_ADDRESS_ERROR         ((int32_t)-3)
  99. #define  TM1638_STATUS_RESET_TIMEOUT         ((int32_t)-2)
  100. #define  TM1638_STATUS_ERROR                 ((int32_t)-1)
  101. #define  TM1638_STATUS_OK                    ((int32_t) 0)
  102. #define  TM1638_STATUS_LINK_DOWN             ((int32_t) 1)
  103. #define  TM1638_STATUS_AUTONEGO_NOTDONE      ((int32_t) 2)
  104. /**
  105.   * @}
  106.   */
  107. /**
  108.   * @}
  109.   */

  110. /* Exported types ------------------------------------------------------------*/
  111. /** @defgroup TM1638_Exported_Types TM1638 Exported Types
  112.   * @{
  113.   */
  114. typedef enum{
  115.         tube_0 = 0x3F,
  116.         tube_1 = 0x06,
  117.         tube_2 = 0x5B,
  118.         tube_3 = 0x4F,
  119.         tube_4 = 0x66,
  120.         tube_5 = 0x6D,
  121.         tube_6 = 0x7D,
  122.         tube_7 = 0x07,
  123.         tube_8 = 0x7F,
  124.         tube_9 = 0x6F,
  125.         tube_A = 0x77,
  126.         tube_b = 0x7C,
  127.         tube_C = 0x39,
  128.         tube_d = 0x5E,
  129.         tube_E = 0x79,
  130.         tube_F = 0x71,
  131.         tube_P = 0x73,
  132.         tube_J = 0x0E,
  133.         tube_r = 0x50,
  134.         tube_s = 0x6D,
  135.         tube_t = 0x78,
  136.         tube_dot = 0x80,
  137.         tube__ = 0x40,
  138.         tube_no = 0x00,
  139. }tm1638_tube_enum_t;
  140. typedef int32_t  (*tm1638_Init_Func) (void);
  141. typedef int32_t  (*tm1638_DeInit_Func) (void);
  142. typedef int32_t  (*tm1638_ReadReg_Func)   (uint8_t*);
  143. typedef int32_t  (*tm1638_WriteReg_Func)  (uint8_t);
  144. typedef int32_t  (*tm1638_WriteSTB_Func)  (uint8_t);
  145. typedef int32_t  (*tm1638_DelayUS_Func)  (uint16_t);

  146. typedef struct
  147. {
  148.   tm1638_Init_Func      Init;
  149.   tm1638_DeInit_Func    DeInit;
  150.   tm1638_WriteReg_Func  WriteReg;
  151.   tm1638_ReadReg_Func   ReadReg;
  152.   tm1638_WriteSTB_Func  WriteSTB;
  153.   tm1638_DelayUS_Func   DelayUS;
  154. }tm1638_IOCtx_t;


  155. typedef struct
  156. {
  157.   uint32_t            DevAddr;
  158.   uint32_t            Is_Initialized;
  159.   tm1638_IOCtx_t     IO;
  160.   void               *pData;
  161. }tm1638_Object_t;
  162. /**
  163.   * @}
  164.   */
  165. /* Exported macro ------------------------------------------------------------*/
  166. /* Exported functions --------------------------------------------------------*/
  167. /** @defgroup TM1638_Exported_Functions TM1638 Exported Functions
  168.   * @{
  169.   */
  170. int32_t TM1638_RegisterBusIO(tm1638_Object_t *pObj, tm1638_IOCtx_t *ioctx);
  171. int32_t TM1638_Init(tm1638_Object_t *pObj,uint8_t mode);
  172. int32_t TM1638_DeInit(tm1638_Object_t *pObj);
  173. int32_t TM1638_ReadKey(tm1638_Object_t *pObj,uint8_t *pkey);
  174. int32_t TM1638_WriteGRAM_Fix(tm1638_Object_t *pObj,uint8_t add,uint8_t data);
  175. int32_t TM1638_WriteGRAM_Inc(tm1638_Object_t *pObj,uint8_t add,uint8_t *pdata,uint8_t len);
  176. /**
  177.   * @}
  178.   */
  179. #endif /* _TM1638_H_ */

  180. /**
  181.   * @}
  182.   */

  183. /**
  184.   * @}
  185.   */

  186. /**
  187.   * @}
  188.   */      
  189. /************************ (C) COPYRIGHT sonshiqun1225@163.com *****END OF FILE****/
复制代码

2.源文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    tm1638.c
  4.   * @author  byron
  5.   * @version V1.0.0
  6.   * @date    18-March-2019
  7.   * @brief   This file provides a set of functions needed to manage the TM1638
  8.   *          devices.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>© COPYRIGHT(c) 2017 sonshiqun1225@163.com</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of sonshiqun1225@163.com nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "tm1638.h"

  40. /** @addtogroup BSP
  41.   * @{
  42.   */

  43. /** @addtogroup Component
  44.   * @{
  45.   */

  46. /** @defgroup TM1638 TM1638
  47.   * @{
  48.   */  

  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /** @defgroup TM1638_Private_Defines TM1638 Private Defines
  52.   * @{
  53.   */
  54. #define TM1638_INIT_TIMEOUT  ((uint16_t)5000)
  55. #define TM1638_READ_KEY_DELAY_NUS ((uint16_t)10)
  56. /**
  57.   * @}
  58.   */

  59. /* Private macro -------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private function prototypes -----------------------------------------------*/
  62. /* Private functions ---------------------------------------------------------*/

  63. /** @defgroup TM1638_Private_Functions TM1638 Private Functions
  64.   * @{
  65.   */

  66. /**
  67.   * @brief  send command to tm1638
  68.   * @param  pObj: device object  of tm1638_Object_t.
  69.   * @param  cmd: @TM1638_Command.
  70.   * @retval TM1638_STATUS_OK  if OK
  71.   *         TM1638_STATUS_ERROR if missing mandatory function
  72.   */
  73. static int32_t TM1638_WriteCommand(tm1638_Object_t *pObj,uint8_t cmd)
  74. {
  75.         pObj->IO.WriteSTB(0);
  76.         pObj->IO.WriteReg(cmd);
  77.         pObj->IO.WriteSTB(1);
  78.         return TM1638_STATUS_OK;
  79. }

  80. /**
  81.   * @brief  Clear tm1638 display
  82.   * @param  pObj: device object  of tm1638_Object_t.
  83.   * @retval TM1638_STATUS_OK  if OK
  84.   *         TM1638_STATUS_ERROR if missing mandatory function
  85.   */
  86. static void TM1638_ClearGRAM(tm1638_Object_t *pObj)
  87. {
  88.         uint8_t i;
  89.         TM1638_WriteCommand(pObj,TM1638_DATA_CMD_WRITE_GRAM_INC);
  90.         pObj->IO.WriteSTB(0);
  91.         pObj->IO.WriteReg(TM1638_ADDR_CMD_GRAM0);
  92.         for(i=0;i<16;i++)
  93.         {
  94.                 pObj->IO.WriteReg(0x00);
  95.         }
  96.         pObj->IO.WriteSTB(1);
  97. }

  98. /**
  99.   * @brief  Register IO functions to component object
  100.   * @param  pObj: device object  of tm1638_Object_t.
  101.   * @param  ioctx: holds device IO functions.
  102.   * @retval TM1638_STATUS_OK  if OK
  103.   *         TM1638_STATUS_ERROR if missing mandatory function
  104.   */
  105. int32_t  TM1638_RegisterBusIO(tm1638_Object_t *pObj, tm1638_IOCtx_t *ioctx)
  106. {
  107.   if(!pObj || !ioctx->ReadReg || !ioctx->WriteReg || !ioctx->DelayUS || !ioctx->WriteSTB)
  108.   {
  109.     return TM1638_STATUS_ERROR;
  110.   }

  111.   pObj->IO.Init = ioctx->Init;
  112.   pObj->IO.DeInit = ioctx->DeInit;
  113.   pObj->IO.ReadReg = ioctx->ReadReg;
  114.   pObj->IO.WriteReg = ioctx->WriteReg;
  115.   pObj->IO.WriteSTB = ioctx->WriteSTB;
  116.   pObj->IO.DelayUS = ioctx->DelayUS;

  117.   return TM1638_STATUS_OK;
  118. }
  119. /**
  120.   * @brief  Initialize the TM1638 and configure the needed hardware resources
  121.   * @param  pObj: device object tm1638_Object_t.
  122.   * @retval TM1638_STATUS_OK  if OK
  123.   *         TM1638_STATUS_ADDRESS_ERROR if cannot find device address
  124.   *         TM1638_STATUS_READ_ERROR if connot read register
  125.   *         TM1638_STATUS_WRITE_ERROR if connot write to register
  126.   *         TM1638_STATUS_RESET_TIMEOUT if cannot perform a software reset
  127.   */
  128. int32_t TM1638_Init(tm1638_Object_t *pObj,uint8_t mode)
  129. {
  130.         int32_t status = TM1638_STATUS_OK;

  131.         if(pObj->Is_Initialized == 0)
  132.         {
  133.                 if(pObj->IO.Init != 0)
  134.                 {
  135.                         /* GPIO and Clocks initialization */
  136.                         pObj->IO.Init();
  137.                 }
  138.                 TM1638_WriteCommand(pObj,mode);
  139.                 TM1638_ClearGRAM(pObj);
  140.                 if(status == TM1638_STATUS_OK)
  141.                 {
  142.                         pObj->IO.DelayUS(TM1638_INIT_TIMEOUT);
  143.                         pObj->Is_Initialized = 1;
  144.                 }
  145.         }
  146.         return status;
  147. }

  148. /**
  149.   * @brief  De-Initialize the TM1638 and it's hardware resources
  150.   * @param  pObj: device object tm1638_Object_t.
  151.   * @retval TM1638_STATUS_OK  if OK
  152.   *         TM1638_STATUS_ERROR if missing mandatory function
  153.   */
  154. int32_t TM1638_DeInit(tm1638_Object_t *pObj)
  155. {
  156.         if(pObj->Is_Initialized)
  157.         {
  158.                 if(pObj->IO.DeInit != 0)
  159.                 {
  160.                         if(pObj->IO.DeInit() < 0)
  161.                         {
  162.                                 return TM1638_STATUS_ERROR;
  163.                         }
  164.                 }

  165.                 pObj->Is_Initialized = 0;
  166.         }
  167.         return TM1638_STATUS_OK;
  168. }

  169. /**
  170.   * @brief  Read key value of tm1638
  171.   * @param  pObj: device object  of tm1638_Object_t.
  172.   * @param  pkey: pointer to key value.
  173.   * @retval TM1638_STATUS_OK  if OK
  174.   *         TM1638_STATUS_ERROR if missing mandatory function
  175.   */
  176. int32_t TM1638_ReadKey(tm1638_Object_t *pObj,uint8_t *pkey)
  177. {
  178.         uint8_t tmp,i,ret=0;
  179.         pObj->IO.WriteSTB(0);
  180.         pObj->IO.WriteReg(TM1638_DATA_CMD_READ_KEY);
  181.         pObj->IO.DelayUS(TM1638_READ_KEY_DELAY_NUS);
  182.         for(i=0;i<4;i++){
  183.                 pObj->IO.ReadReg(&tmp);
  184.                 ret|=tmp<<i;
  185.         }
  186.         pObj->IO.WriteSTB(1);
  187.         *pkey = ret;
  188.         return TM1638_STATUS_OK;
  189. }
  190. /**
  191.   * @brief  Write display RAM in address fixed mode
  192.   * @param  pObj: device object  of tm1638_Object_t.
  193.   * @param  add: GRAM address.
  194.   * @param  data: display data.
  195.   * @retval TM1638_STATUS_OK  if OK
  196.   *         TM1638_STATUS_ERROR if missing mandatory function
  197.   */
  198. int32_t TM1638_WriteGRAM_Fix(tm1638_Object_t *pObj,uint8_t add,uint8_t data)
  199. {
  200.         TM1638_WriteCommand(pObj,TM1638_DATA_CMD_WRITE_GRAM_FIX);
  201.         pObj->IO.WriteSTB(0);
  202.         pObj->IO.WriteReg(TM1638_ADDR_CMD|add);
  203.         pObj->IO.WriteReg(data);
  204.         pObj->IO.WriteSTB(1);
  205.         return TM1638_STATUS_OK;
  206. }
  207. /**
  208.   * @brief  Write display RAM in address increment  mode
  209.   * @param  pObj: device object  of tm1638_Object_t.
  210.   * @param  add: index of display address.
  211.   * @param  pdata: pointer display data array.
  212.   * @param  len: size of data array in byte.
  213.   * @retval TM1638_STATUS_OK  if OK
  214.   *         TM1638_STATUS_ERROR if missing mandatory function
  215.   */
  216. int32_t TM1638_WriteGRAM_Inc(tm1638_Object_t *pObj,uint8_t add,uint8_t *pdata,uint8_t len)
  217. {
  218.         uint8_t i;
  219.         TM1638_WriteCommand(pObj,TM1638_DATA_CMD_WRITE_GRAM_INC);
  220. //        pObj->IO.DelayUS(TM1638_READ_KEY_DELAY_NUS);
  221.         pObj->IO.WriteSTB(0);
  222.         pObj->IO.WriteReg(TM1638_ADDR_CMD|add);
  223.         for(i=0;i<len;i++){
  224.                 pObj->IO.WriteReg(*pdata++);
  225.         }
  226.         pObj->IO.WriteSTB(1);
  227.         return TM1638_STATUS_OK;
  228. }
  229. /**
  230.   * @}
  231.   */

  232. /**
  233.   * @}
  234.   */

  235. /**
  236.   * @}
  237.   */

  238. /**
  239.   * @}
  240.   */      
  241. /************************ (C) COPYRIGHT sonshiqun1225@163.com *****END OF FILE****/
复制代码
3.示例
  1. #include "stm32h7xx_hal.h"

  2. #include "cmsis_os.h"
  3. #include "tm1638io_drv.h"
  4. #define TM_IO_DELAY_NUS 5

  5. #define TM_DIO_SET_IN() do{GPIOD->MODER &= ~(3<<(2*2));}while(0);
  6. #define TM_DIO_SET_OUT() do{GPIOD->MODER &= ~(3<<(2*2));GPIOD->MODER |= 1<<(2*2);}while(0);

  7. #define TM_STB_1  (GPIOG->BSRRL = GPIO_PIN_3)
  8. #define TM_STB_0  (GPIOG->BSRRH = GPIO_PIN_3)
  9. #define TM_CLK_1  (GPIOG->BSRRL = GPIO_PIN_2)
  10. #define TM_CLK_0  (GPIOG->BSRRH = GPIO_PIN_2)
  11. #define TM_DIO_1  (GPIOD->BSRRL = GPIO_PIN_2)
  12. #define TM_DIO_0  (GPIOD->BSRRH = GPIO_PIN_2)
  13. #define TM_DIO_IN ((GPIOD->IDR & GPIO_PIN_2)?1:0)
  14. //#define TM_STB_1  HAL_GPIO_WritePin(GPIOG,GPIO_PIN_3,GPIO_PIN_SET)
  15. //#define TM_STB_0  HAL_GPIO_WritePin(GPIOG,GPIO_PIN_3,GPIO_PIN_RESET)
  16. //#define TM_CLK_1  HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_SET)
  17. //#define TM_CLK_0  HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_RESET)
  18. //#define TM_DIO_1  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET)
  19. //#define TM_DIO_0  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET)
  20. //#define TM_DIO_IN (HAL_GPIO_ReadPin(GPIOD,GPIO_PIN_2)?1:0)
  21. int32_t  TM1638_IO_Init(void)
  22. {
  23.         TM_STB_1;
  24.         TM_CLK_0;
  25.         TM_DIO_SET_OUT();
  26.         TM_DIO_0;
  27.         return 0;
  28. }
  29. int32_t  TM1638_IO_DeInit(void)
  30. {
  31.         return 0;
  32. }

  33. int32_t  TM1638_IO_DelayUS(uint16_t d)
  34. {
  35. //        uint32_t i;
  36. //        while(d--){
  37. //                for(i=0;i<120;i++)
  38. //                {
  39. //                        ;
  40. //                }
  41. //                HAL_Delay(1);
  42. //                osDelay(1);
  43. //        }
  44.         osal_usleep(d);
  45.         return 0;
  46. }
  47. int32_t  TM1638_IO_ReadReg(uint8_t* p)
  48. {
  49.         unsigned char i;
  50.         unsigned char temp=0;
  51.         TM_DIO_SET_IN();
  52.         for(i=0;i<8;i++)
  53.         {
  54.                 temp>>=1;
  55.                 TM_CLK_0;
  56.                 TM1638_IO_DelayUS(TM_IO_DELAY_NUS);
  57.                 TM_CLK_1;
  58.                 if(TM_DIO_IN)
  59.                         temp|=0x80;
  60.         }
  61.         TM_DIO_SET_OUT();
  62.         *p = temp;
  63.         return 0;
  64. }
  65. int32_t  TM1638_IO_WriteReg(uint8_t d)
  66. {
  67.         unsigned char i;
  68.         for(i=0;i<8;i++)
  69.         {
  70.                 TM_CLK_0;
  71.                 if(d&0x01)
  72.                         TM_DIO_1;
  73.                 else
  74.                         TM_DIO_0;
  75.                 d>>=1;
  76.                 TM_CLK_1;
  77.                 TM1638_IO_DelayUS(TM_IO_DELAY_NUS);
  78.         }
  79.         return 0;
  80. }
  81. int32_t  TM1638_IO_WriteSTB(uint8_t s)
  82. {
  83.         s ? TM_STB_1 : TM_STB_0;
  84.         return 0;
  85. }
  86. tm1638_Object_t TM1638;
  87. tm1638_IOCtx_t  TM1638_IOCtx = {
  88.                 .Init = TM1638_IO_Init,
  89.                 .DeInit = TM1638_IO_DeInit,
  90.                 .WriteReg = TM1638_IO_WriteReg,
  91.                 .ReadReg = TM1638_IO_ReadReg,
  92.                 .WriteSTB = TM1638_IO_WriteSTB,
  93.                 .DelayUS = TM1638_IO_DelayUS
  94. };
  95. int32_t OP_Init(void)
  96. {

  97.         osMutexDef(myMutexOp);
  98.         myMutexOpHandle = osMutexCreate(osMutex(myMutexOp));
  99.         /* Set TM1638 IO functions */
  100.         if(TM1638_RegisterBusIO(&TM1638, &TM1638_IOCtx)){
  101.                 return -1;
  102.         }
  103.     /* Initialize the TM1638 */
  104.     if(TM1638_Init(&TM1638,TM1638_DISP_CMD_LV2)){
  105.             return -2;
  106.     }
  107.     op.Is_Initialized = ManufacturerSpecificProfileArea[0].bSubCount;
  108.    
  109.         return 0;
  110. }
复制代码



tm1638.zip

4.42 KB, 下载次数: 15, 下载积分: ST金币 -1

tm1638层驱动

tm1638io_drv.zip

1.15 KB, 下载次数: 14, 下载积分: ST金币 -1

stm32层驱动

回复

使用道具 举报

该用户从未签到

1492

主题

2703

帖子

0

蝴蝶豆

管理员

最后登录
2021-5-11
发表于 2019-4-26 17:13:36 | 显示全部楼层
谢谢分享
回复

使用道具 举报

该用户从未签到

0

主题

5

帖子

0

蝴蝶豆

初级会员

最后登录
2019-12-14
发表于 2019-6-20 20:31:58 | 显示全部楼层
请问TM1638全灭的时候有的灯微亮,用AIP1638全灭的时候没问题,但是会不定时的全黑一下,有可能是什么问题
回复 支持 反对

使用道具 举报

该用户从未签到

37

主题

551

帖子

62

蝴蝶豆

金牌会员

最后登录
2024-3-30
 楼主| 发表于 2019-6-21 08:02:39 | 显示全部楼层
‭星光 发表于 2019-6-20 20:31
请问TM1638全灭的时候有的灯微亮,用AIP1638全灭的时候没问题,但是会不定时的全黑一下,有可能是什么问题 ...

可能是硬件设计的问题,看看芯片的电气参数。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|论坛-意法半导体STM32/STM8技术社区

GMT+8, 2024-4-25 00:50 , Processed in 0.164321 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

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