STM32F103ZET6 [standard library function development] ----- TM1638 module drives 4-bit 8-segment common cathode digital tube

1. Environment introduction
Hardware: Punctuality Atomic Battleship Development Board, TM1638 Digital Tube Module, JLINK Downloader
Software: Keil uVision5

2. Brief introduction
of TM1638 module The figure below is the circuit schematic diagram of TM1638 module. The digital tube used is an 8-segment 4-digit common cathode digital tube.

Although the typical value of the logic power supply voltage written in the TM1638 specification is 5V, see the figure below. The minimum and maximum values ​​are not written, but for actual use, it can be used when connected to 3.3V, but the brightness is higher when connected to a 5V power supply, and the minimum high-level input voltage is 0.7VDD. If it is calculated according to 5V, 0.7VDD=3.5V, but in actual use, it is found that even if it is connected to 5V, the STM32 microcontroller can be used normally. It is well known that the power supply of STM32 is 2.0~3.6V. If you look at it according to the specification, it will not be considered high as long as it is lower than 3.5V. However, the actual verification has found that this is not the case. Pay attention to it when using it.

3. Line connection

STM32 TM1638 module
5V/3.3V VCC
GND GND
PC7 STB
PC9 CLK
PC8 GAVE

4. Timing diagram analysis

CLK is clock input,
DIO is data input and output when the rising edge is valid,
STB is chip selection when the rising edge is valid , and low level is valid

5. Instruction analysis

6. Code analysis
The following is the TM1638.hsum TM1638.cand the main.csource code

//TM1638.h
#ifndef	_TM1638_H
#define	_TM1638_H
#include "sys.h"
//TM1638模块引脚定义
#define STB PCout(7)
#define DIO PCout(8)
#define CLK PCout(9)	

#define DIO_INT GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8); 
#define	DISP_OFF	    0X00   //全灭
#define	DISP_ON	   		0Xff   //全亮
#define	DISP_COMMAND	0x80   //小数点
#define	ADDR_COMMAND	0XC0   //地址从0x00开始
#define	DATA_COMMAND	0X40   //写数据到显示寄存器,地址自动增加,普通模式 

void init_TM1638(void);//初始化
void Write_DATA(unsigned char add,unsigned char DATA);	//指定地址写入数据
unsigned char Read_key(void);
void Write_allLED(unsigned char LED_flag);
#endif
//TM1638.c
#include "TM1638.h"

void TM1638_Write(unsigned char	DATA)		//写数据函数
{
    
    
	unsigned char i;
	for(i=0;i<8;i++)
	{
    
    
		CLK=0;
		if(DATA&0X01)
			DIO=1;
		else
			DIO=0;
			DATA>>=1;
		CLK=1;
	}
}

void Write_COM(unsigned char cmd)		//发送命令字
{
    
    
	STB=0;
	TM1638_Write(cmd);
	STB=1;
}

void Write_DATA(unsigned char add,unsigned char DATA)		//指定地址写入数据
{
    
    
	Write_COM(0x40);
	STB=0;
	TM1638_Write(0xc0|add);
	TM1638_Write(DATA);
	STB=1;
}

//TM1638初始化函数
void init_TM1638(void)
{
    
    
	unsigned char i;
	GPIO_InitTypeDef  GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	 //使能PB,PE端口时钟

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;				 //LED0-->PB.5 端口配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
	GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB.5
	GPIO_SetBits(GPIOC,GPIO_Pin_7);							 //PC.7 输出高

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;	    		 //LED1-->PE.5 端口配置, 推挽输出
	GPIO_Init(GPIOC, &GPIO_InitStructure);	  				 //推挽输出 ,IO口速度为50MHz
	GPIO_SetBits(GPIOC,GPIO_Pin_8); 						 //PC.8 输出高 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;	    		 //LED1-->PE.5 端口配置, 推挽输出
	GPIO_Init(GPIOC, &GPIO_InitStructure);	  				 //推挽输出 ,IO口速度为50MHz
	GPIO_SetBits(GPIOC,GPIO_Pin_9); 						 //PC.9 输出高 

	Write_COM(0x8b);       //亮度 (0x88-0x8f)8级亮度可调
	Write_COM(0x40);       //写数据到显示寄存器,地址自动增加,普通模式
	STB=0;		           
	TM1638_Write(0xc0);    //设置起始地址为0x00
	for(i=0;i<16;i++)	   //传送16个字节的数据
	TM1638_Write(0x00);
	STB=1;
}
//main.h
#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "TM1638.h"
 
//共阴数码管显示代码
unsigned char tab[]={
    
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
                      0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};//0-f

unsigned char tab_del[]={
    
    0x00,0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
                    0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};//全灭,0-f
int main(void)
{
    
     
 	unsigned char i,j;
	delay_init();		  //初始化延时函数
	init_TM1638();	                           //初始化TM1638
	for(j=0;j<8;j++)
	{
    
    
		Write_DATA(0<<1,DISP_OFF);	               
		Write_DATA(1<<1,tab[1]);	    		 //显示数字1          
		Write_DATA(2<<1,tab[2]);	     		 //显示数字2              
	 	Write_DATA(3<<1,tab[3]+DISP_COMMAND);	 //显示数字3+小数点   
		Write_DATA(4<<1,tab[4]);	             //显示数字4         
		Write_DATA(5<<1,tab[5]);	             //显示数字5       
		Write_DATA(6<<1,tab[6]);	             //显示数字6         
		Write_DATA(7<<1,tab[7]);				 //显示数字7       
		delay_ms(800);		
	}
} 

Guess you like

Origin blog.csdn.net/wsq_666/article/details/114224342