Skip to content

Commit 04713d7

Browse files
committed
Add temperature, fix brightness, change how pins are toggled
1 parent 0b6b804 commit 04713d7

File tree

3 files changed

+71
-50
lines changed

3 files changed

+71
-50
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
setup(
88
name='micropython-tm1637',
99
py_modules=['tm1637'],
10-
version='1.1.0',
10+
version='1.2.0',
1111
description='MicroPython library for TM1637 LED driver.',
1212
long_description='This library lets you operate quad 7-segment LED display modules based on the TM1637 LED driver.',
1313
keywords='tm1637 seven segment led micropython',

tm1637.py

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# MicroPython TM1637 quad 7-segment LED display driver
22

3+
from micropython import const
34
from machine import Pin
45
from time import sleep_us
56

6-
_CMD_SET1 = const(64) # 0x40 data set
7-
_CMD_SET2 = const(192) # 0xC0 address command set
8-
_CMD_SET3 = const(128) # 0x80 data control command set
7+
TM1637_CMD1 = const(64) # 0x40 data command
8+
TM1637_CMD2 = const(192) # 0xC0 address command
9+
TM1637_CMD3 = const(128) # 0x80 display control command
10+
TM1637_DSP_ON = const(8) # 0x08 display on
11+
TM1637_DELAY = const(10) # 10us delay between clk/dio pulses
912

1013
# 0-9, a-f, blank, dash
1114
_SEGMENTS = [63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113,0,64]
1215

1316
class TM1637(object):
14-
"""Library for the quad 7-segment LED display modules based on the TM1637
15-
LED driver."""
17+
"""Library for quad 7-segment LED modules based on the TM1637 LED driver."""
1618
def __init__(self, clk, dio, brightness=7):
1719
self.clk = clk
1820
self.dio = dio
@@ -21,75 +23,80 @@ def __init__(self, clk, dio, brightness=7):
2123
raise ValueError("Brightness out of range")
2224
self._brightness = brightness
2325

24-
self.clk.init(Pin.IN)
25-
self.dio.init(Pin.IN)
26-
self.clk(0)
27-
self.dio(0)
26+
self.clk.init(Pin.OUT, value=0)
27+
self.dio.init(Pin.OUT, value=0)
28+
sleep_us(TM1637_DELAY)
29+
30+
self._write_data_cmd()
31+
self._write_dsp_ctrl()
2832

2933
def _start(self):
30-
self.dio.init(Pin.OUT)
31-
sleep_us(50)
34+
self.dio(0)
35+
sleep_us(TM1637_DELAY)
36+
self.clk(0)
37+
sleep_us(TM1637_DELAY)
3238

3339
def _stop(self):
34-
self.dio.init(Pin.OUT)
35-
sleep_us(50)
36-
self.clk.init(Pin.IN)
37-
sleep_us(50)
38-
self.dio.init(Pin.IN)
39-
sleep_us(50)
40-
41-
def _write_comm1(self):
40+
self.dio(0)
41+
sleep_us(TM1637_DELAY)
42+
self.clk(1)
43+
sleep_us(TM1637_DELAY)
44+
self.dio(1)
45+
46+
def _write_data_cmd(self):
47+
# automatic address increment, normal mode
4248
self._start()
43-
self._write_byte(_CMD_SET1)
49+
self._write_byte(TM1637_CMD1)
4450
self._stop()
4551

46-
def _write_comm3(self):
52+
def _write_dsp_ctrl(self):
53+
# display on, set brightness
4754
self._start()
48-
self._write_byte(_CMD_SET3 + self._brightness + 7)
55+
self._write_byte(TM1637_CMD3 | TM1637_DSP_ON | self._brightness)
4956
self._stop()
5057

5158
def _write_byte(self, b):
52-
# send each bit
5359
for i in range(8):
54-
self.clk.init(Pin.OUT)
55-
sleep_us(50)
56-
self.dio.init(Pin.IN if b & 1 else Pin.OUT)
57-
sleep_us(50)
58-
self.clk.init(Pin.IN)
59-
sleep_us(50)
60-
b >>= 1
61-
self.clk.init(Pin.OUT)
62-
sleep_us(50)
63-
self.clk.init(Pin.IN)
64-
sleep_us(50)
65-
self.clk.init(Pin.OUT)
66-
sleep_us(50)
60+
self.dio((b >> i) & 1)
61+
sleep_us(TM1637_DELAY)
62+
self.clk(1)
63+
sleep_us(TM1637_DELAY)
64+
self.clk(0)
65+
sleep_us(TM1637_DELAY)
66+
self.clk(0)
67+
sleep_us(TM1637_DELAY)
68+
self.clk(1)
69+
sleep_us(TM1637_DELAY)
70+
self.clk(0)
71+
sleep_us(TM1637_DELAY)
6772

6873
def brightness(self, val=None):
6974
"""Set the display brightness 0-7."""
75+
# brightness 0 = 1/16th pulse width
76+
# brightness 7 = 14/16th pulse width
7077
if val is None:
7178
return self._brightness
7279
if not 0 <= val <= 7:
7380
raise ValueError("Brightness out of range")
81+
7482
self._brightness = val
75-
self._write_comm1()
76-
self._write_comm3()
83+
self._write_data_cmd()
84+
self._write_dsp_ctrl()
7785

7886
def write(self, segments, pos=0):
7987
"""Display up to 4 segments moving right from a given position.
8088
The MSB in the 2nd segment controls the colon between the 2nd
8189
and 3rd segments."""
8290
if not 0 <= pos <= 3:
8391
raise ValueError("Position out of range")
84-
self._write_comm1()
85-
86-
# write COMM2 + first digit address
92+
self._write_data_cmd()
8793
self._start()
88-
self._write_byte(_CMD_SET2 + pos)
94+
95+
self._write_byte(TM1637_CMD2 | pos)
8996
for seg in segments:
9097
self._write_byte(seg)
9198
self._stop()
92-
self._write_comm3()
99+
self._write_dsp_ctrl()
93100

94101
def encode_digit(self, digit):
95102
"""Convert a character 0-9, a-f to a segment."""
@@ -146,3 +153,13 @@ def numbers(self, num1, num2, colon=True):
146153
if colon:
147154
segments[1] |= 0x80
148155
self.write(segments)
156+
157+
def temperature(self, num):
158+
if num < -9:
159+
self.write([0x38, 0x3F]) # LO
160+
elif num > 99:
161+
self.write([0x76, 0x06]) # HI
162+
else:
163+
string = '{0: >2d}'.format(num)
164+
self.write(self.encode_string(string))
165+
self.write([0x63, 0x39], 2) # degrees C

tm1637_test.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@
3939
# show "COOL"
4040
tm.write([0b00111001, 0b00111111, 0b00111111, 0b00111000])
4141

42-
# all LEDs off
43-
tm.brightness(0)
44-
4542
# all LEDs dim
46-
tm.brightness(1)
43+
tm.brightness(0)
4744

4845
# all LEDs bright
4946
tm.brightness(7)
@@ -149,5 +146,12 @@
149146
# show "12:59"
150147
tm.numbers(12,59)
151148

152-
# show temperature
153-
tm.temperature(24) '24*C'
149+
# show temperature '24*C'
150+
tm.temperature(24)
151+
152+
# show temperature works for range -9 to +99
153+
tm.temperature(-10) # LO*C
154+
tm.temperature(-9) # -9*C
155+
tm.temperature(5) # 5*C
156+
tm.temperature(99) # 99*C
157+
tm.temperature(100) # HI*C

0 commit comments

Comments
 (0)