在线时间222 小时
UID394460
ST金币2639
蝴蝶豆62
注册时间2012-10-12
该用户从未签到
金牌会员
- 最后登录
- 2024-3-30
|
本帖最后由 songshiqun2010 于 2019-4-25 11:03 编辑
1.头文件 - /**
- ******************************************************************************
- * @file tm1638.h
- * @author byron
- * @version V1.0.0
- * @date 18-March-2019
- * @brief This file contains all the functions prototypes for the
- * tm1638.c driver.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2019 sonshiqun1225@163.com</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of sonshiqun1225@163.com nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- #ifndef _TM1638_H_
- #define _TM1638_H_
- #include <stdint.h>
- /** @addtogroup BSP
- * @{
- */
- /** @addtogroup Component
- * @{
- */
-
- /** @defgroup TM1638
- * @{
- */
- /* Exported constants --------------------------------------------------------*/
- /** @defgroup TM1638_Exported_Constants TM1638 Exported Constants
- * @{
- */
- /** @defgroup TM1638_Command TM1638 Command
- * @{
- */
- #define TM1638_DATA_CMD ((uint8_t)0x40U)
- #define TM1638_DISP_CMD ((uint8_t)0x80U)
- #define TM1638_ADDR_CMD ((uint8_t)0xC0U)
- #define TM1638_DATA_CMD_WRITE_GRAM_INC ((uint8_t)0x40U)
- #define TM1638_DATA_CMD_WRITE_GRAM_FIX ((uint8_t)0x44U)
- #define TM1638_DATA_CMD_READ_KEY ((uint8_t)0x42U)
- #define TM1638_DISP_CMD_OFF ((uint8_t)0x80U)
- #define TM1638_DISP_CMD_ON ((uint8_t)0x88U)
- #define TM1638_DISP_CMD_LV0 ((uint8_t)0x88U)
- #define TM1638_DISP_CMD_LV1 ((uint8_t)0x89U)
- #define TM1638_DISP_CMD_LV2 ((uint8_t)0x8AU)
- #define TM1638_DISP_CMD_LV3 ((uint8_t)0x8BU)
- #define TM1638_DISP_CMD_LV4 ((uint8_t)0x8CU)
- #define TM1638_DISP_CMD_LV5 ((uint8_t)0x8DU)
- #define TM1638_DISP_CMD_LV6 ((uint8_t)0x8EU)
- #define TM1638_DISP_CMD_LV7 ((uint8_t)0x8FU)
- #define TM1638_ADDR_CMD_GRAM0 ((uint8_t)0xC0U)
- #define TM1638_ADDR_CMD_GRAM1 ((uint8_t)0xC1U)
- #define TM1638_ADDR_CMD_GRAM2 ((uint8_t)0xC2U)
- #define TM1638_ADDR_CMD_GRAM3 ((uint8_t)0xC3U)
- #define TM1638_ADDR_CMD_GRAM4 ((uint8_t)0xC4U)
- #define TM1638_ADDR_CMD_GRAM5 ((uint8_t)0xC5U)
- #define TM1638_ADDR_CMD_GRAM6 ((uint8_t)0xC6U)
- #define TM1638_ADDR_CMD_GRAM7 ((uint8_t)0xC7U)
- #define TM1638_ADDR_CMD_GRAM8 ((uint8_t)0xC8U)
- #define TM1638_ADDR_CMD_GRAM9 ((uint8_t)0xC9U)
- #define TM1638_ADDR_CMD_GRAM10 ((uint8_t)0xCAU)
- #define TM1638_ADDR_CMD_GRAM11 ((uint8_t)0xCBU)
- #define TM1638_ADDR_CMD_GRAM12 ((uint8_t)0xCCU)
- #define TM1638_ADDR_CMD_GRAM13 ((uint8_t)0xCDU)
- #define TM1638_ADDR_CMD_GRAM14 ((uint8_t)0xCEU)
- #define TM1638_ADDR_CMD_GRAM15 ((uint8_t)0xCFU)
- /**
- * @}
- */
- /** @defgroup TM1638_Status TM1638 Status
- * @{
- */
- #define TM1638_STATUS_READ_ERROR ((int32_t)-5)
- #define TM1638_STATUS_WRITE_ERROR ((int32_t)-4)
- #define TM1638_STATUS_ADDRESS_ERROR ((int32_t)-3)
- #define TM1638_STATUS_RESET_TIMEOUT ((int32_t)-2)
- #define TM1638_STATUS_ERROR ((int32_t)-1)
- #define TM1638_STATUS_OK ((int32_t) 0)
- #define TM1638_STATUS_LINK_DOWN ((int32_t) 1)
- #define TM1638_STATUS_AUTONEGO_NOTDONE ((int32_t) 2)
- /**
- * @}
- */
- /**
- * @}
- */
- /* Exported types ------------------------------------------------------------*/
- /** @defgroup TM1638_Exported_Types TM1638 Exported Types
- * @{
- */
- typedef enum{
- tube_0 = 0x3F,
- tube_1 = 0x06,
- tube_2 = 0x5B,
- tube_3 = 0x4F,
- tube_4 = 0x66,
- tube_5 = 0x6D,
- tube_6 = 0x7D,
- tube_7 = 0x07,
- tube_8 = 0x7F,
- tube_9 = 0x6F,
- tube_A = 0x77,
- tube_b = 0x7C,
- tube_C = 0x39,
- tube_d = 0x5E,
- tube_E = 0x79,
- tube_F = 0x71,
- tube_P = 0x73,
- tube_J = 0x0E,
- tube_r = 0x50,
- tube_s = 0x6D,
- tube_t = 0x78,
- tube_dot = 0x80,
- tube__ = 0x40,
- tube_no = 0x00,
- }tm1638_tube_enum_t;
- typedef int32_t (*tm1638_Init_Func) (void);
- typedef int32_t (*tm1638_DeInit_Func) (void);
- typedef int32_t (*tm1638_ReadReg_Func) (uint8_t*);
- typedef int32_t (*tm1638_WriteReg_Func) (uint8_t);
- typedef int32_t (*tm1638_WriteSTB_Func) (uint8_t);
- typedef int32_t (*tm1638_DelayUS_Func) (uint16_t);
- typedef struct
- {
- tm1638_Init_Func Init;
- tm1638_DeInit_Func DeInit;
- tm1638_WriteReg_Func WriteReg;
- tm1638_ReadReg_Func ReadReg;
- tm1638_WriteSTB_Func WriteSTB;
- tm1638_DelayUS_Func DelayUS;
- }tm1638_IOCtx_t;
- typedef struct
- {
- uint32_t DevAddr;
- uint32_t Is_Initialized;
- tm1638_IOCtx_t IO;
- void *pData;
- }tm1638_Object_t;
- /**
- * @}
- */
- /* Exported macro ------------------------------------------------------------*/
- /* Exported functions --------------------------------------------------------*/
- /** @defgroup TM1638_Exported_Functions TM1638 Exported Functions
- * @{
- */
- int32_t TM1638_RegisterBusIO(tm1638_Object_t *pObj, tm1638_IOCtx_t *ioctx);
- int32_t TM1638_Init(tm1638_Object_t *pObj,uint8_t mode);
- int32_t TM1638_DeInit(tm1638_Object_t *pObj);
- int32_t TM1638_ReadKey(tm1638_Object_t *pObj,uint8_t *pkey);
- int32_t TM1638_WriteGRAM_Fix(tm1638_Object_t *pObj,uint8_t add,uint8_t data);
- int32_t TM1638_WriteGRAM_Inc(tm1638_Object_t *pObj,uint8_t add,uint8_t *pdata,uint8_t len);
- /**
- * @}
- */
- #endif /* _TM1638_H_ */
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT sonshiqun1225@163.com *****END OF FILE****/
复制代码
2.源文件 - /**
- ******************************************************************************
- * @file tm1638.c
- * @author byron
- * @version V1.0.0
- * @date 18-March-2019
- * @brief This file provides a set of functions needed to manage the TM1638
- * devices.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2017 sonshiqun1225@163.com</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of sonshiqun1225@163.com nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "tm1638.h"
- /** @addtogroup BSP
- * @{
- */
- /** @addtogroup Component
- * @{
- */
- /** @defgroup TM1638 TM1638
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /** @defgroup TM1638_Private_Defines TM1638 Private Defines
- * @{
- */
- #define TM1638_INIT_TIMEOUT ((uint16_t)5000)
- #define TM1638_READ_KEY_DELAY_NUS ((uint16_t)10)
- /**
- * @}
- */
-
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /** @defgroup TM1638_Private_Functions TM1638 Private Functions
- * @{
- */
- /**
- * @brief send command to tm1638
- * @param pObj: device object of tm1638_Object_t.
- * @param cmd: @TM1638_Command.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ERROR if missing mandatory function
- */
- static int32_t TM1638_WriteCommand(tm1638_Object_t *pObj,uint8_t cmd)
- {
- pObj->IO.WriteSTB(0);
- pObj->IO.WriteReg(cmd);
- pObj->IO.WriteSTB(1);
- return TM1638_STATUS_OK;
- }
- /**
- * @brief Clear tm1638 display
- * @param pObj: device object of tm1638_Object_t.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ERROR if missing mandatory function
- */
- static void TM1638_ClearGRAM(tm1638_Object_t *pObj)
- {
- uint8_t i;
- TM1638_WriteCommand(pObj,TM1638_DATA_CMD_WRITE_GRAM_INC);
- pObj->IO.WriteSTB(0);
- pObj->IO.WriteReg(TM1638_ADDR_CMD_GRAM0);
- for(i=0;i<16;i++)
- {
- pObj->IO.WriteReg(0x00);
- }
- pObj->IO.WriteSTB(1);
- }
- /**
- * @brief Register IO functions to component object
- * @param pObj: device object of tm1638_Object_t.
- * @param ioctx: holds device IO functions.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ERROR if missing mandatory function
- */
- int32_t TM1638_RegisterBusIO(tm1638_Object_t *pObj, tm1638_IOCtx_t *ioctx)
- {
- if(!pObj || !ioctx->ReadReg || !ioctx->WriteReg || !ioctx->DelayUS || !ioctx->WriteSTB)
- {
- return TM1638_STATUS_ERROR;
- }
- pObj->IO.Init = ioctx->Init;
- pObj->IO.DeInit = ioctx->DeInit;
- pObj->IO.ReadReg = ioctx->ReadReg;
- pObj->IO.WriteReg = ioctx->WriteReg;
- pObj->IO.WriteSTB = ioctx->WriteSTB;
- pObj->IO.DelayUS = ioctx->DelayUS;
- return TM1638_STATUS_OK;
- }
- /**
- * @brief Initialize the TM1638 and configure the needed hardware resources
- * @param pObj: device object tm1638_Object_t.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ADDRESS_ERROR if cannot find device address
- * TM1638_STATUS_READ_ERROR if connot read register
- * TM1638_STATUS_WRITE_ERROR if connot write to register
- * TM1638_STATUS_RESET_TIMEOUT if cannot perform a software reset
- */
- int32_t TM1638_Init(tm1638_Object_t *pObj,uint8_t mode)
- {
- int32_t status = TM1638_STATUS_OK;
- if(pObj->Is_Initialized == 0)
- {
- if(pObj->IO.Init != 0)
- {
- /* GPIO and Clocks initialization */
- pObj->IO.Init();
- }
- TM1638_WriteCommand(pObj,mode);
- TM1638_ClearGRAM(pObj);
- if(status == TM1638_STATUS_OK)
- {
- pObj->IO.DelayUS(TM1638_INIT_TIMEOUT);
- pObj->Is_Initialized = 1;
- }
- }
- return status;
- }
- /**
- * @brief De-Initialize the TM1638 and it's hardware resources
- * @param pObj: device object tm1638_Object_t.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ERROR if missing mandatory function
- */
- int32_t TM1638_DeInit(tm1638_Object_t *pObj)
- {
- if(pObj->Is_Initialized)
- {
- if(pObj->IO.DeInit != 0)
- {
- if(pObj->IO.DeInit() < 0)
- {
- return TM1638_STATUS_ERROR;
- }
- }
- pObj->Is_Initialized = 0;
- }
- return TM1638_STATUS_OK;
- }
- /**
- * @brief Read key value of tm1638
- * @param pObj: device object of tm1638_Object_t.
- * @param pkey: pointer to key value.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ERROR if missing mandatory function
- */
- int32_t TM1638_ReadKey(tm1638_Object_t *pObj,uint8_t *pkey)
- {
- uint8_t tmp,i,ret=0;
- pObj->IO.WriteSTB(0);
- pObj->IO.WriteReg(TM1638_DATA_CMD_READ_KEY);
- pObj->IO.DelayUS(TM1638_READ_KEY_DELAY_NUS);
- for(i=0;i<4;i++){
- pObj->IO.ReadReg(&tmp);
- ret|=tmp<<i;
- }
- pObj->IO.WriteSTB(1);
- *pkey = ret;
- return TM1638_STATUS_OK;
- }
- /**
- * @brief Write display RAM in address fixed mode
- * @param pObj: device object of tm1638_Object_t.
- * @param add: GRAM address.
- * @param data: display data.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ERROR if missing mandatory function
- */
- int32_t TM1638_WriteGRAM_Fix(tm1638_Object_t *pObj,uint8_t add,uint8_t data)
- {
- TM1638_WriteCommand(pObj,TM1638_DATA_CMD_WRITE_GRAM_FIX);
- pObj->IO.WriteSTB(0);
- pObj->IO.WriteReg(TM1638_ADDR_CMD|add);
- pObj->IO.WriteReg(data);
- pObj->IO.WriteSTB(1);
- return TM1638_STATUS_OK;
- }
- /**
- * @brief Write display RAM in address increment mode
- * @param pObj: device object of tm1638_Object_t.
- * @param add: index of display address.
- * @param pdata: pointer display data array.
- * @param len: size of data array in byte.
- * @retval TM1638_STATUS_OK if OK
- * TM1638_STATUS_ERROR if missing mandatory function
- */
- int32_t TM1638_WriteGRAM_Inc(tm1638_Object_t *pObj,uint8_t add,uint8_t *pdata,uint8_t len)
- {
- uint8_t i;
- TM1638_WriteCommand(pObj,TM1638_DATA_CMD_WRITE_GRAM_INC);
- // pObj->IO.DelayUS(TM1638_READ_KEY_DELAY_NUS);
- pObj->IO.WriteSTB(0);
- pObj->IO.WriteReg(TM1638_ADDR_CMD|add);
- for(i=0;i<len;i++){
- pObj->IO.WriteReg(*pdata++);
- }
- pObj->IO.WriteSTB(1);
- return TM1638_STATUS_OK;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT sonshiqun1225@163.com *****END OF FILE****/
复制代码 3.示例
- #include "stm32h7xx_hal.h"
- #include "cmsis_os.h"
- #include "tm1638io_drv.h"
- #define TM_IO_DELAY_NUS 5
- #define TM_DIO_SET_IN() do{GPIOD->MODER &= ~(3<<(2*2));}while(0);
- #define TM_DIO_SET_OUT() do{GPIOD->MODER &= ~(3<<(2*2));GPIOD->MODER |= 1<<(2*2);}while(0);
- #define TM_STB_1 (GPIOG->BSRRL = GPIO_PIN_3)
- #define TM_STB_0 (GPIOG->BSRRH = GPIO_PIN_3)
- #define TM_CLK_1 (GPIOG->BSRRL = GPIO_PIN_2)
- #define TM_CLK_0 (GPIOG->BSRRH = GPIO_PIN_2)
- #define TM_DIO_1 (GPIOD->BSRRL = GPIO_PIN_2)
- #define TM_DIO_0 (GPIOD->BSRRH = GPIO_PIN_2)
- #define TM_DIO_IN ((GPIOD->IDR & GPIO_PIN_2)?1:0)
- //#define TM_STB_1 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_3,GPIO_PIN_SET)
- //#define TM_STB_0 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_3,GPIO_PIN_RESET)
- //#define TM_CLK_1 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_SET)
- //#define TM_CLK_0 HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_RESET)
- //#define TM_DIO_1 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET)
- //#define TM_DIO_0 HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET)
- //#define TM_DIO_IN (HAL_GPIO_ReadPin(GPIOD,GPIO_PIN_2)?1:0)
- int32_t TM1638_IO_Init(void)
- {
- TM_STB_1;
- TM_CLK_0;
- TM_DIO_SET_OUT();
- TM_DIO_0;
- return 0;
- }
- int32_t TM1638_IO_DeInit(void)
- {
- return 0;
- }
- int32_t TM1638_IO_DelayUS(uint16_t d)
- {
- // uint32_t i;
- // while(d--){
- // for(i=0;i<120;i++)
- // {
- // ;
- // }
- // HAL_Delay(1);
- // osDelay(1);
- // }
- osal_usleep(d);
- return 0;
- }
- int32_t TM1638_IO_ReadReg(uint8_t* p)
- {
- unsigned char i;
- unsigned char temp=0;
- TM_DIO_SET_IN();
- for(i=0;i<8;i++)
- {
- temp>>=1;
- TM_CLK_0;
- TM1638_IO_DelayUS(TM_IO_DELAY_NUS);
- TM_CLK_1;
- if(TM_DIO_IN)
- temp|=0x80;
- }
- TM_DIO_SET_OUT();
- *p = temp;
- return 0;
- }
- int32_t TM1638_IO_WriteReg(uint8_t d)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- TM_CLK_0;
- if(d&0x01)
- TM_DIO_1;
- else
- TM_DIO_0;
- d>>=1;
- TM_CLK_1;
- TM1638_IO_DelayUS(TM_IO_DELAY_NUS);
- }
- return 0;
- }
- int32_t TM1638_IO_WriteSTB(uint8_t s)
- {
- s ? TM_STB_1 : TM_STB_0;
- return 0;
- }
- tm1638_Object_t TM1638;
- tm1638_IOCtx_t TM1638_IOCtx = {
- .Init = TM1638_IO_Init,
- .DeInit = TM1638_IO_DeInit,
- .WriteReg = TM1638_IO_WriteReg,
- .ReadReg = TM1638_IO_ReadReg,
- .WriteSTB = TM1638_IO_WriteSTB,
- .DelayUS = TM1638_IO_DelayUS
- };
- int32_t OP_Init(void)
- {
- osMutexDef(myMutexOp);
- myMutexOpHandle = osMutexCreate(osMutex(myMutexOp));
- /* Set TM1638 IO functions */
- if(TM1638_RegisterBusIO(&TM1638, &TM1638_IOCtx)){
- return -1;
- }
- /* Initialize the TM1638 */
- if(TM1638_Init(&TM1638,TM1638_DISP_CMD_LV2)){
- return -2;
- }
- op.Is_Initialized = ManufacturerSpecificProfileArea[0].bSubCount;
-
- return 0;
- }
复制代码
|
|