当前位置: 首页 > article >正文

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

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

/************************************************* 
* @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();
}
	
	




http://www.kler.cn/a/542518.html

相关文章:

  • 7.推荐系统的评价与优化
  • JavaEE-前端与后台的搭建
  • Matlab机械手碰撞检测应用
  • 线上HBase client返回超时异常分析 HBase callTimeout=60000
  • 大语言模型需要的可观测性数据的关联方式
  • C语言预处理艺术:编译前的魔法之旅
  • 新数据结构(4)——Java继承
  • Python实现GO鹅优化算法优化支持向量机SVM分类模型项目实战
  • 港中文腾讯提出可穿戴3D资产生成方法BAG,可自动生成服装和配饰等3D资产如,并适应特定的人体模型。
  • Java 读取 PDF 模板文档并替换内容重新生成 PDF
  • CES Asia 2025:科技盛宴助力中国数字经济腾飞
  • 中间件-安装Minio-集成使用(ubantu-docker)
  • Vue项目--动画效果的改变
  • Swift的方法派发机制
  • 模块化的基本概念
  • docker 安装 Prometheus、Node Exporter 和 Grafana
  • 【如何掌握CSP-J 信奥赛中的排序算法】
  • oracle执行grant授权sql被阻塞问题处理
  • 【PromptCoder + Bolt.new】自动生成页面和路由——提升开发效率的利器
  • 简述C#多线程
  • Zookeeper 作注册中心 和nacos 和eruka 有什么差异 ?基于什么理论选择?
  • 第七节 文件与流
  • spring cloud 使用 webSocket
  • SpringCloud - Gateway 网关
  • 常用电路(过压保护、电流/电压采集)
  • 开源AI智能名片2+1链动模式S2B2C商城小程序在实体店与线上营销中的应用探索