STM32 development (7) STM32F103 display - detailed explanation of digital tube display


1. Basic knowledge points

Learn about the TM1620 chip manual . This experiment is based on the development of STM32F103 to realize TM1620 digital tube display through GPIO analog timing.

Are you ready? Start my show time.


2. Development environment

1. Hardware development preparation
Main control: STM32F103ZET6
control digital tube chip: TM1620
insert image description here
2. Software development preparation
Use virtual machine + VScode + STM32Cube to develop STM32 for software development, and compile and download directly in the virtual machine.
This part can refer to: Software Development Environment Construction


3. STM32CubeMX related configuration

1. Basic configuration of STM32CubeMX
This experiment is developed based on the basic framework of CubeMX detailed explanation .

2. TM1620 controller signal pin configuration (GPIO configuration)
insert image description here
insert image description here
insert image description here


Four, Vscode code explanation

1. Definition and initialization of digital tube related structures

//定义结构体类型
typedef struct
{
    
    
    Brightness_Mode_t  Brightness;
    
    void (*TM1620_Init)(void);                              //TM1620初始化
    void (*Disp)(Disp_NUM_t,uint8_t,Disp_DP_Status_t);      //数码管显示特定数组内容
    void (*Disp_Other)(Disp_NUM_t,uint8_t,Disp_DP_Status_t);// 数码管自定义内容显示
} Display_t;

Display_t Display ={
    
    
  Brightness_Mode_Level3,    // 显示亮度等级

  TM1620_Init,               // 初始化,下面有具体实现        
  Disp,                      // 数码管根据特定数组内容显示,下面有具体实现
  Disp_Other  				// 数码管自定义内容显示,下面有具体实现
};

2. Use the macro definition to represent the high and low levels of each pin

#define TM1620_STB_SET   HAL_GPIO_WritePin(GPIOC, TM1620_STB_Pin, GPIO_PIN_SET);
#define TM1620_STB_RESET HAL_GPIO_WritePin(GPIOC, TM1620_STB_Pin, GPIO_PIN_RESET);

#define TM1620_CLK_SET   HAL_GPIO_WritePin(GPIOC, TM1620_CLK_Pin, GPIO_PIN_SET);
#define TM1620_CLK_RESET HAL_GPIO_WritePin(GPIOC, TM1620_CLK_Pin, GPIO_PIN_RESET);

#define TM1620_DIN_SET   HAL_GPIO_WritePin(GPIOC, TM1620_DIN_Pin, GPIO_PIN_SET);
#define TM1620_DIN_RESET HAL_GPIO_WritePin(GPIOC, TM1620_DIN_Pin, GPIO_PIN_RESET);

3. According to the chip write timing, use GPIO simulation

//TM1620写一个数据
static void TM1620_write_date(uint8_t date)
{
    
    
  uint8_t i = 0;

  TM1620_STB_RESET;                // 使能脚拉低

  for(i=0; i<8; i++){
    
                  // 循环发送数据位
    TM1620_CLK_RESET;                // 时钟拉低

    if((date & 0x01) == 0x01){
    
          // 取出最低位
      TM1620_DIN_SET;
    }              // 数据位拉高  
    else
      TM1620_DIN_RESET;            // 数据位拉低 

    date = date>>1;                // 移位,准备下一个发送的数据
    TM1620_CLK_SET;                // 时钟拉高,发送数据
  }
}

4. Initialize the default state of the digital tube.
The command to set the data during the initialization process is Disp_Addr_Mode_Auto, which uses the address auto-increment mode. The timing is based on the chip manual to realize the continuous display of the digital tube.

//TM1620初始化 地址增加模式   
static void TM1620_Init(void)      
{
    
    
  TM1620_write_date(Disp_Mode_GRID6_SGE8);            // 设置显示模式
  TM1620_STB_SET;

  TM1620_write_date(Disp_Addr_Mode_Auto);            // 设置数据命令
  TM1620_STB_SET;

  TM1620_write_date(Disp_Register_00H);            // 设置显示地址

  for(int i=0; i<Disp_Register_NUM; i++){
    
    
    TM1620_write_date(0x00);                        // Date
  }
  TM1620_STB_SET;

  TM1620_write_date(Display.Brightness);            // 设置控制命令
  TM1620_STB_SET;
}  

5. Make a fixed address display digital tube interface

uint8_t Date[16]    = {
    
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};  //数码管译码 0 - 9

static void Disp(Disp_NUM_t disp_num, uint8_t disp_value, Disp_DP_Status_t dp_status)  
{
    
    
  //参数范围检查
  if(disp_value > 0x0F)
  {
    
    
    System.Assert_Failed();
  }

  TM1620_write_date(Disp_Mode_GRID6_SGE8);            // 设置显示模式
  TM1620_STB_SET;

  TM1620_write_date(Disp_Addr_Mode_Fix);            // 设置数据命令
  TM1620_STB_SET;

  TM1620_write_date(disp_num);                       // 设置显示地址
  if(dp_status == Disp_DP_ON)
    TM1620_write_date(Date[disp_value]+0x80);
  else
    TM1620_write_date(Date[disp_value]);                     // Date
  TM1620_STB_SET;

  TM1620_write_date(Display.Brightness);            // 设置控制命令
  TM1620_STB_SET;
}

6. The digital tube displays custom content

static void Disp_Other(Disp_NUM_t Disp_NUM,uint8_t Dat,Disp_DP_Status_t Disp_DP_Status) 
{
    
    
	//设置显示模式
	TM1620_write_date(Disp_Mode_GRID6_SGE8); 
	TM1620_STB_SET;
	
	//地址固定
	TM1620_write_date(Disp_Addr_Mode_Fix); 
	TM1620_STB_SET;
	
	//写地址
	TM1620_write_date(Disp_NUM);
	//写数据
	if(Disp_DP_Status == Disp_DP_ON)
		TM1620_write_date(Dat + 0x80);
	else
		TM1620_write_date(Dat);
	TM1620_STB_SET;
	
	//显示
  TM1620_write_date(Display.Brightness);
	TM1620_STB_SET; 
}


5. Results demonstration

Control the digital tube display by calling the Display.Disp interface.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43564241/article/details/129410421