Menu

New Lib for TM1637 7-Segment Display driver

ToniG
2022-08-23
2022-11-25
  • ToniG

    ToniG - 2022-08-23

    TM1637 is great for where there is a need for buttons & LED display & fewer pins on the micro.
    I needed to read buttons & display more than 1 value, display long strings, flash some digits etc.
    It was not feasible to build upon the existing lib(s) as I needed a more modular code structure.
    The library is targeted to OEM display boards(ones we make), OTS 4d & 6d hobby modules are supported.
    There are 3 sections to the library, 1. Command lib, 2. Message processor, 3. Hardware driver. Initially it was one big file but separate .h files made it more manageable.
    Commands are easy to use & many parameters are default set.
    Most settings & config. are also default value, only need to re-set if required to enable flexible/advanced use. Making it easy to get started or for basic use..
    As the TM1637 digit registers are write only, there is a display buffer to set & manipulate, then send to disp.
    Tested with PIC12F683 all commands & some combined ok. I could display a scrolling string with length up to the compiler length error The demo runs on a PIC16F1829.

    This version is alpha & for Test/Comment/Review. There may be remnants of testing still in the code.
    If someone has a TM1637 module it would be great to test some other (mega2560 etc...)

    Features...
    • Display size from 1 to 6 digits.
    • Values can display as decimal 0 - 999999 or Hex 0 - FFFFFF (0d16777215).
    • Value can be displayed at any digit position.
    • More than 1 value can be displayed simultaneously.(eg. 3 hex bytes dot separated)
    • Read key buttons.
    • Option for digit remap to correct swapped com pins of 6d hobby disp modules.
    • Option to remap button number per datasheet (weird sequence).
    • Uses a buffer to reflect TM digit registers.
    • Additional user buffers enabled.
    • Zero blanking on/off (can control number of leading zeros).
    • Direct writing a byte to display digit.(bypass buffer)
    • Display text with scrolling for long strings.
    • Scroll array of raw digit values.
    • Flash display, digits or segment.
    • Linear bar-graph up to 16 LED's (with fill/no-fill)
    • Use sequential or fixed register addressing mode.
    • Fixed 2 existing issues in existing libs with ACK (1. both devices driving DIO pin for half clock & 2. NACK disable latched).

    I made an ACP file (append it to existing greatcowbasic.acp) that I found useful when I had memory leaks.
    There is a vid here: https://youtu.be/Ng-FH4Exrts

    Will have a help doc soon...

     
  • Anobium

    Anobium - 2022-08-23

    Very nice video.

    The functionality looks to be very good.

    Do you want me to wait, until a later version, for a code review?

     
  • ToniG

    ToniG - 2022-08-23

    It is final from me at this point, review this version.
    I am interested if someone can try it & feedback if somethings not working right or "a better way to do this".


    I had a bit of luck with the ACK code. I wondered how I was going to test for NACK when later I started to get display anomalies after changing chips & compiler version, thinking that was my issue, then I looked with the scope & saw the problem, I left the logic analyzer connected but it wasn't powered! (PIC has more drive pin current on DIO & TM couldn't assert the ACK properly.)
    Anyway this helped test the NACK recovery.

     
  • Anobium

    Anobium - 2022-08-23

    Code looks good.

    Here is a report of your constants(symbols). This may give you some insights. The numbers on the right are the file handle numbers that added the symbol.

    CODE/AddSymbol:     TM1637_CLK                                  PORTB.4                             1
    CODE/AddSymbol:     TM1637_DIO                                  PORTB.5                             1
    CODE/AddSymbol:     TM_DISPLEN                                  6                                   1
    CODE/AddSymbol:     KEYMAP                                      BUTNMAP1                            2
    CODE/CurrentValues: TM_DISPLEN                                  6                                   1
    4 Constant: TM_DISPLEN now '6' tobe '6'   ;?F2L84S0I84?
    CODE/AddSymbol:     TM_LEDS                                     0                                   2
    CODE/AddSymbol:     TBLPTR0                                     0                                   2
    CODE/AddSymbol:     TBLPTR1                                     26                                  2
    CODE/AddSymbol:     TBLPTR2                                     52                                  2
    CODE/AddSymbol:     TBLPTR3                                     62                                  2
    CODE/AddSymbol:     TMDLY                                       10                                  3
    CODE/AddSymbol:     TMCMD0                                      128                                 3
    CODE/AddSymbol:     TMCMD1                                      64                                  3
    CODE/AddSymbol:     TMCMD2                                      68                                  3
    CODE/AddSymbol:     TMCMD3                                      66                                  3
    CODE/AddSymbol:     TMADDR                                      192                                 3
    

    You can also get this report of Conditional Statements. This also may confirm your thinking.

    This is line number in your source, the statement and comments. The #ENDIF is not shown,.

                  91: #IFDEF TM1637_DIO
                  ;Dir TM1637_DIO Out
                  92: DIR TM1637_DIO OUT
    
                  94: #IFDEF TM1637_CLK
                  ;Dir TM1637_CLK Out
                  95: DIR TM1637_CLK OUT
    
                  56: #IFNDEF SNDMODE_SEQ
                  ;Nack_Rst
                  57: NACK_RST
                  ;TM1637_Start
                  58: TM1637_START
                  ;TM1637_WrVal (TMcmd2) : TM1637_Ack
                  59: TM1637_WRVAL (TMCMD2)
                  ;TM1637_WrVal (TMcmd2) : TM1637_Ack
                  59: TM1637_ACK
                  ;TM1637_Stop
                  60: TM1637_STOP
                  ;For TMlp2 = Dig_pos - (Num_Digs - 1)  to  Dig_pos
                  61: FOR TMLP2 = DIG_POS - (NUM_DIGS - 1) TO DIG_POS
                  ;If TM_6dReMap = 1 then ReadTable Digit_ReMap, TMlp2, TM_lp2 'reMap_Addr
                  62: IF TM_6DREMAP = 1 THEN
                  ;If TM_6dReMap = 1 then ReadTable Digit_ReMap, TMlp2, TM_lp2 'reMap_Addr
                  62: READTABLE DIGIT_REMAP, TMLP2, TM_LP2
                  ;If TM_6dReMap = 1 then ReadTable Digit_ReMap, TMlp2, TM_lp2 'reMap_Addr
                  62: END IF
                  ;TM1637_Start
                  63: TM1637_START
                  ;TM1637_WrVal (TMaddr + (TM_lp2 -1)) : TM1637_Ack  ' Digit addr
                  64: TM1637_WRVAL (TMADDR + (TM_LP2 -1))
                  ;TM1637_WrVal (TMaddr + (TM_lp2 -1)) : TM1637_Ack  ' Digit addr
                  64: TM1637_ACK
                  ;TM1637_WrVal (DBuf(TMlp2)) : TM1637_Ack          ' Digit data
                  65: TM1637_WRVAL (DBUF(TMLP2))
                  ;TM1637_WrVal (DBuf(TMlp2)) : TM1637_Ack          ' Digit data
                  65: TM1637_ACK
                  ;TM1637_Stop
                  66: TM1637_STOP
                  ;Next
                  67: NEXT
                  ;tmCtrlSnd
                  68: TMCTRLSND
    

    This debug capability is in the new release of the compiler. All intended to assist developers.

     
  • ToniG

    ToniG - 2022-08-23

    Its nice that the re-define shows in the report?
    Defined in include as default, & defined in program as (potentially) new value.

    CODE/CurrentValues: TM_DISPLEN 6 1
    4 Constant: TM_DISPLEN now '6' tobe '6' ;?F2L84S0I84?

    I wonder if sub call depth could be reported in debug (just wishing)

     
    • Anobium

      Anobium - 2022-08-23

      The attempted reDEFINE is nice, but, not formatted correctly. I see that.
      Showing reDEFINE is very useful to know where and what it is being set to.

      ?F2L84S0I84? - the internal tracker of each line of code. Sometimes you see this in the ASM when the compiler has failed to convert to pseudo code and/or ASM.

      • F2 File number 2 - at some point we could change to the filename
      • L84 Line number in the file
      • S0 - Method number. An index that increments every time the compiler finds a method.
      • I84 - Line number in the all of the source files.

      Sub call depth... not when this report is executed. We would have to report from the Assembler. At that point the optimisation would have happened with the replacement of CALLs with BRAnches etc. So, you could potentially write an analyser to walk the ASM to calc the stack usage.

       

      Last edit: Anobium 2022-08-23
  • ToniG

    ToniG - 2022-08-23

    Help reference attached.

     

    Last edit: ToniG 2022-08-23
  • Anobium

    Anobium - 2022-08-23

    I have a board with a TM1638 - this has the extra connection for STB.

    Will this work ?

     
    • mmotte

      mmotte - 2022-08-23
       
      • Anobium

        Anobium - 2022-08-23

        Aha... that code is good to put into a release? I did not move it out.. so, the .h is not in the release or your demo.

        :-(

         
  • ToniG

    ToniG - 2022-08-23

    I started with the TM1638, I wanted to see scrolling text on 10 LED digits, & display 40bit hex,
    but had a 5m flood & another La Nina summer coming... I'm still getting the water out of the tractor hydraulics.

     
  • Anobium

    Anobium - 2022-08-24

    I just tried the TM1638 code. Too many issues to resolve. I will look another time,

     
  • ToniG

    ToniG - 2022-08-28

    TM1638.
    I ported? the TM1637_OEM_lib to TM1638, was simpler than I assumed
    1. Change all Start/Stop to Strobe line Low/Hi.
    2. Delete ACK code.
    3. Digit address set to even registers. (digit-1)*2
    4. a few tweaks & limitations.

    That was the easy part, but I could not get it working properly, data message looked good but display was garbled, on/off & bright were working ok.
    I made a test program to fill successive 16 registers with bytes & bits with 400ms waits to observe, it looks like the TM1638 board I have is wired stupid.
    Each bit of register 0 sets segment "a" of each digit, register 2 sets segment "b" of each digit etc. , successive even address are segments not digits. (odd address registers are not connected)
    https://youtu.be/FOhTs2_JT_A

    The datasheet show it is possible to reverse the Segment_Digit Common to accommodate CC or CA displays but I'm sure it requires a remap subroutine to encode the data <--> address for this. I would rather de-solder & rewire the display for test at this moment. Ordered some different boards so a few weeks...
    My board is the one with no discrete LED's "QYF-TM1638"
    For reference I tried mmotte's tm1638.h & it is same garble.

     

    Last edit: ToniG 2022-08-28
  • Anobium

    Anobium - 2022-08-28

    Interesting state of affairs. :-)

    I should hold off testing?

     
  • ToniG

    ToniG - 2022-08-28

    No, this is a different device TM1638 lib, submitted the TM1637 lib for review ,
    I just updated as you mentioned...

    I just tried the TM1638 code. Too many issues to resolve. I will look another time

    I assumed you tried to re-code my lib for TM1638 (I should have asked for clarification)
    I will start a new discussion topic "TM1638_OEM_ Lib" when I get new info.

     
    • Anobium

      Anobium - 2022-08-28

      Yes, I tried the new TM1637 lib - no action on a TM1638 board.
      I get action when using mmotte's tm1638.h

       
  • ToniG

    ToniG - 2022-08-28

    Yes, I tried the new TM1637 lib - no action on a TM1638 board

    That is because the new TM1637 lib is for TM1637 & not a TM1638 board.
    TM1637 lib is for TM1637 boards only.

    TM1637 - 2 wire I2C like interface CLK DIO
    TM1638 - 3 wire SPI like interface STB CLK DIO

     
  • ToniG

    ToniG - 2022-08-29

    Ok, Back to the TM1637 lib submitted for review.
    There is a line of code missing in Sub tmSndBuf. - " TM_lp2 = TMlp2 '< If not remap"

    I noticed it when I set Var TM_6dReMap = Off (off = correctly wired displays = not 6digit hobby modules)

     
    • Anobium

      Anobium - 2022-08-29

      oh.. :-) Do you have a new complete file?

       
  • ToniG

    ToniG - 2022-08-29

    Don't think its ready for commit to release yet, #includes are still local, option explicit still active, ...

    @ToniG "This version is alpha & for Test/Comment/Review"

    #Include "TM1638_OEM_Cmd_Lib.h"
    #Include "TM1638_OEM_Message.h"
    #Include "TM1637_HW_Driver.h"
    # OPTION explicit  '< while in dev...
    
     
  • ToniG

    ToniG - 2022-11-25

    Apologies, I was supposed to updated this post a while ago...
    Attached zip with latest files for reference. (the latest GCB release will include these)

     

Log in to post a comment.