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

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

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)
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
Image for post
Image for post
7-segment digit with a decimal point

3. TM1638 SPI-like interface

Image for post
Image for post

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