STM32、GD32驱动TM1640原理图、源码分享

发布于:2025-02-13 ⋅ 阅读:(15) ⋅ 点赞:(0)

一、原理图分享
在这里插入图片描述
在这里插入图片描述
二、源码分享

/************************************************* 
* @copyright: 
* @author:Xupeng
* @date:2024-07-18
* @description: 
**************************************************/  
#include "smg.h"


#define DBG_TAG    "smg"
#define DBG_LVL    DBG_LOG
#include <rtdbg.h>

static const int sckPin = GET_PIN(D,2);
static const int sdaPin = GET_PIN(D,3);

/************************************************* 
* @function:static int tm1640_init()      
* @description: tm1640初始化  
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
static int tm1640_init()
{
	rt_pin_mode(sckPin,PIN_MODE_OUTPUT);
	rt_pin_mode(sdaPin,PIN_MODE_OUTPUT);
	
	rt_pin_write(sckPin,PIN_HIGH);
	rt_pin_write(sdaPin,PIN_HIGH);
	return 0;
}

INIT_BOARD_EXPORT(tm1640_init);
/************************************************* 
* @function:static int tm1640_start()      
* @description: tm1640启动
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
static void tm1640_start()
{
	rt_pin_write(sckPin,PIN_HIGH);
	rt_pin_write(sdaPin,PIN_HIGH);
	rt_hw_us_delay(5);
	rt_pin_write(sdaPin,PIN_LOW);
	rt_hw_us_delay(5);
	rt_pin_write(sckPin,PIN_LOW);
}
/************************************************* 
* @function:static int tm1640_stop()      
* @description: tm1640停止
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
static void tm1640_stop()
{
   rt_pin_write(sdaPin,PIN_LOW);
   rt_pin_write(sckPin,PIN_HIGH);
   rt_hw_us_delay(5);
   rt_pin_write(sdaPin,PIN_HIGH);
   rt_hw_us_delay(5);
   rt_pin_write(sckPin,PIN_HIGH);

}
/************************************************* 
* @function:static void tm1640_write_byte(uint8_t data)   
* @description: tm1640发送字节
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
static void tm1640_write_byte(uint8_t data)
{
	uint8_t i;
	for(i=0;i<8;i++)
	{
		rt_pin_write(sckPin,PIN_LOW);
		if(data & 0x01)
			rt_pin_write(sdaPin,PIN_HIGH);
		else
			rt_pin_write(sdaPin,PIN_LOW);
		data>>=1;
		rt_hw_us_delay(5);
		rt_pin_write(sckPin,PIN_HIGH);
	}	
}


static const uint8_t smgCode[]={//显示段码 数码管字跟 
    0x3F,  //[0]  '0'
    0x06,  //[1]  '1'
    0x5B,  //[2]  '2'
    0x4F,  //[3]  '3'
    0x66,  //[4]  '4'
    0x6D,  //[5]  '5'
    0x7D,  //[6]  '6'
    0x07,  //[7]  '7'
    0x7F,  //[8]  '8'
    0x6F,  //[9]  '9'
    0x77,  //[10]  'A'
    0x7C,  //[11]  'b'
    0x58,  //[12]  'c'
    0x39,  //[13]  'C'
    0x5E,  //[14]  'd'
    0x79,  //[15]  'E'
    0x71,  //[16]  'F'
    0x3D,  //[17]  'G'
    0x74,  //[18]  'h'
    0x76,  //[19]  'H'
    0x0E,  //[20]  'J'
    0x38,  //[21]  'L'
    0x54,  //[22]  'n'
    0x37,  //[23]  'N'
    0x5C,  //[24]  'o'
    0x73,  //[25]  'P'
    0x67,  //[26]  'q'
    0x67,  //[27]  'R'
    0x50,  //[28]  'r'
    0x3E,  //[29]  'u'
    0x1C,  //[30]  'v'
    0x6E,  //[31]  'y'
    0x40,  //[32]  '-'
	0x00,
};

static uint8_t showBuf[16] = {0};
/************************************************* 
* @function:void smg_show_value(float value) 
* @description: 数码管显示值
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
void smg_show_value(float value)
{
	if( weight > 99999 || weight < -99999)
	{
		for(uint8_t i=0;i<8;i++)
			showBuf[i] = 0x40;
		return;
	}
	
	
	int32_t w = weight*100;//保留两位小数
	
	//显示符号
	if(w > 0)
		showBuf[0] = 0;
	else
		showBuf[0] = 0x40;
	
	w = abs(w);
	
	showBuf[7] = smgCode[w%10];
	showBuf[6] = smgCode[w/10%10];
	showBuf[5] = smgCode[w/100%10] | 0x80;
	showBuf[4] = smgCode[w/1000%10];
	showBuf[3] = smgCode[w/10000%10];
	showBuf[2] = smgCode[w/100000%10];
	showBuf[1] = smgCode[w/1000000%10];
	
	//取消前面0的显示
	for(uint8_t i=1;i<6;i++)
	{
		if(showBuf[i] == smgCode[0])
			showBuf[i] = 0x00;
		else
			break;
	}
	
}
/************************************************* 
* @function:void smg_send_data(bool on,uint8_t brightness)   
* @description: 数码管发送数据
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
void smg_send_data(bool on,uint8_t brightness)
{
	tm1640_start();
	tm1640_write_byte(0x40);				//设置数据命令
	tm1640_stop();
	tm1640_start();
	tm1640_write_byte(0xc0);				//设置显示地址
	for(uint8_t i=0;i<sizeof(showBuf);i++)
	{
		tm1640_write_byte(showBuf[i]);
	}
	tm1640_stop();
	tm1640_start();
	tm1640_write_byte(0x80 | on<<3 | brightness);		//设置显示控制命令
	tm1640_stop();
}