1 Star 0 Fork 0

visitor009/esp32 micropython

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tm1650.py 4.06 KB
一键复制 编辑 原始数据 按行查看 历史
visitor009 提交于 2024-02-02 13:25 . update tm1650.py.
'''
from tm1650 import TM1650
display = TM1650(22,21) # clk dio
display.displayDot(2,1) # 显示小数点.位置2 显示1 0关闭
display.displayBit(2,1) # 显示数字。位置2 数字1
display.ShowNum(11) 显示数字
'''
from machine import Pin
class TM1650:
# TM1650定义
ADDR_DIS = 0x48 #模式命令
# 数字:0~9
NUM = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f]
DIG = [0x6e,0x6c,0x6a,0x68]
DOT = [0,0,0,0]
DisplayCommand = (0 & 0xfe)+1
def __init__(self, clk,dio):
self.clkPin = clk
self.dioPin = dio
self.clk = Pin(clk,Pin.OUT)
self.dio = Pin(dio,Pin.OUT)
def writeByte(self,wr_data):
clk = self.clk
dio = self.dio
for i in range(8):
if(wr_data & 0x80 == 0x80):
dio.value(1)
else:
dio.value(0)
clk.value(0)
time.sleep(0.0001)
clk.value(1)
time.sleep(0.0001)
clk.value(0)
wr_data <<= 1
return
def start(self):
clk = self.clk
dio = self.dio
dio.value(1)
clk.value(1)
time.sleep(0.0001)
dio.value(0)
return
def ack(self):
clk = self.clk
dio = self.dio
dy = 0
clk.value(0)
time.sleep(0.0001)
dio = Pin(self.dioPin, Pin.IN)
while(dio.value() == 1):
time.sleep(0.0001)
dy += 1
if(dy>5000):
break
clk.value(1)
time.sleep(0.0001)
clk.value(0)
dio = Pin(self.dioPin, Pin.OUT)
return
def stop(self):
clk = self.clk
dio = self.dio
dio.value(0)
clk.value(1)
time.sleep(0.0001)
dio.value(1)
return
def displayBit(self,bit, num):
if(num > 9 and bit > 4):
return
self.start()
self.writeByte(self.ADDR_DIS)
self.ack()
self.writeByte(self.DisplayCommand)
self.ack()
self.stop()
self.start()
self.writeByte(self.DIG[bit-1])
self.ack()
if(self.DOT[bit-1] == 1):
self.writeByte(self.NUM[num] | 0x80)
else:
self.writeByte(self.NUM[num])
self.ack()
self.stop()
return
def clearBit(self,bit):
if(bit > 4):
return
self.start()
self.writeByte(self.ADDR_DIS)
self.ack()
self.writeByte(self.DisplayCommand)
self.ack()
self.stop()
self.start()
self.writeByte(self.DIG[bit-1])
self.ack()
self.writeByte(0x00)
self.ack()
self.stop()
return
def setBrightness(self,b = 2):
self.DisplayCommand = (self.DisplayCommand & 0x0f)+(b<<4)
return
def setMode(self,segment = 0):
self.DisplayCommand = (self.DisplayCommand & 0xf7)+(segment<<3)
return
def displayOnOFF(self,OnOff = 1):
self.DisplayCommand = (self.DisplayCommand & 0xfe)+OnOff
return
def displayDot(self,bit, OnOff):
if(bit > 4):
return
if(OnOff == 1):
self.DOT[bit-1] = 1;
else:
self.DOT[bit-1] = 0;
return
def ShowNum(self,num): #0~9999
self.displayBit(1,num%10)
if(num < 10):
self.clearBit(2)
self.clearBit(3)
self.clearBit(4)
if(num > 9 and num < 100):
self.displayBit(2,num//10%10)
self.clearBit(3)
self.clearBit(4)
if(num > 99 and num < 1000):
self.displayBit(2,num//10%10)
self.displayBit(3,num//100%10)
self.clearBit(4)
if(num > 999 and num < 10000):
self.displayBit(2,num//10%10)
self.displayBit(3,num//100%10)
self.displayBit(4,num//1000)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/visitor009/esp32-micropython.git
[email protected]:visitor009/esp32-micropython.git
visitor009
esp32-micropython
esp32 micropython
master

搜索帮助