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

打印 上一主题 下一主题

主题 1047|帖子 1047|积分 3141

一、原理图分享


二、源码分享
  1. /*************************************************
  2. * @copyright:
  3. * @author:Xupeng
  4. * @date:2024-07-18
  5. * @description:
  6. **************************************************/  
  7. #include "smg.h"
  8. #define DBG_TAG    "smg"
  9. #define DBG_LVL    DBG_LOG
  10. #include <rtdbg.h>
  11. static const int sckPin = GET_PIN(D,2);
  12. static const int sdaPin = GET_PIN(D,3);
  13. /*************************************************
  14. * @function:static int tm1640_init()      
  15. * @description: tm1640初始化  
  16. * @calls:         
  17. * @input:                    
  18. * @return:      
  19. * @others:      
  20. *************************************************/
  21. static int tm1640_init()
  22. {
  23.         rt_pin_mode(sckPin,PIN_MODE_OUTPUT);
  24.         rt_pin_mode(sdaPin,PIN_MODE_OUTPUT);
  25.        
  26.         rt_pin_write(sckPin,PIN_HIGH);
  27.         rt_pin_write(sdaPin,PIN_HIGH);
  28.         return 0;
  29. }
  30. INIT_BOARD_EXPORT(tm1640_init);
  31. /*************************************************
  32. * @function:static int tm1640_start()      
  33. * @description: tm1640启动
  34. * @calls:         
  35. * @input:                    
  36. * @return:      
  37. * @others:      
  38. *************************************************/
  39. static void tm1640_start()
  40. {
  41.         rt_pin_write(sckPin,PIN_HIGH);
  42.         rt_pin_write(sdaPin,PIN_HIGH);
  43.         rt_hw_us_delay(5);
  44.         rt_pin_write(sdaPin,PIN_LOW);
  45.         rt_hw_us_delay(5);
  46.         rt_pin_write(sckPin,PIN_LOW);
  47. }
  48. /*************************************************
  49. * @function:static int tm1640_stop()      
  50. * @description: tm1640停止
  51. * @calls:         
  52. * @input:                    
  53. * @return:      
  54. * @others:      
  55. *************************************************/
  56. static void tm1640_stop()
  57. {
  58.    rt_pin_write(sdaPin,PIN_LOW);
  59.    rt_pin_write(sckPin,PIN_HIGH);
  60.    rt_hw_us_delay(5);
  61.    rt_pin_write(sdaPin,PIN_HIGH);
  62.    rt_hw_us_delay(5);
  63.    rt_pin_write(sckPin,PIN_HIGH);
  64. }
  65. /*************************************************
  66. * @function:static void tm1640_write_byte(uint8_t data)   
  67. * @description: tm1640发送字节
  68. * @calls:         
  69. * @input:                    
  70. * @return:      
  71. * @others:      
  72. *************************************************/
  73. static void tm1640_write_byte(uint8_t data)
  74. {
  75.         uint8_t i;
  76.         for(i=0;i<8;i++)
  77.         {
  78.                 rt_pin_write(sckPin,PIN_LOW);
  79.                 if(data & 0x01)
  80.                         rt_pin_write(sdaPin,PIN_HIGH);
  81.                 else
  82.                         rt_pin_write(sdaPin,PIN_LOW);
  83.                 data>>=1;
  84.                 rt_hw_us_delay(5);
  85.                 rt_pin_write(sckPin,PIN_HIGH);
  86.         }       
  87. }
  88. static const uint8_t smgCode[]={//显示段码 数码管字跟
  89.     0x3F,  //[0]  '0'
  90.     0x06,  //[1]  '1'
  91.     0x5B,  //[2]  '2'
  92.     0x4F,  //[3]  '3'
  93.     0x66,  //[4]  '4'
  94.     0x6D,  //[5]  '5'
  95.     0x7D,  //[6]  '6'
  96.     0x07,  //[7]  '7'
  97.     0x7F,  //[8]  '8'
  98.     0x6F,  //[9]  '9'
  99.     0x77,  //[10]  'A'
  100.     0x7C,  //[11]  'b'
  101.     0x58,  //[12]  'c'
  102.     0x39,  //[13]  'C'
  103.     0x5E,  //[14]  'd'
  104.     0x79,  //[15]  'E'
  105.     0x71,  //[16]  'F'
  106.     0x3D,  //[17]  'G'
  107.     0x74,  //[18]  'h'
  108.     0x76,  //[19]  'H'
  109.     0x0E,  //[20]  'J'
  110.     0x38,  //[21]  'L'
  111.     0x54,  //[22]  'n'
  112.     0x37,  //[23]  'N'
  113.     0x5C,  //[24]  'o'
  114.     0x73,  //[25]  'P'
  115.     0x67,  //[26]  'q'
  116.     0x67,  //[27]  'R'
  117.     0x50,  //[28]  'r'
  118.     0x3E,  //[29]  'u'
  119.     0x1C,  //[30]  'v'
  120.     0x6E,  //[31]  'y'
  121.     0x40,  //[32]  '-'
  122.         0x00,
  123. };
  124. static uint8_t showBuf[16] = {0};
  125. /*************************************************
  126. * @function:void smg_show_value(float value)
  127. * @description: 数码管显示值
  128. * @calls:         
  129. * @input:                    
  130. * @return:      
  131. * @others:      
  132. *************************************************/
  133. void smg_show_value(float value)
  134. {
  135.         if( weight > 99999 || weight < -99999)
  136.         {
  137.                 for(uint8_t i=0;i<8;i++)
  138.                         showBuf[i] = 0x40;
  139.                 return;
  140.         }
  141.        
  142.        
  143.         int32_t w = weight*100;//保留两位小数
  144.        
  145.         //显示符号
  146.         if(w > 0)
  147.                 showBuf[0] = 0;
  148.         else
  149.                 showBuf[0] = 0x40;
  150.        
  151.         w = abs(w);
  152.        
  153.         showBuf[7] = smgCode[w%10];
  154.         showBuf[6] = smgCode[w/10%10];
  155.         showBuf[5] = smgCode[w/100%10] | 0x80;
  156.         showBuf[4] = smgCode[w/1000%10];
  157.         showBuf[3] = smgCode[w/10000%10];
  158.         showBuf[2] = smgCode[w/100000%10];
  159.         showBuf[1] = smgCode[w/1000000%10];
  160.        
  161.         //取消前面0的显示
  162.         for(uint8_t i=1;i<6;i++)
  163.         {
  164.                 if(showBuf[i] == smgCode[0])
  165.                         showBuf[i] = 0x00;
  166.                 else
  167.                         break;
  168.         }
  169.        
  170. }
  171. /*************************************************
  172. * @function:void smg_send_data(bool on,uint8_t brightness)   
  173. * @description: 数码管发送数据
  174. * @calls:         
  175. * @input:                    
  176. * @return:      
  177. * @others:      
  178. *************************************************/
  179. void smg_send_data(bool on,uint8_t brightness)
  180. {
  181.         tm1640_start();
  182.         tm1640_write_byte(0x40);                                //设置数据命令
  183.         tm1640_stop();
  184.         tm1640_start();
  185.         tm1640_write_byte(0xc0);                                //设置显示地址
  186.         for(uint8_t i=0;i<sizeof(showBuf);i++)
  187.         {
  188.                 tm1640_write_byte(showBuf[i]);
  189.         }
  190.         tm1640_stop();
  191.         tm1640_start();
  192.         tm1640_write_byte(0x80 | on<<3 | brightness);                //设置显示控制命令
  193.         tm1640_stop();
  194. }
  195.        
  196.        
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

去皮卡多

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表