Menu

TM1637 cheap 4 digit 7seg display library

mmotte
2017-08-14
2020-12-10
  • mmotte

    mmotte - 2017-08-14

    I have been working on a temperature display/controller and ebay had thse for cheap. A lot less wiring with only two wires for communication and 2 for power supply versus sement wires, digit wires and "keep it scanning" on a regular 7 seg display.

    Thanks to viscomjim for giving a starting point

    This is the demo main:

    '#chip 12f1840,32
    #chip 16F1705,8
    
    #include <DS18B20.h>
    #include <TM1637.h>
    #define TM1637_CLK portC.4             'CLK pin TM1637
    #define TM1637_DIO portC.5            'DIO pin TM1637
    #define DQ PortC.3              'Data pin DS18B20
    
    ; ----- Variables
      'TempC_100 required to support temp sensor
      Dim TempC_100,TempF_100, DSdata as word
      Dim Whole , Fract as word
      Dim CCOUNT, SIGNBIT, WHOLE, FRACT, DIG as Byte
    
    DIM xx,Thou,Hund,Tens,Ones as word
    
    Write4Dig (17, 16, 17, 16, 0) 'clear display
    wait 3 s
    
    WriteChar (0 , "g")
    WriteChar (1 , "c")
    WriteChar (2 , "b")
    WriteChar (3 , " ")
    
    wait 3 s
    
    TMhex(255)
    wait 3 s
    
    TMdec(8095)
    wait 3 s
    
    Write4Dig(16,7,0,7,8,1)  ' clock disp , no leading zero, minimum brightness, Colon On
    wait 3 s
    
    main:
    
    'DSdata = readtemp12
      ' The function readtemp12 returns the raw value of the sensor.
           ' The sensor is read as a 12 bit value. Each unit equates to 0.0625 of a degree
           DSdata = readtemp12
           SignBit = DSdata / 256 / 128
           If SignBit = 0 Then goto Positive
           ' its negative!
           DSdata = ( DSdata # 0xffff ) + 1 ' take twos comp
    
        Positive:
    
           ' Convert value * 0.0625. Mulitple value by 6 then add result to multiplication of the value with 25 then divide result by 100.
           TempC_100 =  DSdata * 6
           DSdata = ( DSdata * 25 ) / 100
           TempC_100 = TempC_100 + DSdata
           TempF_100 = TempC_100 * 9 / 5 +3200  'Is multiplied by 100 for ranging
           Whole = TempF_100 / 100
           Fract = TempF_100 % 100
           'If SignBit = 0 Then goto DisplayTemp
           'Print "-"
    
    TMDec(TempF_100/10)   ' Throw away the hundreth place so there is room on 4 digit diplay
    
    wait 2 s
    
    'next xx
    
    goto main
    
    end
    

    This is the library:

    '    7 Segment LED display routines using the TM1637  for Great Cow BASIC
    '     The TM1637 module provide a cheap bright LED Display
    '     The ebay modules can be had for $1 to $4, somtimes less.
    '     They only need 2 pins to control : clk and dio
    '     4 digit 7 seg Display
    '     They do have a  colon but no decimal points
    '     Brightness can be set: 8 is display on minimum bright , 15 is display on max bright
    '     Less than 8  is display off
    '     The TM1637 chip has the ability to read key matrix BUT that is not included.
    '    Copyright (C)  2017  Mike Otte
    
    '    This library is free software; you can redistribute it and/or
    '    modify it under the terms of the GNU Lesser General Public
    '    License as published by the Free Software Foundation; either
    '    version 2.1 of the License, or (at your option) any later version.
    
    '    This library is distributed in the hope that it will be useful,
    '    but WITHOUT ANY WARRANTY; without even the implied warranty of
    '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    '    Lesser General Public License for more details.
    
    '    You should have received a copy of the GNU Lesser General Public
    '    License along with this library; if not, write to the Free Software
    '    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
    '********************************************************************************
    'IMPORTANT:
    'THIS FILE IS ESSENTIAL FOR SOME OF THE COMMANDS IN GCBASIC. DO NOT ALTER THIS FILE
    'UNLESS YOU KNOW WHAT YOU ARE DOING. CHANGING THIS FILE COULD RENDER SOME GCBASIC
    'COMMANDS UNUSABLE!
    '********************************************************************************
    
    'Changes:
    '  initial h file build  8/7/2017  by Mike Otte
    'Starting point was the viscomjim - 2016-11-23 post,  Thankyou
    '
    'Public Commands:
    '
    ' Write4Dig (In TMdig1, In TMdig2, In TMdig3, In TMdig4, Optional In bright = 15, Optional In ColonON = 0)
    ' Program defines each digit as 0 to 9  OR 0x00 to 0x0F  (15)
    ' additionally  0x10  (16) is a blank  and 0x11 (17) is a minus sign
    ' optional parameters can set the brightness and the turn the colon on (only on digit 2)
    '
    ' WriteChar (In TMaddr, In TMchar)
    ' TMchar is an ASCII char
    ' TMaddr is 0 , 1 , 2 , 3
    '
    '
    ' TMHex  ( In  TMValue as word)
    ' values up to 0xFFFF can be displayed
    '
    '
    ' TMDec  ( In  TMValue as word)
    ' Values up to 9999 can be displayed
    '
    '
    ' Remember you must define the two  ports in the main program
    '#define TM1637_DIO PORTa.z
    '#define TM1637_Clk PORTa.x
    
    'commands
    #define COM1 0x40   'Automatic address adding,  0x44 Fix address
    #define COM2 0xC0   'display register address, 0xC0,0xC1,0xC2,0xC3
    #define COM3 0x80   'Display control,  0x80 + 0x08 + 0x07
                        'Bit3 is display on =1  or off =0
                        'bits0-2 are bightness  0 to 7
    
    #startup InitTM1637
    
    Sub InitTM1637
      dim DispTemp as Byte
      dim Bright as Byte
          Bright = 15  'full brightness and turn on display
    '#define TM1637_DIO PORTa.z
    '#define TM1637_CLK PORTa.x
      #ifdef TM1637_DIO
        Dir TM1637_DIO Out
      #endif
      #ifdef TM1637_CLK
        Dir TM1637_CLK Out
    
    end sub
    
    'Numbers for display - support 0x00 to 0x0F
    Table SevenSegDispDigit
      63 '0
      6
      91
      79
      102
      109
      125
      7
      127
      111 '9
              0x77
              0x7c
              0x39
              0x5e
              0x79
              0x71
              0x00  'blank
              0x40  ' -  dash
    End Table
    
    'Letters for display
    'Letter is at ASC - 64, so A at 1, B at 2, etc
    Table SevenSegDispLetter
      119 'A
      124 'B
      57 'C
      94 'D
      121 'E
      113 'F
      61 'G
      118 'H
      6 'I
      14 'J
      118 'K
      56 'L
      55 'M
      55 'N
      63 'O
      115 'P
      103 'Q
      80 'R
      109 'S
      7 'T
      62 'U
      62 'V
      62 'W
      118 'X
      110 'Y
      27 'Z
    End Table
    
    '#define TM1637_DIO PORTa.z
    '#define TM1637_Clk PORTa.x
    
    sub Write4Dig (In TMdig1, In TMdig2, In TMdig3, In TMdig4, Optional In bright = 15, Optional In ColonON = 0)
        'TMdigx is number 0 -15 , 16 =sp , 17 = -
        'convert digit number to 7segments
        ReadTable SevenSegDispDigit, TMdig1 + 1, TMseg1
        ReadTable SevenSegDispDigit, TMdig2 + 1, TMseg2
        ReadTable SevenSegDispDigit, TMdig3 + 1, TMseg3
        ReadTable SevenSegDispDigit, TMdig4 + 1, TMseg4
        If ColonON = 1 then
         TMseg2 = TMseg2 | 0B10000000   'turns on the colon
        End if
        Start
        WriteValue (COM1)
        Stop
        Start
        WriteValue (COM2)
        WriteValue (TMseg1)
        WriteValue (TMseg2)
        WriteValue (TMseg3)
        WriteValue (TMseg4)
        Stop
        Start
        WriteValue ((COM3) + (bright & 0x0f))
        Stop
    end sub
    
    sub WriteChar (In TMaddr, In TMchar)
        ' TMaddr  0 ,1, 2 ,3    digit postion to display
        'TMchar   ascii character  to display
        bright = 15
        'write ascii letter or number or sp or -
        If TMchar = 32 Then     ' space?
          TMseg1 = 0
          Goto ShowChar
      End if
    
      ' -
      If TMchar = 45 Then      'minus -
          TMseg1 = 64
          Goto ShowChar
      End if
    
      'Numbers
      If TMchar >= 48 And TMchar <= 57 Then
        ReadTable SevenSegDispDigit,  TMchar-47, TMseg1
        Goto ShowChar
      End If
    
      If TMchar < 65 Then Exit Sub    'should have been covered as number and space and -
      'Convert to upper case
    
      If TMchar > 96 Then           ' small case letter make it big
          TMchar = TMchar - 32
      End if
    
      'Exit if not a letter
      If TMchar > 90 Then Exit Sub
    
      'Convert to code for output
    
      ReadTable SevenSegDispLetter, TMchar -64, TMseg1
    ShowChar:
    
        Start
        WriteValue (0x40)   '0x44 Fix address
        Stop
        Start
        WriteValue (0xC0 + TMaddr)
        WriteValue (TMseg1)
        Stop
        Start
        WriteValue ((COM3) + (bright & 0x0f))
        Stop
    end sub
    
    sub TMDec  ( In  TMValue as word)
    
        'TMValue must be in the range of 0 to 65535 (Dec)
        'TMValue can be entered as dec, binary or hex
        TMsVal = 16
        TMeVal = 0
        TMhVal = 0
        TMlVal = 0
    
              IF TMValue >= 1000 Then
                        TMsVal = TMValue / 1000 [word]
                        TMValue =SysCalcTempX
                        Goto TMWord100
              End If
              IF TMValue >= 100 Then
              TMWord100:
                        TMeVal = TMValue / 100  [word]
                        TMValue = SysCalcTempX
                        Goto TMWord10
              End If
              IF TMValue >= 10 Then
              TMWord10:
                        TMhVal = TMValue / 10 [word]
                        TMlVal = SysCalcTempX
              End If
    
        Write4Dig ( TMsVal, TMeVal, TMhVal, TMlVal)
    
    end sub
    sub TMHex  ( In  TMValue as word)
    
        'TMValue must be in the range of 0 to 65535 (Dec)
        'TMValue can be entered as dec, binary or hex
        TMsVal = 0
        TMeVal = 0
        TMhVal = 0
        TMlVal = 0
    
              IF TMValue >= 4096 Then
                        TMsVal = TMValue / 4096
                        TMValue = TMValue % 4096
              End If
              IF TMValue >= 256 Then
                        TMeVal = TMValue / 256
                        TMValue = TMValue % 256
              End If
              IF TMValue >= 16 Then
                        TMhVal = TMValue / 16
                        TMlVal = TMValue % 16
              End If
    
        Write4Dig ( TMsVal, TMeVal, TMhVal, TMlVal)
    
    end sub
    
    sub WriteValue(Value)
        for yy = 1 to 8
            set TM1637_CLK off
            'wait 5 us
            if Value & 1 = 1 then
                set TM1637_DIO on
            else
                set TM1637_DIO off
            end if
            'wait 5 us
            set TM1637_CLK on
            'wait 5 us
            Value = FnLSR(Value, 1)
        next yy
    
        'wait for ACK
        set TM1637_CLK off
        'wait 5 us
        dir TM1637_DIO in
        set TM1637_CLK on
        'wait 5 us
        if TM1637_DIO = 0 then
        dir TM1637_DIO out
        end if
        set TM1637_CLK off
    end sub
    
    sub Start
        set TM1637_CLK on
        set TM1637_DIO on
        'wait 5 us
        set TM1637_DIO off
        set TM1637_CLK off
        'wait 5 us
    end sub
    
    sub Stop
        set TM1637_CLK off
        set TM1637_DIO off
        'wait 5 us
        set TM1637_CLK on
        set TM1637_DIO on
        'wait 5 us
    end sub
    
     

    Last edit: mmotte 2017-08-14
  • Anobium

    Anobium - 2017-08-14

    Well done! Got an operational video?

     
  • Friderik Back

    Friderik Back - 2020-09-12

    The above code crashes the GCBasic compiler extremely easy and generally doesn’t work (the main reason is that communication between MCU and TM1637 is too fast), so I wrote a patch. Tested and works fine on the GCBasic versions 0.95, 0.97 and 0.98. I've also included some new features:

    • Original and Siekoo character set
    • changing the brightness of the letters
    • additional special characters: °,) and ?
    • more options for the TMdec command

    I'm wondering if I should attach the my code here or somewhere else?

     
  • stan cartwright

    stan cartwright - 2020-09-12

    nice work. I saw one on ebay for £1.39 free postage.

     
  • mmotte

    mmotte - 2020-09-12

    Yes, please attach the code here so we can enjoy your improvements. this is the contributors forum where we share code . Anobium can check it out and update the library.

    Thanks
    Mike

     
  • Friderik Back

    Friderik Back - 2020-09-13

    The attached zip file contains:
    tm1637a.h - new version of TM1637.h library
    tm1637.doc - list of commands
    tm1637demo.gcb - simple demo program
    tm1637.avi - video clip on how the demo program works

    Please note that English is not my language, so check the grammar in the .doc file is OK.

     
  • David Stephenson

    I don't have one of these displays so I can't check, but having used 7-segment displays I know that many letters cannot be rendered with just 7-segments (K,W,M...there are many). So how does it do it?

     
  • Friderik Back

    Friderik Back - 2020-09-19

    Yes, I agree, this is a known problem of 7-segment displays. For this reason, I have added the Siekoo character set (http://en.fakoo.de/siekoo.html), which solves this problem quite well. However, in some cases, the default character set looks better, so I kept both (for example, OPEn sometimes looks better than oPEn).

    Because of this, in my version of the TM1637 library, a programmer can combine both character sets.

     
  • Anobium

    Anobium - 2020-10-01

    Friderik thank you.

    Thank you on behalf of the community and thank you for your contribution to the development of Great Cow BASIC.

    Most grateful.

    Evan


    I have got to code reviewing and adding your documentation to the Help. You have written a great library. Well done and thank you.


    Code review

    I have reviewed the library and sample code.

    Library - I am recommending a few naming changes of methods() and variables. This is for consistency across your solution and to ensure your variables do not clash. (trust me YY as a variable name will clash).

    The few names changes are reflected in the draft Help see later.

    The functional change I am recommending relates to the TM_CHARBRIGHT and TM_BRIGHT. I think these do the same thing therefore I have rationalised.

    I have put the libary in the repository. This means it is now under version control. This just means you need to get it from the repository, make any changes and then we can upload for you. See here: https://sourceforge.net/p/gcbasic/code/HEAD/tree/GCBASIC/trunk/include/tm1637a.h

    Please make further changes to make perfect.

    I also removed the comments regarding the methods as we have the Help (see next)

    Help

    Thank you for the Word document.

    I have just spent a few hours hacking it into the Great Cow BASIC help. I have used the revised naming.

    But, the Help needs a good review and edit. I added an introduction and then a page for each command.

    In the Help I bumped the existing to 'legacy' and included your works in the same section. I have retained your copyright in the ADOCs.

    There are two documents - these two documents contain all of your pages. I needed to add two pages to control the CHM file.

    See https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/TM1637.adoc and https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/TM1637Sections.adoc

    You can edit these files online by cloning the GIT or directly in my GIT but I need to give you access.

    To see the draft pages, see the formats here: http://gcbasic.sourceforge.net/help/output/ The CHM is the one that we release with the IDE.

    Can you review and revise these Help pages please?

    Demo code

    I took you demo code and adapted for the new naming.

    See https://github.com/Anobium/Great-Cow-BASIC-Demonstration-Sources/blob/master/7_Segment_LED_Solutions/7_Segment_TM1637_Solutions/TM1637_7Segment.gcb

    I also reformatted the indents and a few variables needed sorting when I added #option explicit.

    IDE Helps

    I will add the IDE helpers. This means your command pop up in the IDE and then the command gets the correct Help page once you have completed a review of the Help.


    So, I compiled the new library with the demo. It compiled. It would be best for you to test also before we release to the world. :-)

    Thank you again.

     
  • Friderik Back

    Friderik Back - 2020-10-02

    Thanks Evan, great works.

    The library is working fine, the demo program needed some minor fixes and now it works flawlessly. Also I changed the name of the variable in the last for/next loop into (hopefully more appropriate to understand the program): time_to_end

    The revised demo program (with hex and other stuff) is attached below.

    In the Help, I found only one small mistake:

    https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/TM1637.adoc

    The TM1637 chip supports the reading of the LED matrix however that is not supported in the library.

    Not LED, must be keyboard:

    The TM1637 chip supports the reading of the keyboard matrix however that is not supported in the library.

    About the my Copyright Notice, if this is not common practice in the Help file, you can remove it, no problem.

    Thanks again-
    Friderik

     
  • Friderik Back

    Friderik Back - 2020-10-02

    One more thing, I don't know if it is necessary or not, in the attached zip file there is documentation about the TM1637 chip.

     
    • Anobium

      Anobium - 2020-10-02

      Thank you!

      I am truly amazed I did break your library. But, it was laid out well and no strange coding. :-)


      I will update the Help. Please check at some time to make sure I followed your instructions.

      I will update the demo with the one from the ZIP above.

      And, I have uploaded the PDFs to the website and then linked from the Help.

      Wonderful stuff.

      Thank you again.

       
  • Friderik Back

    Friderik Back - 2020-10-07

    Thank you Evan and the others for this great project.

    Here is another simple, slightly different demo program for ATtiny85 (tested on the Digispark module) and a video of the operation. Unfortunately, I don't have any Microchip MCUs at the moment to be able to check the operation of the library on it.

     
  • Anobium

    Anobium - 2020-10-07

    Thank you. I will update everything.

    Nice video!!!

    Go and get a LGT8FX328P. Then, ping me for the latest RC release... you will then see how fast you can update the TM1637!!!

     
  • Friderik Back

    Friderik Back - 2020-10-07

    One, probably stupid question, what is this LGT8FX328P?

     
  • Friderik Back

    Friderik Back - 2020-12-04

    I'm sorry for late answer, I have a huge lack of free time last weeks.

    Thanks, really very interesting development board.

    About the communication speed between MCU and TM1637 module.

    At the beginning of this discussion, there is a fear that slow communication will cause the display to flicker. This simply is not true. Flickerless operation is guaranteed with the TM1637 chip itself.

    However, this fear probably caused the communication speed in the first version of this library to be set too high (commented out Wait 5 us lines).

    When I started fixing this library, turning on these lines was among the first changes.

    Unfortunately, after a few hours of testing, the TM1637 module failed. I don't know the reason, but I preemptively set an even lower communication speed and it's fine for now, as I don't want to hear complaints that someone has broken the module only by using my library. 

    I think about 100 "FPS" for this LED display (on an Arduino UNO) is quite enough.

     
  • Bonkers

    Bonkers - 2020-12-08

    Hi FB,
    many thanks for this code - works great.
    one point to note is that the TM1637 doesn't work reliably at 4.9V (on my sample of 1) - it still displays, but misses incoming messages, which may be the problem you saw above.
    Your timings are well within chip spec, 290kHz vs ~1MHz.
    It depends also on brightness and number of segments lit, so might be an undervoltage within the IC, or poor decoupling (nope - extra 10uF X7R has no effect - but datasheet asks for 100uF)

    Any chance of a TM1638 driver? - similar part with 8 digits, 8 LEDs, 8 buttons.

    best regds
    Bonkers

     
  • mmotte

    mmotte - 2020-12-08

    @Bonkers
    TM1638 is a different protocol and has 3 comm lines data,clk,stb.

    I have been playing with one and have the 7 seg display working but not the leds nor the pushbuttons.

     
  • Bonkers

    Bonkers - 2020-12-08

    Thanks MM, I will look-up your postings on TM1638, not sure they were there last time i looked.
    Sure, the chip is a different protocol, like a 3-wire SPI, it's not a small variant of TM1637, even if there are many similarities. I bought a few boards, about £3 each if you buy 5, otherwise £5. https://www.banggood.com/5Pcs-TM1638-Chip-Key-LED-Display-Module-8-Bits-Digital-LED-Tube-p-1156507.html

     
  • Friderik Back

    Friderik Back - 2020-12-09

    Thanks Bonkers for your feedback.

    I also noticed the unreliable operation on one of my 0.56" modules. In my case, the problem was a broken/bad GND trace from both headers to chip. Low price - low quality. I fixed it with a wire, it doesn't look nice, but the module now at least works properly.

    I don't have any TM1638 module at the moment. I just wrote a slightly different library for TM1637 modules with the 6 digits, as they have a different layout of digits (and decimal points) than modules with the 4 digit clock display.

    https://sourceforge.net/p/gcbasic/discussion/629990/thread/de80c6381a/

     
  • Bonkers

    Bonkers - 2020-12-09

    No probs FB, difficult to complain about quality when the modules are far cheaper than the parts.
    I don't know if it is the right thing to post the code right here - that ramps brightness up and down, and shows the low-voltage fault? I can do...
    I've now got the 7-segment text running, amazing that we can understand more or less the restricted character set if there are real words in there, I used "the quick brown fox jumps over the lazy dog".
    I note however that the WriteChar routine has a "Bright = 15" instruction, I think its better to comment this out, and let brightness remain as it was - i set it with a dummy Write4Dig command, or ultimately, have an optional parameter passed like with Write4Dig.
    Sounds like I'm complaining about quality of free SW - no, it's great what you've done, much appreciated. I can send you a TM1638 board if you like, as a token of thanks, if you can PM me or something?
    best regds
    bonkers

     
  • Bonkers

    Bonkers - 2020-12-10

    Update: second display works fine down to 4V, so not a problem with the TM1637.
    I'm delighted and amazed that the same code - just changing the processor type (AVR to PIC) and the 2 pins - and it all works first time - that's never happened before. I will be spending some more time with this.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.