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

news/2025/2/15 3:31:38/

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

/************************************************* 
* @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);elsert_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;elseshowBuf[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;elsebreak;}}
/************************************************* 
* @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.ppmy.cn/news/1572134.html

相关文章

【Vue】打包vue3+vite项目发布到github page的完整过程

文章目录 第一步&#xff1a;打包第二步&#xff1a;github仓库设置第三步&#xff1a;安装插件gh-pages第四步&#xff1a;两个配置第五步&#xff1a;上传github其他问题1. 路由2.待补充 参考文章&#xff1a; 环境&#xff1a; vue3vite windows11&#xff08;使用终端即可&…

#渗透测试#批量漏洞挖掘#Crocus系统—Download 文件读取

免责声明 本教程仅为合法的教学目的而准备&#xff0c;严禁用于任何形式的违法犯罪活动及其他商业行为&#xff0c;在使用本教程前&#xff0c;您应确保该行为符合当地的法律法规&#xff0c;继续阅读即表示您需自行承担所有操作的后果&#xff0c;如有异议&#xff0c;请立即停…

计算机网络综合实训室解决方案(2025年最新版)

一、计算机网络综合实训室概述 数字化转型的关键在于计算机网络技术的支撑&#xff0c;因此&#xff0c;当前教育教学的一大热点及社会需求&#xff0c;在于培养能够全面设计计算机整体系统、实施综合布线、安装网络设备并进行调试与维护的专业人才。鉴于计算机网络技术的高度…

【推荐】碰一碰发视频源码搭建,支持OEM

引言&#xff1a;重新定义视频分享体验 在移动优先的互联网时代&#xff0c;"碰一碰"交互已成为设备间快速连接的代名词。本文将突破传统文件传输思维&#xff0c;结合Web NFC与WebRTC技术&#xff0c;实现无需后端服务器的端到端视频实时传输方案。通过纯前端技术栈…

从零开始设计一个完整的网站:HTML、CSS、PHP、MySQL 和 JavaScript 实战教程

前言 本文将从实战角度出发&#xff0c;带你一步步设计一个完整的网站。我们将从 静态网页 开始&#xff0c;然后加入 动态功能&#xff08;使用 PHP&#xff09;&#xff0c;连接 数据库&#xff0c;最后加入 JavaScript 实现交互功能。通过这个教程&#xff0c;你将掌握一个…

Linux ln创建、删除软链接的正确方式

在 Linux 系统中&#xff0c;ln 命令用于创建链接文件&#xff0c;链接分为软链接&#xff08;符号链接&#xff09;和硬链接。以下详细介绍创建和删除软链接的正确方式&#xff1a; 创建软链接 基本语法 ln -s [源文件或目录] [目标软链接文件或目录] -s 选项&#xff1a;…

解决 DeepSeek 官网服务器繁忙的实用方案

解决 DeepSeek 官网服务器繁忙的实用方案 大家在使用 DeepSeek 时&#xff0c;是不是经常遇到官网服务器繁忙&#xff0c;等半天都加载不出来的情况&#xff1f;别担心&#xff0c;今天就给大家分享一个用 DeepSeek 硅基流动 Cherry Studio 解决这个问题的实用方案&#xff…

uake 网络安全 reverse网络安全

首先从PEID的算法分析插件来介绍&#xff0c;要知道不管是在CTF竞赛的REVERSE题目中&#xff0c;还是在实际的商业产品中&#xff0c;很多程序都喜欢使用成熟的标准算法来作为注册算法的一个部分&#xff0c;如MD5、Blowfish等。这些算法本身往往就十分复杂和难以你理解&#x…