Write SPIN code for TM1637 and TM1638 (Part I)

big-banging seven-segment driver chips

Image for post
Image for post
Apollo guidance computer DSKY display unit with seven-segment digits
Image for post
Image for post
LED & KEY (left); QYF-TM1638 (middle); RobotDyn 4-digit (right)

1. Parallax Propeller and SPIN

Parallax Propeller is an interesting MCU. It is helpful to think of it as a piece of FPGA hardware with 8 CPU cores. The CPU core is uniquely hand-crafted, called COG. (The current Propeller chip has 8 COGs and it is even open sourced here.)

CON
LED_PIN = 0 'LED connected to P0 pin

PUB main
dira[LED_PIN] := 1 'set pin as output
outa[LED_PIN] := 1 'set pin as high
repeat

2. TM1637 I2C-like interface

TM1637 datasheet wasn’t well translated into English from its Chinese version (download from its manufacturer). For example, in the English datasheet, “LED high pressure drive” should really be “LED high voltage drive”, because 高压 was mistaken into “high water pressure”, rather than “high voltage”. There are many such “machine translation” issues in the English datasheet.

Image for post
Image for post
4 connections between TM1637 and Propeller Flip board; 3.3V is enough for 5V input; Blue LED may require more than 3.3V
Image for post
Image for post
10K external pull-up on CLK and DIO
Image for post
Image for post
START + 8-bit DATA + 1-bit ACK + END
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
CON
DIO_PIN = 24 'P24
CLK_PIN = 25 'P25

CON
DISPLAY_ON_CMD = $8F
DISPLAY_OFF_CMD = $80
DATA_CMD = $40
ADDR_CMD = $C0
PUB main
'init: external pull up high both DIO and CLK
dira[DIO_PIN] := 0
dira[CLK_PIN] := 0
start 'start condition
send_byte(DISPLAY_ON_CMD) 'turn-on display; full brightness
end 'end condition
repeatPRI start
'get ready to drive low
outa[DIO_PIN] := 0
outa[CLK_PIN] := 0
'start condition
dira[DIO_PIN]:= 1 'drive DIO low
delay
dira[CLK_PIN] := 1 'drive CLK low
PRI send_byte(data_byte)
'8-bit LSB first
repeat 8
outa[CLK_PIN] := 0
delay
outa[DIO_PIN] := data_byte & $01
data_byte >>= 1
outa[CLK_PIN] := 1
delay

'9th CLK for ACK bit
outa[CLK_PIN] := 0
dira[DIO_PIN] := 0 'release DIO to TM1637
delay
outa[CLK_PIN] := 1
delay
outa[CLK_PIN] := 0 'finish 9th CLK; TM1637 release DIO
outa[DIO_PIN] := 0
dira[DIO_PIN] := 1 'reclaim DIO
delay

PRI end
'end condition
outa[CLK_PIN] := 1
delay
outa[DIO_PIN] := 1
delay

'release bus
dira[CLK_PIN] := 0
dira[DIO_PIN] := 0
PRI delay
'~10us
waitcnt(clkfreq/100_000 + cnt)
  1. after sending 8-bit dira[DIO_PIN]:= 0 sets DIO pin as input, thus releases the line from MCU side. Because of the pull up resistor externally, it is either pulled high or driven low by TM1637 signaling ACK bit.
  2. data_byte >>= 1 right shift data byte to send it out LSB first
  3. START condition: drive DIO low while CLK is high
  4. END condition: drive CLK high first, followed by DIO going high
Image for post
Image for post
8th and 9th CLK; TM1637 grabs and releases of DIO line
DAT
digits BYTE %00111111, %00000110, %01011011, %01001111, %01100110, %01101101, %01111101, %00000111, %01111111, %01101111
PUB main
'init: external pull up to set both DIO and CLK high
dira[DIO_PIN] := 0
dira[CLK_PIN] := 0

start 'start condition
send_byte(DISPLAY_ON) 'turn-on display; full brightness
end
start
send_byte(DATA_CMD)
end
start
send_byte(ADDR_CMD)
send_byte(digits[3] | $80)
send_byte(digits[1])
send_byte(digits[4])
send_byte(digits[1])
end
repeat
  1. Usually each command is enclosed in between start and end. The ADDR_CMD can take up to 6 bytes to control 6 digits in sequence.
  2. Each bit turns on/off one segment of the digit, according to datasheet
Image for post
Image for post
7-segment digit with a decimal point

3. TM1638 SPI-like interface

Because both chips come from the same manufacturer (Chinese datasheet), TM1638 has similar commands with TM1637.

Image for post
Image for post

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store