​ ​ ​ ​ Easy 4 digit LED display
Results 1 to 3 of 3

Thread: Easy 4 digit LED display

  1. #1
    Member
    Join Date
    Jul 2010
    Location
    Colorado USA
    Posts
    88

    Default Easy 4 digit LED display

    The TM1637 LED display is widely available, cheap, and an easy way to add a four
    digit LED display to a PICAXE project. Borrowing code from the link below I wrote
    a version which runs much faster and the code size is reduced. It has been tested from
    4 to 32 mhz on M2 parts and also at 64 mhz on X2 parts. The code could be made
    to run even faster by converting the subroutines to macros.

    Code:
    ;TM1637_test.bas
    
    #picaxe 08m2
    
    ;segment data for TM1637
         ;(  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 dio = C.1                        ;TM1637 data pin to 08m2 leg 6
    symbol clk = C.2                        ;TM1637 clock pin to 08m2 leg 5
    symbol  D3 = b2
    symbol  D2 = b3                         ;for all 4 digits
    symbol  D1 = b4
    symbol  D0 = b5
    
    
    gosub brightness                        ;set display brightness
    
    for b1 = 0 to 17                        ;sequence thru all character
      D3 = b1:D2 = b1                       ;bytes stored in the data 
      D1 = b1:D0 = b1
      gosub DISPLAY
      pause 1000
    next b1
    end
    
    ;===========================================================================  
    
    ;Group of subroutines to read the values from data and output to the display
    
    DISPLAY:
      ;Enable sequential movement (from one digit to next)
      call i2CStart
      b0 = $40
      call i2CWrByte
      call i2CStop
      
      ;Move to first digit
      call i2CStart 
      b0 = $C0
      call i2CWrByte
    
      ;First value
      read D3,b0
      call i2CWrByte
      
      ;Second value
      read D2,b0
      if b1 = 17 then                                         
      b0 = b0 + 128                         ;add 128 to turn on colon
      endif                                             
      call i2CWrByte              
    
      ;Third value
      read D1,b0
      call i2CWrByte
    
      ;Fourth value
      read D0,b0
      call i2CWrByte
      
      call i2CStop
      return
    
    i2CStart:
      high clk
      high dio
      low dio
      low clk
      return
      
    i2CStop:
      low clk
      low dio
      high clk
      high dio
      return
    
    ;Send a byte
    i2CWrByte:  ;outpinC.1 = dio
      outpinC.1 = bit0:pulsout clk, 1
      outpinC.1 = bit1:pulsout clk, 1
      outpinC.1 = bit2:pulsout clk, 1
      outpinC.1 = bit3:pulsout clk, 1
      outpinC.1 = bit4:pulsout clk, 1
      outpinC.1 = bit5:pulsout clk, 1
      outpinC.1 = bit6:pulsout clk, 1
      outpinC.1 = bit7:pulsout clk, 1
      pulsout clk, 1
      return
    
    ;Set the brightness ($88 = on, $88 to $8F are the brightness levels)
    brightness:
      call i2CStart
      b0 = $89
      call i2CWrByte
      call i2CStop
      return
      
    ;===========================================================================

    Cheers to All
    Last edited by pxgator; 16-06-2017 at 01:48. Reason: updated code

  2. #2
    Senior Member
    Join Date
    May 2013
    Location
    Ireland
    Posts
    221

    Default

    Quote Originally Posted by pxgator View Post
    The TM1637 LED display is widely available, cheap, and an easy way to add a four
    digit LED display to a PICAXE project. Borrowing code from the link below I wrote
    a version which runs much faster and the code size is reduced. It has been tested from
    4 to 32 mhz on M2 parts and also at 64 mhz on X2 parts. The code could be made
    to run even faster by converting the subroutines to macros.

    Code:
    ;TM1637_test.bas
    
    #picaxe 08m2
    
    ;segment data for TM1637
         ;(  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 dio = C.1                        ;TM1637 data pin to 08m2 leg 6
    symbol clk = C.2                        ;TM1637 clock pin to 08m2 leg 5
    symbol  D3 = b2
    symbol  D2 = b3                         ;for all 4 digits
    symbol  D1 = b4
    symbol  D0 = b5
    
    
    gosub brightness                        ;set display brightness
    
    for b1 = 0 to 17                        ;sequence thru all character
      D3 = b1:D2 = b1                       ;bytes stored in the data 
      D1 = b1:D0 = b1
      gosub DISPLAY
      pause 1000
    next b1
    end
    
    ;===========================================================================  
    
    ;Group of subroutines to read the values from data and output to the display
    
    DISPLAY:
      ;Enable sequential movement (from one digit to next)
      call i2CStart
      b0 = $40
      call i2CWrByte
      call i2CStop
      
      ;Move to first digit
      call i2CStart 
      b0 = $C0
      call i2CWrByte
    
      ;First value
      read D3,b0
      call i2CWrByte
      
      ;Second value
      read D2,b0
      if b1 = 17 then                                         
      b0 = b0 + 128                         ;add 128 to turn on colon
      endif                                             
      call i2CWrByte              
    
      ;Third value
      read D1,b0
      call i2CWrByte
    
      ;Fourth value
      read D0,b0
      call i2CWrByte
      
      call i2CStop
      return
    
    i2CStart:
      high clk
      high dio
      low dio
      low clk
      return
      
    i2CStop:
      low clk
      low dio
      high clk
      high dio
      return
    
    ;Send a byte
    i2CWrByte:  ;outpinC.1 = dio
      outpinC.1 = bit0:pulsout clk, 1
      outpinC.1 = bit1:pulsout clk, 1
      outpinC.1 = bit2:pulsout clk, 1
      outpinC.1 = bit3:pulsout clk, 1
      outpinC.1 = bit4:pulsout clk, 1
      outpinC.1 = bit5:pulsout clk, 1
      outpinC.1 = bit6:pulsout clk, 1
      outpinC.1 = bit7:pulsout clk, 1
      pulsout clk, 1
      return
    
    ;Set the brightness ($88 = on, $88 to $8F are the brightness levels)
    brightness:
      call i2CStart
      b0 = $89
      call i2CWrByte
      call i2CStop
      return
      
    ;===========================================================================

    Cheers to All
    Thanks very much for the code.

    I have ordered a few of these screens and planning on trying to implement one into my project.

    Would you be able to put up a schematic diagram in terms of pin conecction from screen to PICAXE please?

    I am using PICAXE14M2 if that makes a difference.

    Thanks very much.

    Andrei.

  3. #3
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,538

    Default

    It seems to be an I2C connection, so just two signals plus power ...

    Code:
    symbol dio = C.1 ;TM1637 data pin to 08m2 leg 6
    symbol clk = C.2 ;TM1637 clock pin to 08m2 leg 5
    Not studied the TM1367 datasheet but I would have thought the bit-banged I2C software could be replaced by HI2C commands to simplify things, improve speeds and reduce memory. That would swap the two pins around; CLK on C.1, SDA on C.2.
    Last edited by hippy; 12-09-2017 at 10:20.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •