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

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);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.lryc.cn/news/537605.html

相关文章:

  • spring boot 对接aws 的S3 服务,实现上传和查询
  • PH热榜 | 2025-02-12
  • 通过例子学 rust 个人精简版 1-1
  • HTTP的前世今生:如何塑造现代互联网的交互方式?
  • Flutter_学习记录_动画的简单了解
  • 【java】for (int num : numbers) { System.out.print(num + “ “); } for里的是什么意思
  • 内容中台驱动企业CMS架构优化与高效策略
  • 我用 Cursor 开发了一款个人小记系统
  • 百问网(100ask)提供的烧写工具的原理和详解;将自己编译生成的u-boot镜像文件烧写到eMMC中
  • doris:异步物化视图概述
  • 图像缩放的双线性插值实现方式
  • 深入剖析 Vue 的响应式原理:构建高效 Web 应用的基石
  • 40.日常算法
  • CAS单点登录(第7版)11.SSO SLO
  • Bob the Canadian
  • CAS单点登录(第7版)16.模仿
  • 预留:大数据Hadoop之——部署hadoop+hive+Mysql环境(Linux)
  • RabbitMQ介绍以及基本使用
  • C++演示中介模式
  • Vue的简单入门 一
  • 【免费送书活动】《MySQL 9从入门到性能优化(视频教学版)》
  • export default与export区别
  • 最佳的出牌方法
  • Kotlin 2.1.0 入门教程(二十一)数据类
  • 30天开发操作系统 第 20 天 -- API
  • WEB安全--SQL注入--floor报错注入
  • 【java面向对象的三大特性】封装、继承和多态
  • Hermite 插值
  • 【推理llm论文精度】DeepSeek-R1:强化学习驱动LLM推理能力飞跃
  • arm linux下的中断处理过程。