tm1620与TM1640和TM1650不同,有STB引脚
部分代码搬运
tm1620.c
#include "tm1620.h" static void SetSTB1(GPIO_PinState state) { HAL_GPIO_WritePin(STB1_GPIO_Port, STB1_Pin, state); } static void SetCLK1(GPIO_PinState state) { HAL_GPIO_WritePin(CLK1_GPIO_Port, CLK1_Pin, state); } static void SetDIN1(GPIO_PinState state) { HAL_GPIO_WritePin(DIN1_GPIO_Port, DIN1_Pin, state); } static void SetSTB2(GPIO_PinState state) { HAL_GPIO_WritePin(STB2_GPIO_Port, STB2_Pin, state); } static void SetCLK2(GPIO_PinState state) { HAL_GPIO_WritePin(CLK2_GPIO_Port, CLK2_Pin, state); } static void SetDIN2(GPIO_PinState state) { HAL_GPIO_WritePin(DIN2_GPIO_Port, DIN2_Pin, state); } static void SetSTB3(GPIO_PinState state) { HAL_GPIO_WritePin(STB3_GPIO_Port, STB3_Pin, state); } static void SetCLK3(GPIO_PinState state) { HAL_GPIO_WritePin(CLK3_GPIO_Port, CLK3_Pin, state); } static void SetDIN3(GPIO_PinState state) { HAL_GPIO_WritePin(DIN3_GPIO_Port, DIN3_Pin, state); } /* Private variables----------------------------------------------------------*/ uint8_t Disp_SEG[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //数码管显示0 ~ 9 uint8_t DisplayAddress[5] = {0xC8,0xC6,0xC4,0xC2, 0xC0}; uint8_t Disp_SEG_dp[10] = {0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xDF}; //数码管显示0. ~ 9. static void TM1620_Write_Byte(TM1620_DEVICE_T device, uint8_t dat); static void TM1620_Init(TM1620_DEVICE_T device); static void Disp_Num(TM1620_DEVICE_T device, uint8_t num, long dat); static void Disp_FloatNum(TM1620_DEVICE_T device, uint8_t num, float dat); /* Public variables-----------------------------------------------------------*/ TM1620_DEVICE_T TM1620_Device1 = {SetSTB1,SetCLK1,SetDIN1,Brightness_level_3}; TM1620_DEVICE_T TM1620_Device2 = {SetSTB2,SetCLK2,SetDIN2,Brightness_level_3}; TM1620_DEVICE_T TM1620_Device3 = {SetSTB3,SetCLK3,SetDIN3,Brightness_level_3}; TM1620_t TM1620_1 = { &TM1620_Device1, TM1620_Init, Disp_Num, Disp_FloatNum }; TM1620_t TM1620_2 = { &TM1620_Device2, TM1620_Init, Disp_Num, Disp_FloatNum }; TM1620_t TM1620_3 = { &TM1620_Device3, TM1620_Init, Disp_Num, Disp_FloatNum }; /* Private function prototypes------------------------------------------------*/ /* * @name TM1620_Write_Byte * @brief TM1620写入一个字节 * @param dat:待写入数据 * @retval None */ static void TM1620_Write_Byte(TM1620_DEVICE_T device, uint8_t dat) { uint8_t i = 0; device.SetSTBIo(GPIO_PIN_RESET); HAL_Delay(1); //需要加延时等待,不然数码管显示会出异常 for(i=0;i<8;i++) { //CLK为低电平时往DIN上放数据 device.SetCLKIo(GPIO_PIN_RESET); if(0x01 & dat){ //先取出最低位进行发送,然后dat右移,把最低位移出去,再取补上地低位进行发送 device.SetDINIo(GPIO_PIN_SET); }else{ device.SetDINIo(GPIO_PIN_RESET); } __nop(); //延时1us dat >>= 1; //CLK为高电平时TM1620读取DIN上的数据 device.SetCLKIo(GPIO_PIN_SET); __nop(); //延时1us } } /* * @name TM1620_Init * @brief TM1620初始化 * @param None * @retval None */ static void TM1620_Init(TM1620_DEVICE_T device) { uint8_t i = 0; // //设置显示模式:6位8段 // TM1620_Write_Byte(device, Disp_Mode_GRID6_SEG8); // device.SetSTBIo(GPIO_PIN_SET); // //设置地址模式:自动地址增加 // TM1620_Write_Byte(device, Write_Data_Addr_Auto_Add); // device.SetSTBIo(GPIO_PIN_SET); // //清除显示寄存器 // TM1620_Write_Byte(device, Disp_SFR_Addr_00H); // for(i=0;i<Disp_SFR_Addr_Num;i++) // { // TM1620_Write_Byte(device, 0x00); // } // device.SetSTBIo(GPIO_PIN_SET); // //显示默认辉度:level3 // TM1620_Write_Byte(device, device.Brightness); // device.SetSTBIo(GPIO_PIN_SET); //设置显示模式:6位8段 TM1620_Write_Byte(device, Disp_Mode_GRID6_SEG8); device.SetSTBIo(GPIO_PIN_SET); //设置地址模式:固定地址 TM1620_Write_Byte(device, Write_Data_Addr_Fix); device.SetSTBIo(GPIO_PIN_SET); //写显示寄存器 //先设置位选,设置为左边的数码管,再写入段选的数据0xFF,数码管8个段全亮 for(i=0;i<5;i++) { TM1620_Write_Byte(device, DisplayAddress[i]); TM1620_Write_Byte(device, 0x3F); device.SetSTBIo(GPIO_PIN_SET); } //显示辉度 TM1620_Write_Byte(device, device.Brightness); device.SetSTBIo(GPIO_PIN_SET); } /* * @name Disp_Num * @brief 数码管显示数据 * @param None * @retval None */ static void Disp_Num(TM1620_DEVICE_T device, uint8_t num, long dat) { int i; uint8_t temp = 0; for(i = 0; i < num; i++){ temp = dat % 10; TM1620_Write_Byte(device, DisplayAddress[i]); TM1620_Write_Byte(device, Disp_SEG[temp]); device.SetSTBIo(GPIO_PIN_SET); dat /= 10; } //显示辉度 TM1620_Write_Byte(device, device.Brightness); device.SetSTBIo(GPIO_PIN_SET); } static void Disp_FloatNum(TM1620_DEVICE_T device, uint8_t num, float dat) { int i; uint8_t temp = 0; int intNum; intNum = (int)(dat*10); for(i = 0; i < num; i++){ temp = intNum % 10; TM1620_Write_Byte(device, DisplayAddress[i]); if(i == 1) TM1620_Write_Byte(device, Disp_SEG_dp[temp]); else TM1620_Write_Byte(device, Disp_SEG[temp]); device.SetSTBIo(GPIO_PIN_SET); intNum /= 10; } //显示辉度 TM1620_Write_Byte(device, device.Brightness); device.SetSTBIo(GPIO_PIN_SET); }
tm1620.h
#ifndef _TM1620_H_ #define _TM1620_H_ #include "user_main.h" //显示模式枚举类型 typedef enum { Disp_Mode_GRID4_SEG10 = 0x00, //4位10段 Disp_Mode_GRID5_SEG9 = 0x01, //5位9段 Disp_Mode_GRID6_SEG8 = 0x02 //6位8段 }Disp_Mode_t; //写数据地址模式枚举类型 typedef enum { Write_Data_Addr_Auto_Add = 0x40, //自动地址增加 Write_Data_Addr_Fix = 0x44 //固定地址 }Write_Data_Addr_Mode_t; //辉度等级枚举类型 typedef enum { Brightness_level_0 = 0x80, //显示关 Brightness_level_1 = 0x88, //脉冲宽度为1/16 Brightness_level_2 = 0x89, //脉冲宽度为2/16 Brightness_level_3 = 0x8A, //脉冲宽度为4/16 Brightness_level_4 = 0x8B, //脉冲宽度为10/16 Brightness_level_5 = 0x8C, //脉冲宽度为11/16 Brightness_level_6 = 0x8D, //脉冲宽度为12/16 Brightness_level_7 = 0x8E, //脉冲宽度为13/16 Brightness_level_8 = 0x8F, //脉冲宽度为14/16 }Brightness_level_t; //显示寄存器地址枚举类型 —> 位选GRID的地址 typedef enum { Disp_SFR_Addr_Num = (uint8_t)12, Disp_SFR_Addr_00H = 0xC0, Disp_SFR_Addr_01H = 0xC1, Disp_SFR_Addr_02H = 0xC2, Disp_SFR_Addr_03H = 0xC3, Disp_SFR_Addr_04H = 0xC4, Disp_SFR_Addr_05H = 0xC5, Disp_SFR_Addr_06H = 0xC6, Disp_SFR_Addr_07H = 0xC7, Disp_SFR_Addr_08H = 0xC8, Disp_SFR_Addr_09H = 0xC9, Disp_SFR_Addr_0AH = 0xCA, Disp_SFR_Addr_0BH = 0xCB, }Disp_SFR_Addr_t; typedef void (*SetIoState)(GPIO_PinState state); //定义结构体类型 typedef struct { SetIoState SetSTBIo; SetIoState SetCLKIo; SetIoState SetDINIo; Brightness_level_t Brightness; //辉度变量 }TM1620_DEVICE_T; typedef struct { TM1620_DEVICE_T *device; void (*TM1620_Init)(TM1620_DEVICE_T device); //TM1620初始化 void (*Disp_Num)(TM1620_DEVICE_T device, uint8_t num, long dat); //数码管显示 void (*Disp_FloatNum)(TM1620_DEVICE_T device, uint8_t num, float dat); }TM1620_t; /* extern variables-----------------------------------------------------------*/ extern TM1620_t TM1620_1; extern TM1620_t TM1620_2; extern TM1620_t TM1620_3; /* extern function prototypes-------------------------------------------------*/ #endif
显示
TM1620_1.TM1620_Init(*(TM1620_1.device)); TM1620_2.TM1620_Init(*(TM1620_2.device)); TM1620_3.TM1620_Init(*(TM1620_3.device)); TM1620_1.Disp_Num(*(TM1620_1.device), 5, 12345); TM1620_2.Disp_FloatNum(*(TM1620_2.device), 5, 1234.5); TM1620_3.Disp_Num(*(TM1620_3.device), 5, 54321);
标签:Disp,void,TM1620,state,device,GPIO From: https://www.cnblogs.com/yianyan/p/17916930.html