1
1
# MicroPython TM1637 quad 7-segment LED display driver
2
2
3
+ from micropython import const
3
4
from machine import Pin
4
5
from time import sleep_us
5
6
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
9
12
10
13
# 0-9, a-f, blank, dash
11
14
_SEGMENTS = [63 ,6 ,91 ,79 ,102 ,109 ,125 ,7 ,127 ,111 ,119 ,124 ,57 ,94 ,121 ,113 ,0 ,64 ]
12
15
13
16
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."""
16
18
def __init__ (self , clk , dio , brightness = 7 ):
17
19
self .clk = clk
18
20
self .dio = dio
@@ -21,75 +23,80 @@ def __init__(self, clk, dio, brightness=7):
21
23
raise ValueError ("Brightness out of range" )
22
24
self ._brightness = brightness
23
25
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 ()
28
32
29
33
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 )
32
38
33
39
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
42
48
self ._start ()
43
- self ._write_byte (_CMD_SET1 )
49
+ self ._write_byte (TM1637_CMD1 )
44
50
self ._stop ()
45
51
46
- def _write_comm3 (self ):
52
+ def _write_dsp_ctrl (self ):
53
+ # display on, set brightness
47
54
self ._start ()
48
- self ._write_byte (_CMD_SET3 + self ._brightness + 7 )
55
+ self ._write_byte (TM1637_CMD3 | TM1637_DSP_ON | self ._brightness )
49
56
self ._stop ()
50
57
51
58
def _write_byte (self , b ):
52
- # send each bit
53
59
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 )
67
72
68
73
def brightness (self , val = None ):
69
74
"""Set the display brightness 0-7."""
75
+ # brightness 0 = 1/16th pulse width
76
+ # brightness 7 = 14/16th pulse width
70
77
if val is None :
71
78
return self ._brightness
72
79
if not 0 <= val <= 7 :
73
80
raise ValueError ("Brightness out of range" )
81
+
74
82
self ._brightness = val
75
- self ._write_comm1 ()
76
- self ._write_comm3 ()
83
+ self ._write_data_cmd ()
84
+ self ._write_dsp_ctrl ()
77
85
78
86
def write (self , segments , pos = 0 ):
79
87
"""Display up to 4 segments moving right from a given position.
80
88
The MSB in the 2nd segment controls the colon between the 2nd
81
89
and 3rd segments."""
82
90
if not 0 <= pos <= 3 :
83
91
raise ValueError ("Position out of range" )
84
- self ._write_comm1 ()
85
-
86
- # write COMM2 + first digit address
92
+ self ._write_data_cmd ()
87
93
self ._start ()
88
- self ._write_byte (_CMD_SET2 + pos )
94
+
95
+ self ._write_byte (TM1637_CMD2 | pos )
89
96
for seg in segments :
90
97
self ._write_byte (seg )
91
98
self ._stop ()
92
- self ._write_comm3 ()
99
+ self ._write_dsp_ctrl ()
93
100
94
101
def encode_digit (self , digit ):
95
102
"""Convert a character 0-9, a-f to a segment."""
@@ -146,3 +153,13 @@ def numbers(self, num1, num2, colon=True):
146
153
if colon :
147
154
segments [1 ] |= 0x80
148
155
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
0 commit comments