Write SPIN code for TM1637 and TM1638 (Part II)

big-banging seven-segment driver chips

In Pat I of this series, we focused on TM1637, its digital signals and timing. In comparison, TM1638’s signals are a lot easier. Because it has an extra STB (just like SPI’s CS line), there is no need for START, END conditions.

Several inexpensive seven-segment modules use TM1638, such as QTF-TM1638, LED&KEY modules. Main features include:

  1. 8 of seven-segment digits
  2. push-button keyboard

Here is the connection diagram of LED&KEY module with Propeller.

Image for post
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
CON
DIO_PIN = 24
CLK_PIN = 25
STB_PIN = 26
DAT
digits BYTE %00111111, %00000110, %01011011, %01001111, %01100110, %01101101, %01111101, %00000111, %01111111, %01101111


PUB main
dira[DIO_PIN] := 1
dira[CLK_PIN] := 1
dira[STB_PIN] := 1
outa[STB_PIN] := 1
outa[CLK_PIN] := 1
start
send_byte($8f) 'turn on
end

start
send_byte($40) 'write to register
end

start 'clear
send_byte($c0)
repeat 16
send_byte($00)
end
start
send_byte($c0)
send_byte(digits[3]^$80)
send_byte($01)
send_byte(digits[1])
send_byte($01)
send_byte(digits[4])
send_byte($01)
send_byte(digits[1])
send_byte($01)
send_byte(digits[5])
send_byte($01)
send_byte(digits[9])
send_byte($01)
send_byte(digits[2])
send_byte($01)
send_byte(digits[6])
send_byte($01)
end

repeat
PRI send_byte(data_byte)
'lsb first
repeat 8
outa[CLK_PIN] := 0
delay
outa[DIO_PIN] := data_byte & $01
data_byte >>= 1
outa[CLK_PIN] := 1
delay


PRI start
outa[STB_PIN]:= 0
delay
PRI end
outa[STB_PIN] := 1
delay
PRI delay '10us
waitcnt(clkfreq/100_000 + cnt)

Things to note:

  1. start, end, and send_byte functions are lot simpler comparing to TM1637 SPIN code
  2. the three commands to turn on/off display (0x80/0x8F), set data in/out (0x40/0x44/0x42), set address (0xC0 to 0xCF) are very similar to TM1637; however since TM1638 supports 10 segments, it takes two bytes to store their on/off infomation. According the schematic of the module (here), segment 9 goes to light an LED.
Image for post

Now let’s add the code to read the push button key press, scanned by TM1638.

CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
CON
DIO_PIN = 24
CLK_PIN = 25
STB_PIN = 26
DAT
digits BYTE %00111111, %00000110, %01011011, %01001111, %01100110, %01101101, %01111101, %00000111, %01111111, %01101111

VAR
byte DIN[4]

PUB main | Index
dira[DIO_PIN] := 1
dira[CLK_PIN] := 1
dira[STB_PIN] := 1
outa[STB_PIN] := 1
outa[CLK_PIN] := 1
start
send_byte($8f) 'turn on
end

start
send_byte($40) 'write to register
end

start 'clear
send_byte($c0)
repeat 16
send_byte($00)
end

repeat
start
send_byte($42)
delay
repeat Index from 0 to 3
read_byte(@DIN[Index])
end
start
send_byte($40)
end
start
send_byte($c0)
repeat Index from 0 to 3
send_byte(DIN[Index])
send_byte($01)
end

waitcnt(clkfreq/10 + cnt)
PRI read_byte(data_byte_addr) | tmp
tmp := 0
dira[DIO_PIN]~
repeat 8
tmp <<= 1
outa[CLK_PIN] := 0
delay
outa[CLK_PIN] := 1
tmp := tmp | ina[DIO_PIN]
delay

byte[data_byte_addr] := tmp >< 8
dira[DIO_PIN]~~
PRI send_byte(data_byte)
'lsb first
repeat 8
outa[CLK_PIN] := 0
delay
outa[DIO_PIN] := data_byte & $01
data_byte >>= 1
outa[CLK_PIN] := 1
delay


PRI start
outa[STB_PIN]:= 0
delay
PRI end
outa[STB_PIN] := 1
delay
PRI delay '10us
waitcnt(clkfreq/100_000 + cnt)

Things to note:

  1. read_byte function shifts in bits from DIO pin and store the 4-byte into an array.
  2. These 4 bytes are then used to light 4 seven-segment digits.
  3. Because of the wiring of S1 to S8 push button, you will see the corresponding segment lights up when the button is pushed

It is easier to identify which button is scanned in the 4-byte by looking at the DIO pin waveform on an oscilloscope.

Image for post

You ask why the B0 of the second byte goes high when S2 is pressed.

First according to the schematic, push button S2 is connected between “K3” and “SEG3”. Then according to this table

Image for post

BYTE2 (second row), K3 and KS3 corresponds to B0. Mystery solved.

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