• TM1637 - display module include example

    Hi guys ,

    have had a need to use the TM1637 4 digit display module

    please refer to link for the display module circuit and display unit used

    http://www.picbasic.co.uk/forum/showthread.php?t=23968

    Thanks to richard for help on the start/stop/ write timing section

    hope it of use to save some time when using this module


    cheers

    Sheldon

    Code:
    '*  Version : 0.0                                                              *
    '*  Notes   : Driver Libary for TM1637 chip                                    *
    '*          : For use on 4 digit with centre colon only                        *
    '*          : Note: Driver does not support the keyscan option                 *
    '*******************************************************************************
    ' Description:                                                                 *
    '             This include supports 4 digits, no Decimal Points on each digit  *
    '             and 1 colon in centre of 4 digits only                           *
    '             No keyscan support setup on the TM1637 fuctions                  *
    '                                                                              *
    ' Operation:                                                                   *
    '             min voltage for Red, Yellow LED digits is 3.3 volt               *
    '             min voltage for Green , Blue LED digits is 5v                    *
    '             min clock width = 400ns                                          *
    '             min data setup = 100ns + hold =100ns                             *
    '             The Clock and Data Pin have pull up 4k7 resitor,                 *
    '             with a 100pf cap to ground to reduce interfance buit into module *
    '             The 2 colon led segments are connected to anode Com2,cath= seg8  *
    '                                                                              *
    '  Features :                                                                  *
    '            Use either raw segment values or decimal numbers                  *
    '            (with and without leading zero)                                   *
    '            Set either the whole display or any digit independently           *
    '            Control the brightness                                            *
    '            Test display segments display                                     *
    '=============================================================================='
    '           TM1637 4 DIGIT COMMON ANODE - 1 COLON DISPLAY MODULE               *
    '                                                                              *
    '          Note: COLON = Grid2 - seg8(dp)- No other DP on this display         *
    '       -----------------------------------------       DISPLAY FACING         *
    '       |                                        |                             *
    '       |   ______   _____     _____    _____    |                             *
    '       |  |  A  |  |     |   |     |  |     |   |                             *
    '       |  |F   B|  |     |   |     |  |     |  0 ----- CLK                    *
    '       |  |_____|  |_____| O |_____|  |_____|  o ----- DIO                    *
    '       |  |  G  |  |     |   |     |  |     |  0 ----- VCC                    *
    '       |  |E   C|  |     | O |     |  |     |  0 ----- GND                    *
    '       |  |__D__|  |_____|   |_____|  |_____|   |                             *
    '       |   dig1     dig2      dig3     dig4     |                             *
    '       |   grid1    grid2     grid3    grid4    |                             *
    '       -----------------------------------------                              *
    '                                                                              *
    ' =============================================================================*
    ' Command byte information TM1637                                              *
    ' Note: Bit5= 0 ,bit4 = 0 always                                               *
    '    bit7 : Bit6                                                               *
    '     0   :  1    = Data Command                                               *
    '     1   :  0    = Display control Command                                    *
    '     1   :  1    = Address command                                            '   
    '  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *
    '  Data Command table ( bit7 = 1 , bit6 = 0 , bit5=0,bit4 =0) Hex value $4x    *
    '                                                                              *
    ' Bit1:Bit0                                                                    *
    '  0 : 0   = Write Data to display                                             *
    '  1 : 0   = Read key scan                                                     *
    '  0 : 1   = reserved                                                          *
    '  1 : 1   = reserved                                                          *
    ' Bit2  0 = Auto Address Adding , 1= Fixed Address                             *
    ' Bit3  0 = Normal Mode  1 = Test Mode                                         *
    ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =* 
    '  Display Control Commnad table ( bit7=1 , Bit6 =0)  hex value $8x            *
    '  Bit2:Bit1:Bit0 - display brightness control level 0 = low , level 7 = high  *
    '   0  : 0  : 0   Display Brightness  1/16 - level 0                           *
    '   0  : 0  : 1   Display Brightness  2/16 - level 1                           *
    '   0  : 1  : 0   Display Brightness  4/16 - level 2                           *
    '   0  : 1  : 1   Display Brightness 10/16 - level 3                           *
    '   1  : 0  : 0   Display Brightness 11/16 - level 4                           *
    '   1  : 0  : 1   Display Brightness 12/16 - level 5                           *
    '   1  : 1  : 0   Display Brightness 13/16 - level 6                           *
    '   1  : 1  : 1   Display Brightness 14/16 - level 7                           *
    '                                                                              *
    ' Bit3 0 = Display Off 1 = Display On                                          *
    '                                                                              *
    ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*  
    '  Grid Segment Address Command Table ( Bit7= 1 ,Bit6 = 1 ) Hex Value = $Cx    *
    '  Display Adreess Registers                                                   *
    '  Bit3-Bit0  $C0 = Grid1(dig1), $C1 = Grid2(dig2+colon) ,$C2=Grid3 ,$C3=Grid4 *
    '                                                                              *
    ' Eech bit for each Grid byte controls a digit led Segment                     *
    ' Bit7=Seg8(DP,COlON),Bit6=Seg7(led-G),Bit5=Seg6(led-F),Bit4=Seg5(led-E)       *
    ' Bit3=Seg3(led-D),Bit2=Seg2(led-C),Bit1=Seg1(led-B),Bit0=Seg1(led-A)          *
    '                                                                              *
    '==============================================================================*
    
    TM_DIO_OUT     var  LATB.2          ' Data Pin OUT TM1637 include - 4dig 7seg Display Module  
    TM_DIO_IN      VAR  PORTB.2         ' Data Pin In  ( not used )
    TM_CLK         var  LATB.3          ' Clock Pin- set in TM1637 include - 4dig 7seg Display Module 
    DIR_TM_DIO     var  TRISB.2         ' Direction control for tm_dio pin  0 = out  1 = in
    
    
    TM1637_WR          con $40          ' write data command , with auto Address adding , display mode = normal 
    TM1637_WR_fixed    con $44          ' write data command , with fixed Address , display mode = normal
    Cmd_Dig_Seg        con $C0          ' Grid Segment Address Command $C0 - $C3 used on this module 
    TM1637_Dsp_CTL     con $80          ' display bright control command , bit3-0=display off 1= on ,bit2-0 brightness 0 - low 7 = high  
    
    Dig_7seg      var byte[4]           ' 4 byte array byte for each digit in use  
    TM1637_Data   var byte              ' value for each digit / segment in send command  
    YY            var byte              ' geneeral loop varable 
    g_num         var word              ' reqested number or chr to be dipslayed 
    TM1637_Bright var byte              ' display brightness value (0-7) 0=low to 7=high brighness 
    Colon_On      var bit               ' select if the colon segment is on/off 
    TM1637_On     var bit               ' select display on / off bit for command3  
    R_Justfy      var bit               ' set leading 0 if digit value 0 displayed over the digits  
    
    
     DIR_TM_DIO = 0     ' set direction output as start 
     TM_CLK     = 1     ' SET HIGH AT START   - modual has pullups 
     TM_DIO_out = 1      ' set high at start - module has pullups 
    
    goto Jumpover_TM1637
    '======================================
    
    TM1637_Startup:
    ' Command to do test pattern at device starup
    '---- each digit turn on in seq, all digits 
        
          Dig_7seg(0) = 0
          Dig_7seg(1) = 0 
          Dig_7seg(2) = 0 
          Dig_7seg(3) = 0 
          TM1637_on = 1 
          for g_num = 0 to 3 
             Dig_7seg(g_num) = $FF 
              TM1637_On = 1 
              TM1637_Bright = 7
             gosub WR_4dig 
             pause 200
          next g_num
          gosub TM1637_flash   ' flash the display 3 times 
          gosub TM1637_off     ' turn off display 
     return 
    
    
    ' -------------------------------
    TM1637_flash:
    ' flash the display 3 times with current values and brightness 
    ' if display was off prior to call , will leave it off after , visa versa if display was on prior      
         for g_num = 0 to 5         ' flash display on / off 3 times
              toggle TM1637_On  
              TM1637_Bright = 7
              gosub WR_Disp_Command3 
              pause 300
         next g_num    
    return
    
    '---------------------------------------------------------
       TM1637_Off:
     ' turn off the tm1637 display 
     
       TM1637_On = 0           ' set bit to 0 turn off display
       gosub WR_Disp_Command3  ' send the commands to display, other values dont care as display tuned off 
     return  
    '--------------------------------------  
    
     WR_4dig:
    ' assumes each digit data 7 segment info varable ( dig1_7seg,dig2_7seg, dig3_7seg dig4_7seg ) 
    ' assumes brightness setting value for command3  ( tm1637_bright) 
    ' assumes colon bit,TM1637_ON Bit , Dig_7seg(0-3) value 
    
    
     '     TM1637_On = 1
     '     Colon_On = 1
     '     TM1637_Bright = 7
    
        gosub Start_data                               ' start seq
        TM1637_Data =  TM1637_WR                       ' set write data command1 , with auto Address increment , display mode = normal 
        Gosub Write_Tm1637                             ' write to module 
        gosub Stop_data                                ' stop seq after data command1 
        
        gosub Start_data                               ' start seq for command2  
        TM1637_Data = Cmd_Dig_Seg                      ' grid1 start address for auto increment  
        Gosub Write_Tm1637                             ' write to module 
        
        TM1637_Data = Dig_7seg(3)                      ' digit 4 Data on grid1  
        Gosub Write_Tm1637                             ' write to module 
        
        TM1637_Data = Dig_7seg(2)                      ' digit 3 Data on Grid2 
        TM1637_Data = TM1637_Data + (Colon_On << 7)    ' add colon bit setting  (bit7) to  digit2 data for grid2  
        Gosub Write_Tm1637                             ' write to module 
        
        TM1637_Data = Dig_7Seg(1)                      ' digit 2 data for grid3  
        Gosub Write_Tm1637                             ' write to module  
        
        TM1637_Data = Dig_7Seg(0)                      ' digit 1 data for grid4 
        Gosub Write_Tm1637                             ' write to module 
        Gosub Stop_data                                ' send stop seq to show end of address data 
    
    WR_Disp_Command3:                                  ' Label to write display command3 only  , assumes dig_7seg(0-3)+ colon setting, brightness , display on /off setting  
        gosub Start_data                               ' start seq  for command 3 to start 
        TM1637_Data = TM1637_Dsp_CTL                   ' build display command3 byte
        TM1637_Data = TM1637_Data +(TM1637_On <<3)     ' add display on/off bit setting(bit3)to command3  
        TM1637_Data = TM1637_Data + TM1637_Bright      ' add display brighness value ( 0-7 ) to byte command3
        Gosub Write_Tm1637                             ' write to module 
        gosub Stop_data                                ' stop seq 
        
      return 
    
     
    '-----------------------------------------------------------------------------
    ' digits placed and display left to right with 4th digit first 1digit last 
    ' routine can set all 4 values
    ' assumes R_Justfy value, g_num  
     Put_4dig:
        TM1637_Data = g_num DIG 3            ' get 4th digit value into temp varable 
        gosub lookup_dig                        ' convert value to 7seg display value 
        Dig_7seg(3) = TM1637_Data               ' place 7seg value into digit1 including "0" 
        if TM1637_Data = $3F then               ' if it 7seg "0" then 
           if R_Justfy = 0 then Dig_7seg(3) = 0 ' if flag not set dont show digit   
        endif       
    
     Put_3dig:
        TM1637_Data = g_num DIG 2            ' get 3rd digit value into temp varable 
        gosub lookup_dig                        ' convert value to 7seg display value 
        Dig_7seg(2) = TM1637_Data               ' place 7seg value into digit1 including "0" 
        if TM1637_Data = $3F then               ' if it 7seg "0" then 
           if R_Justfy = 0 then Dig_7seg(2) = 0 ' if flag not set dont show digit   
        endif       
     
     Put_2dig:
        TM1637_Data = g_num DIG 1           ' get 2nd digit value into temp varable 
        gosub lookup_dig                        ' convert value to 7seg display value 
        Dig_7seg(1) = TM1637_Data               ' place 7seg value into digit1 including "0" 
        if TM1637_Data = $3F then               ' if it 7seg "0" then 
           if R_Justfy = 0 then Dig_7seg(1) = 0 ' if flag not set dont show digit   
        endif       
     
     Put_1dig:
        TM1637_Data = g_num DIG 0            ' get 1st digit value into temp varable 
        gosub lookup_dig                        ' convert value to 7seg display value 
        Dig_7seg(0) = TM1637_Data               ' place 7seg value into digit1 including "0" 
       
     return 
    '------------------------------------------------------------------------------
    lookup_dig: 
    ' converts selected dec value to 7seg display value 
    ' digits display      0   1   2   3   4   5   6   7   8   9   
     lookup TM1637_Data,[$3F,$06,$5B,$4F,$66,$6D,$7D,$07,$7F,$6F],TM1637_Data
    
    return
    
    '-----------------------------------------------------------------------------
    test_txt:
    
    ' place "CH:" txt and current Rf CH value onto display ,
    ' use R_justfy leading 0 for 2 digits   
       Dig_7seg(3) = $39   '"C"
       Dig_7seg(2) = $76   '"H"
       Colon_On = 1        '":"
      g_num  = 1    ' get the current RF CH value 
       R_Justfy = 1        ' set leading 0 if single digit 
       gosub put_2dig      ' put 2 digits values for location dig3, dig4
       
       TM1637_Bright = 7   ' Set display  to full brightness  - value (0-7) 0=low to 7=high brighness 
       TM1637_On  = 1      ' select display on in command3 
       gosub WR_4DIG       ' write the values to display
    return
    
     '--------------------------------------------
    ' signal requirments at start of command 
     Start_data:
       tm_CLK = 1
       tm_DIO_out = 1 
    @ NOP   
       Tm_DIO_out = 0
    @ NOP
       tm_CLK = 0
     return
    '------------------------------------
    ' signal requirement after command is sent 
     Stop_data:
       tm_CLK = 0
       tm_DIO_out = 0
       tm_CLK = 1
    @ NOP
       tm_DIO_out = 1
    @ NOP 
     return
    
    '--------------------------------------------
    Write_TM1637:
    ' assumes TM1637_Data 
    ' commands/ data  sent LSbit first    
          yy= 0
          for yy = 0 to 7                   ' send 8 bits of data 
             tm_CLK = 0  
             tm_dio_out = TM1637_Data.0(yy)
             tm_CLK = 1 
    @ NOP
          Next yy
          tm_CLK = 0                 ' clock in data to look for the Ack from TM1637 
          DIR_TM_DIO = 1             ' set Data pin direction to input , release  
          tm_CLK = 1                 ' clock in ACK data on rising edge from TM1637 
    @ NOP                            ; allow time for input of ACK 
    @ NOP
    @ NOP
          tm_CLK = 0                 ' release DIO on oomplete 9th clock 
    @ NOP 
          DIR_TM_DIO = 0             ' set Data pin direction to output
        
     return
     
     '----------------
    'show_number:
    'send number example 
         g_num = 32
         R_Justfy = 1      ' place leading 0 in display 
         gosub Put_4dig    ' place 4 digit values 
         
         TM1637_On = 1
         Colon_On = 0
         TM1637_Bright = 7
         gosub WR_4dig 
    
    
    'return
    '-------------------------------------
     
     
     
     
     
    '==========================================================================
    
     Jumpover_TM1637:    'Jumpover code for PBP compile requirements
    This article was originally published in forum thread: TM1637 - display module include example started by longpole001 View original post