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

一、原理图分享


二、源码分享

c 复制代码
/************************************************* 
* @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();
}
	
	
相关推荐
一 乐7 分钟前
高校教务|教务管理|基于springboot+vue的高校教务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·教务管理
数字冰雹8 分钟前
重塑城市公共安全管理的“智慧之眼”
java·大数据·数据库
黄毛火烧雪下17 分钟前
React Native (RN)项目在web、Android和IOS上运行
android·前端·react native
August_._26 分钟前
【MySQL】触发器、日志、锁机制 深度解析
java·大数据·数据库·人工智能·后端·mysql·青少年编程
Halo_tjn27 分钟前
基于 Object 类及包装类的专项实验
java·开发语言·计算机
百锦再27 分钟前
第10章 错误处理
java·git·ai·rust·go·错误·pathon
下位子30 分钟前
『OpenGL学习滤镜相机』- Day7: FBO(帧缓冲对象)
android·opengl
拾忆,想起35 分钟前
超时重传 vs 快速重传:TCP双保险如何拯救网络丢包?
java·开发语言·网络·数据库·网络协议·tcp/ip·php
從南走到北37 分钟前
JAVA国际版同城外卖跑腿团购到店跑腿多合一APP系统源码支持Android+IOS+H5
android·java·ios·微信小程序·小程序
空白格9738 分钟前
组件化攻略
android