一、目的
这一节我们学习如何使用我们的ESP32开发板来通过IIC接口控制TM1650四位共阴数码管模块。
二、环境
ESP32 + Thonny + TM1650四位共阴数码管模块 + 几根杜邦线 + Win10
接线方法:
三、驱动
我根据别人博客改的,不敢说是全网唯一,但是也是第二,哈哈。。。不好之处,大家多多提意见。你有好的驱动,麻烦请评论区留言,谢谢!
FourDigitDisplay.py
'''
ESP32 drive for Four Digit LED Display (TM1650)
http://www.micropython.org.cn
Auther: 魔都飘雪 2023-2-14
'''
from machine import Pin,I2C
#from machine import Pin,SoftI2C #软I2C
COMMAND_I2C_ADDRESS = 0x24 # 对应数码管I2C地址[36, 37, 38, 39, 52, 53, 54, 55]的36
DISPLAY_I2C_ADDRESS = 0x34 # 对应数码管I2C地址[36, 37, 38, 39, 52, 53, 54, 55]的52
# 对应单个数码管的0到F显示。0不亮1亮。例如0x3F二进制为0011 1111,高位dp,地位为a,对应8段数码管则显示为0
buf = (0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71)
i2c = I2C(1)
#i2c = SoftI2C(scl=Pin(25), sda=Pin(26)) #软I2C
class FourDigitDisplay():
def __init__(self):
self._intensity = 3
self.dbuf = [0, 0, 0, 0]
self.tbuf = bytearray(1)
self.on()
def intensity(self, dat = -1):
if dat < 0 or dat > 8:
return self._intensity
if dat == 0:
self.off()
else:
self._intensity = dat
self.cmd((dat<<4)|0x01)
def cmd(self, c):
self.tbuf[0] = c
i2c.writeto(COMMAND_I2C_ADDRESS, self.tbuf)
def dat(self, bit, d):
self.tbuf[0] = d
i2c.writeto(DISPLAY_I2C_ADDRESS + (bit%4), self.tbuf)
def on(self):
self.cmd((self._intensity<<4)|0x01)
def off(self):
self._intensity = 0
self.cmd(0)
def clear(self):
self.dat(0, 0)
self.dat(1, 0)
self.dat(2, 0)
self.dat(3, 0)
self.dbuf = [0, 0, 0, 0]
def showbit(self, num, bit = 0):
self.dbuf[bit%4] = buf[num%16]
self.dat(bit, buf[num%16])
def shownum(self, num):
if num < 0:
self.dat(0, 0x40) # '-'
num = -num
else:
self.showbit((num // 1000) % 10)
self.showbit(num % 10, 3)
self.showbit((num // 10) % 10, 2)
self.showbit((num // 100) % 10, 1)
def showhex(self, num):
if num < 0:
self.dat(0, 0x40) # '-'
num = -num
else:
self.showbit((num >> 12) % 16)
self.showbit(num % 16, 3)
self.showbit((num >> 4) % 16, 2)
self.showbit((num >> 8) % 16, 1)
def showDP(self, bit = 1, show = True):
if show:
self.dat(bit, self.dbuf[bit] | 0x80)
else:
self.dat(bit, self.dbuf[bit] & 0x7F)
四、示例代码
from machine import Pin,I2C,SoftI2C
from FourDigitDisplay import *
import time
# 软I2C也是可以的,驱动对应改一下即可
#i2c = SoftI2C(scl=Pin(25), sda=Pin(26), freq=100000)
#print(i2c.scan()) # scan for devices
#硬I2C方法,此处使用I2c 1
i2c = I2C(1, scl=Pin(25), sda=Pin(26), freq=400000)
#36对应驱动里的COMMAND_I2C_ADDRESS = 0x24; 52对应驱动里的DISPLAY_I2C_ADDRESS = 0x34
print(i2c.scan()) # 打印出[36, 37, 38, 39, 52, 53, 54, 55]
#显示整数
fdd = FourDigitDisplay()
fdd.shownum(9999) # 显示4个9
time.sleep(2)
#显示小数
fdd.shownum(1234) # 显示4个9
fdd.showDP(0) # 默认是2位小数;showDP(0)三位小数;showDP(2)一位小数;
time.sleep(2)
#显示十六进制数
fdd.showhex(60) # 显示为3C
time.sleep(2)
#清屏
fdd.clear()
time.sleep(2)
#显示余数数
fdd.showbit(18) # 18%16=2,所以显示为2
time.sleep(2)
#调节显示亮度
fdd.intensity(8) #亮度为1到8
time.sleep(2)
fdd.intensity(0) #关闭显示,相当于fdd.off()
time.sleep(2)
fdd.on() #打开显示
time.sleep(2)
fdd.intensity(2)
time.sleep(2)
for i in range(0,10000,1):
fdd.shownum(i)
time.sleep(0.25)
五、显示效果
六、数码管购买
七、参考资料
【micropython】microbit micropython使用第三方库实现四位数码管显示_GEEK.攻城狮的博客-CSDN博客_micropython安装第三方库1、microbit文件系统当我们使用内置库函数时,只需要使用import功能将相关的库加载进来即可。我们在获取第三方库或者自行编写库文件时,一般为.PY的源代码文件。我们首先需要将文件写入到microbit的文件系统中。在micropython中,可以通过内置os库来操作文件。API功能os.listdir()显示系统中的文件os.remove(filename)删除文件os.size(filename)查看文件大小os.uname()显示MicroPython和设备信息在REhttps://blog.csdn.net/cw_huang/article/details/114061052class I2C – a two-wire serial protocol — MicroPython latest documentationhttp://docs.micropython.org/en/latest/library/machine.I2C.html