TM1637 4 digit Demonstration

premelec

Senior Member
OOps there is a bug in this code so 4th digit doesn't show - I think it's when I added first two digits blanking as it counted ok to 9999 b4-
I'll be back... ;-0 Yep its an error in READ before ENDIF position - I've corrected it here but can't delete attached BASIC file yet...

Code:
; TM1637 Demo & test code
; TM1637test5X.bas Not i2c - spi modified from various forum code;
; two lines required Clock & Data
; TM1637 4 June 2020 counts to 9999 OK...  blank first 2 digits done

#picaxe 08M ;my test unit - should work ok on 08M2 206 of 256 BYTEs
;#no_data
setfreq m8 ; max frequency for 08M
;segment data for TM1637 Seg Bars clockwise from top - 7th bit on is middle bar
;Bit = 1 turns segment on
;(      0   1   2   3   4   5   6   7   8   9   A   b   C   d   E   F  dash blank)
DATA 0,($3f,$06,$5b,$4f,$66,$6d,$7d,$07,$7f,$6f,$77,$7c,$39,$5e,$79,$71, $40, $00)


;for TM1637
symbol datout = C.1        ; TM1637 data pin to 08m leg 6
symbol clk = C.0           ; TM1637 clock pin to 08m leg 5
symbol datain = w4     ; WORD byte to display 16 bits
symbol idx = W5         ; general index word var 16 bits
symbol bitdat = b0     ; holds bits to bang out

symbol  D3 = b2
symbol  D2 = b3           ;Extracted NUMERALS for 4 digits
symbol  D1 = b4
symbol  D0 = b5


gosub brightness           ;set display brightness

DO ;increments digits by 1 to 9999 - could decrement with small changes for countdown
    GOSUB GetDigits
    GOSUB Display
     pause 200
     idx = idx + 1
    Datain = idx
    IF Datain > 9999 THEN ; reset to 0
     idx = 0
     pause 10000 ; long pause at 9999
    ENDIF
LOOP


end ;JIC [Just In Case yoou get here...]

;===========================================================================

;Group of subroutines to read the values from data and output to the display

DISPLAY:
  ;Enable sequential movement (from one digit to next) NOT required??
  #rem
  Gosub begin7
  b0 = $40 ;64
  Gosub Out7
  Gosub Stop7
  #endrem

  ;Move to first digit
  Gosub begin7
   b0 = 192 ;$C0 ;192
  Gosub Out7

  ;First value
  B0=B1

  IF D3 = 0 THEN
   D3=17 ; blank
ENDIF
   read D3,b0
    Gosub Out7

  ;Second value
  IF D3 = 17 AND D2 = 0 THEN
   D2 = 17 ; blank
  ENDIF
  read D2,b0
  ;if b1 = 17 then                                    
  ;b0 = b0 + 128   ;add 128 to turn on colon
  ;endif                                        
  Gosub Out7         

  ;Third value
  read D1,b0
  Gosub Out7

  ;Fourth value
  read D0,b0
  Gosub Out7

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
begin7:

  high clk
  high datout
  low datout
  low clk

RETURN

  Gosub Stop7
RETURN

Stop7:
  low clk
  low datout
  high clk
  high datout
RETURN

;Send a byte to TM1637
Out7:  ; Bit bang SPI  Out

  pin1 = bit0:pulsout clk, 1
  pin1 = bit1:pulsout clk, 1
  pin1 = bit2:pulsout clk, 1
  pin1 = bit3:pulsout clk, 1
  pin1 = bit4:pulsout clk, 1
  pin1 = bit5:pulsout clk, 1
  pin1 = bit6:pulsout clk, 1
  pin1 = bit7:pulsout clk, 1
  pulsout clk, 1

RETURN

;Set the brightness ($88 = on, $88 to $8F 136 to 143 are the brightness levels)
brightness:
  Gosub begin7
  b0 = 136; 143 ;$89 ;137
  Gosub Out7
  Gosub Stop7
RETURN


;Subject: Extract single digits from WORD

GetDigits:
;datain = 0
    D3 = datain /1000
     datain = datain //1000
    D2 = datain / 100
     datain = datain // 100
    D1 = datain / 10
    D0 = datain // 10

RETURN
 

Attachments

Last edited:
Top