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();
}
	
	
相关推荐
孤廖12 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
我命由我1234512 小时前
Android 对话框 - 对话框全屏显示(设置 Window 属性、使用自定义样式、继承 DialogFragment 实现、继承 Dialog 实现)
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
Full Stack Developme12 小时前
java.net 包详解
java·python·.net
一叶飘零_sweeeet12 小时前
深入 Spring 内核:解密 15 种设计模式的实战应用与底层实现
java·spring·设计模式
凤山老林12 小时前
排序算法:详解插入排序
java·开发语言·后端·算法·排序算法
兆龙电子单片机设计12 小时前
【STM32项目开源】STM32单片机智能农业大棚控制系统
stm32·单片机·物联网·开源·自动化
彦楠12 小时前
IDEA实用快捷键
java·ide·intellij-idea
豆沙沙包?12 小时前
2025年--Lc197-077. 排序链表(链表,尾插法)--Java版
java·数据结构·链表
m0_6515939112 小时前
深入理解软件设计中的协议与规范:从理论到Java实践
java·软件工程·代码规范·设计规范
Knight_AL13 小时前
Tomcat 类加载器隔离机制的实际应用
java·tomcat