注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题

zhangyadong300的个人空间 http://home.eeworld.com.cn/space-uid-600082.html [收藏] [复制] [分享] [RSS]

日志

【micropython教程】实验3 TM1637数码管驱动

已有 35 次阅读2018-6-26 15:31 |个人分类:【micropython教程】

首先在GitHub上下载的中文社区mpy驱动库地址:https://github.com/shaoziyang/mpy-lib
驱动库代码

  1. '''
  2.     mpy drive for Four Digit LED Display (TM1637)

  3. Author: shaoziyang
  4.     Date:   2018.3

  5.     [url]http://www.micropython.org.cn[/url]

  6. '''
  7. from machine import Pin
  8. from time import sleep_us

  9. TM1637_CMD1 = (64)  # 0x40 data command
  10. TM1637_CMD2 = (192) # 0xC0 address command
  11. TM1637_CMD3 = (128) # 0x80 display control command
  12. TM1637_DELAY = (10) # 10us delay between clk/dio pulses

  13. _SEGMENTS = (0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71)

  14. class TM1637():
  15.     def __init__(self, clk, dio, intensity=7, number = 4):
  16.         self.clk = clk
  17.         self.dio = dio

  18.         self._intensity = intensity%8
  19.         self._LED = number
  20.         self._ON = 8
  21.         self.dbuf = [0, 0, 0, 0]

  22.         self.clk.init(Pin.OUT, value=0)
  23.         self.dio.init(Pin.OUT, value=0)
  24.         sleep_us(TM1637_DELAY)

  25.         self.clear()

  26.     def _start(self):
  27.         self.dio(0)
  28.         sleep_us(TM1637_DELAY)
  29.         self.clk(0)
  30.         sleep_us(TM1637_DELAY)

  31.     def _stop(self):
  32.         self.dio(0)
  33.         sleep_us(TM1637_DELAY)
  34.         self.clk(1)
  35.         sleep_us(TM1637_DELAY)
  36.         self.dio(1)

  37.     def _write_data_cmd(self):
  38.         # automatic address increment, normal mode
  39.         self._start()
  40.         self._write_byte(TM1637_CMD1)
  41.         self._stop()

  42.     def _write_dsp_ctrl(self):
  43.         # display on, set brightness
  44.         self._start()
  45.         self._write_byte(TM1637_CMD3 | self._ON | self._intensity)
  46.         self._stop()

  47.     def _write_byte(self, b):
  48.         for i in range(8):
  49.             self.dio((b >> i) & 1)
  50.             sleep_us(TM1637_DELAY)
  51.             self.clk(1)
  52.             sleep_us(TM1637_DELAY)
  53.             self.clk(0)
  54.             sleep_us(TM1637_DELAY)
  55.         self.clk(1)
  56.         sleep_us(TM1637_DELAY)
  57.         self.clk(0)
  58.         sleep_us(TM1637_DELAY)

  59.     def on(self):
  60.         self._ON = 8
  61.         self._write_data_cmd()
  62.         self._write_dsp_ctrl()

  63.     def off(self):
  64.         self._ON = 0
  65.         self._write_data_cmd()
  66.         self._write_dsp_ctrl()

  67.     def intensity(self, val=None):
  68.         if val is None:
  69.             return self._intensity
  70.         val = max(0, min(val, 8))
  71.         if val == 0:
  72.             self.off()
  73.         else:
  74.             self._ON = 8
  75.             self._intensity = val-1
  76.             self._write_data_cmd()
  77.             self._write_dsp_ctrl()

  78.     def _dat(self, bit, dat):
  79.         self._write_data_cmd()
  80.         self._start()
  81.         self._write_byte(TM1637_CMD2 | (bit%self._LED))
  82.         self._write_byte(dat)
  83.         self._stop()
  84.         self._write_dsp_ctrl()

  85.     def clear(self):
  86.         self._dat(0, 0)
  87.         self._dat(1, 0)
  88.         self._dat(2, 0)
  89.         self._dat(3, 0)
  90.         self.dbuf = [0, 0, 0, 0]

  91.     def showbit(self, num, bit = 0):
  92.         self.dbuf[bit%self._LED] = _SEGMENTS[num%16]
  93.         self._dat(bit, _SEGMENTS[num%16])

  94.     def showDP(self, bit = 1, show = True):
  95.         bit = bit%self._LED
  96.         if show:
  97.             self._dat(bit, self.dbuf[bit] | 0x80)
  98.         else:
  99.             self._dat(bit, self.dbuf[bit] & 0x7F)

  100.     def shownum(self, num):
  101.         if num < 0:
  102.             self._dat(0, 0x40)   # '-'
  103.             num = -num
  104.         else:
  105.             self.showbit((num // 1000) % 10)
  106.         self.showbit(num % 10, 3)
  107.         self.showbit((num // 10) % 10, 2)
  108.         self.showbit((num // 100) % 10, 1)

  109. def showhex(self, num):
  110.         if num < 0:
  111.             self._dat(0, 0x40)   # '-'
  112.             num = -num
  113.         else:
  114.             self.showbit((num >> 12) % 16)
  115.         self.showbit(num % 16, 3)
  116.         self.showbit((num >> 4) % 16, 2)
  117.         self.showbit((num >> 8) % 16, 1)



复制代码


线路连接

从上到下依次:
CLK--时钟
DIO--数据
VCC--电源正
GND--电源负


上两个TM1637文档链接
https://wenku.baidu.com/view/b255b268998fcc22bcd10dfc.html
https://wenku.baidu.com/view/942f2644647d27284a735117.html

下面是测试代码,也是用的官方的
  1. '''
  2.     Four Digit Display (TM1637) demo

  3.     Author: shaoziyang
  4.     Date:   2018.3

  5.     [url]http://www.micropython.org.cn[/url]

  6. '''
  7. from machine import Pin
  8. import time
  9. import TM1637

  10. dio = Pin(Pin("Y4"), Pin.OUT)
  11. clk = Pin(Pin("Y3"), Pin.OUT)
  12. tm=TM1637.TM1637(dio=dio,clk=clk)

  13. n = 0
  14. while 1:
  15.     tm.shownum(n)
  16.     n += 1
  17.     time.sleep_ms(100)
复制代码





本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

小黑屋|手机版|Archiver|电子工程世界 ( 京ICP证 060456

GMT+8, 2018-7-17 00:36 , Processed in 0.016154 second(s), 11 queries , Gzip On, Redis On.

Powered by EEWORLD电子工程世界

© 2018 http://bbs.eeworld.com.cn/

返回顶部