TM1638 LED Display Driver/Controller


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2019
    Posts
    20

    Default TM1638 LED Display Driver/Controller

    My last forum code example contribution was about a year ago, and provided sample code for the TM1637 7-Segment LED driver/controller. The TM1637 is used on 4-digit 7-Segment display modules available from a variety of Asian sources online. Two module varieties are available -- 7-Segment with decimal point and 7-Segment with semicolon. The semicolon version is commonly found as the display on digital clocks and microwave ovens. My interest in this display module was its ideal suitability for displaying the revolutions per minute (RPM) for a small engine tachometer device I designed. Having completed this device, I began experimenting with other segment type displays and with the keyboard scan function available on the TM1637. Thereafter, I posted a code sample for the TM1637 4 digit display module and 16 button keypad that provided the primitive task of displaying the key number 01 through 16 on the 4-digit display.

    Curiosity led me to acquire some keyboard/display modules, including the "LED&KEY" and the "HCW-132", both easily found and acquired from several online sources. Both of these modules use the TM1638 LED driver/controller instead of the TM1637. While similar, the TM1638 differs in several respects:
    - the TM1638 uses a 3-wire interface (similar to SPI) featuring clock, data and strobe signal (the TM1637 uses a 2-wire interface for clock and data)
    - the TM1638 accommodates up to 8 digits, while the TM1637 is limited to 6 digits
    - the TM1638 is compatible with either common anode or common cathode 7-segment displays, while the TM1637 is common anode only
    - the TM1638 accommodates up to 10 segments/digit while the TM1637 accommodates up to 8 segments/digit
    - the TM1638 accommodates up to 24 input keys, while the TM1637 accommodates up to 16 input keys.

    The first of the two keyboard/display modules, the "LED&KEY" is relatively straight forward inasmuch as its 7-Segment displays are common anode wired. The module (as shown is the picture) has 8 push-button keys, 8 7-segment digits (plus decimal point), and 8 discrete LEDs. This array of features makes it potentially useful in practicable applications, and not just as an experimenter's learning or interface device.

    The code sample provided here is fairly primitive in exercising each of the discrete LEDs, displaying the hex digits 0-9 and A-F, and then waiting for a key press. In response to a key closure, the corresponding 7-segment display shows the key number 1-8, and the discrete LED flashes the corresponding number of times. Pressing the 5th key, for example, displays "5" on the fifth digit location and causes the discrete LED to flash 5 times. If the key is held closed, the display action loops continuously.

    The code is copiously commented and hopefully is clear in describing how characters are written to the 7-segment digits, and how the keypad is scanned. Further explanation of the sample code structure is included in the introductory header comments. A thorough review of the TM1638 data sheet is essential.

    I've interfaced the LED&KEY module with the Microchip Curiosity Board equipped with a PIC16F18346 uC. Your development platform is probably different, so change the configuration "INCLUDE" file at the beginning of the code, and re-specify the port pin assignments as necessary.

    The other module, the "HCW-132" is an entirely different creature in that its 7-segment displays are wired in common cathode mode. This variation transposes the digit positions 1-8 with the segment addresses 1-7 (not including the decimal point). I've written a sample code for this module also and will post it in a future contribution to this forum.

    As always, comments, corrections and criticisms are welcome!
    Name:  LEDKEY.jpg
Views: 1462
Size:  46.3 KB
    Code:
    '********************************************************************
    '*  Name    : TM1638_8Digits_8Keys_8LEDs.PBP                        *
    '*  Author  : Roberts                                               *
    '*  Notice  : Hack it as you wish                                   *
    '*          :                                                       *
    '*  Date    : 10/20/2020                                            *
    '*  Version : 1.0                                                   *
    '*  Notes   : Demo program for TM1638 "LED&KEY" PCB that has 8      *
    '*          : 7-segment LED digits, 8 pushbutton switchs and 8      *
    '*          : discrete LEDs.                                        *
    '*          : THIS VERSION FOR MICROCHIP CURIOSITY BOARD PIC16F18346*
    '*          :                                                       *
    '********************************************************************
    ' This is a primitive demo program that exercises the functions of the TM1638 7-Segment LED driver IC as it is
    ' used in a "LED&KEY" experimenter's printed circuit board (available from many online sources). The LED&KEY PCB
    ' features 8 7-segment display characters, 8 discrete LEDs, and 8 pushbutton switches, and the TM1638 controller.  This
    ' PCB interfaces a microcontroller with a 3-wire (data, clock, and strobe) interface, and resembles industry standard
    ' SPI so that PIC Basic's SHIFTIN and SHIFTOUT commands are usable.
    '
    ' The TM1638 data sheet provides the essential information for commands, addressing, and timing.  Experimentation with 
    ' PIC Basic's modes resulted in finding mode 0 is optimal for SHIFTOUT and mode 1 is optimal for SHIFTIN.
    '
    ' The demo program is segmented in two parts:
    '
    ' "MAIN" demonstates sending LED ON/OFF states and characters to the LED&KEY.  Specifically, the program starts by
    ' clearing the TM1638 memory by sending "-" characters via the Subroutine "CLEARALL". Then, each of the 8 LED's is
    ' individually turned on for 1/2 second in sequence.  Then, the digits 0 thru 7 are sent to the display and cleared.
    ' Then, the digits 8 through F are sent to the display and cleared.
    '
    ' "READKEY" demonstrates use of the 8 keys.  The LED&KEY is cleared so that the display shows all "-" characters.
    ' Pressing any key causes that key's number ("5" for example) to appear on the corresponding display location, while 
    ' the corresponding discrete LED flashes that number of times. Pressing the key momentarily induces one such display
    ' and flash sequence, while holding the key depressed induces looping digit and flashing.
    '
    ' The TM1638 spec sheet indicates that combination key presses are not possible with keys wired in the manner as they
    ' are on the LED&KEY board (e.g., all share "K3").
    '
    ' The LED&KEY device resembles the TM1638 spec sheet's schematic on page 13 (Section XI), but without the 16 switches
    ' connected to the TM1638's K1 and K2 pins, and with 8 discrete LEDs wired in common anode per TM1638 Figure(8).
    '
    ' The TM1638 is designed to accommodate 7-segment LED displays that are wired as common anode or common cathode. This
    ' LED&KEY device has its 7-segment displays wired in common cathode.
    '
    ' The 8 7-segment characters thus appear at 8 address locations (each address designating a Grid#) each with 7 data 
    ' bits (8 including the decimal designated by SEG1-SEG8) while the 8 LEDs appear at 8 address locations (each address  
    ' designating a Grid#) with 1 data bit designated by the value of SEG9.
    '
    ' There are some similar TM1638 PCBs that have bi-color LEDs that use the capability of SEG10.  Thus the LED color can
    ' be altered by the value of the data bits set for SEG9 and Seg10.  This demo code does not accommodate bi-color LEDs
    ' but can be easily modified to do so.
    ' 
    ' A THORUGH READING AND COMPREHENSION OF THE TM1638 DATA SHEET IS HELPFUL!
    '
    ' Below are the hexadecimal segment codes corresponding to decimal values
    '  0     1     2     3     4     5     6     7     8     9       
    ' 0x3F  0x06  0x5B  0x4F  0x66  0x6D  0x7D  0x07  0x7F  0x67
    '
    '********************************Declare Variables and Constants**********************************************
    '
    ' Place the PIC initialization INCLUDE file here (See PBP PIC Configuration Files)  
    INCLUDE "PIC16F18346_Initialize.pbp"
    '
    ANSELB = 000000               'Port B is used for all interface pins
    TRISB = 000000
    
    SDA Var PORTB.4                  'Serial data port pin (bi-directional)
    SCK Var PORTB.6                  'Serial clock port pin (uC output)
    STB Var LATB.5                   'Chip Select/Stobe (uC output/active low)
    
    HIGH SDA                         'Initialize Data Output
    HIGH STB                         'Initialize Strobe Output
    
    i VAR BYTE                       'loop variable for multiple use
    j VAR BYTE                       'loop variable for multiple use
    
                                     'These are 3 of the 4 commands used by the TM1638 controller: ($40 not used)
    CMD1 CON $44                     'CMD1=$44 is the write command for fixed addressing
    CMD2 CON $42                     'CMD2=$42 is the read key matrix command
    CMD3 CON $89                     'CMD3=$89 is the end command that turns the display on and sets brightness at 2/16
                                     '(CMD3=$8F for max brightness - see TM1638 spec sheet)
                                             
    B1 VAR BYTE                      'B1 is the first byte value from the keypad
    B2 VAR BYTE                      'B2 is the second byte value from the keypad
    B3 VAR BYTE                      'B3 is the third byte value from the keypad
    B4 VAR BYTE                      'B4 is the fourth byte value from the keypad
    KEYNO VAR BYTE                   'KEYNO is the key number or key number hex code                                         
    
    '                                                                                       
    '*************************************************** MAIN *********************************************************
    
    GOSUB CLEARALL                                      'Clear the display with all "-" characters
    
    MAIN:                                               'This rountine flashes each LED and sends hex digits to the display
    'This section activates each discrete LED sequentially for 1/2 second
    
      For i = $C1 to $CF Step 2                         'LEDs are located at $Cn where "n" are odd values 1 thru F
        LOW STB                                         'Turn on/off each LED in sequence one time
          SHIFTOUT SDA, SCK, 0, [i,$01]                 'Turn on the LED for 1/2 second
        PAUSE 500                                       
        HIGH STB
        PAUSE 1
        LOW STB
          SHIFTOUT SDA, SCK, 0, [i,$00]                 'Turn off the LED 
        HIGH STB
        PAUSE 5
      Next i                                            'Sequence thru all 8 LEDs
    
    'This loop sends characters 0 thru 7 to the 8 digit display    
      For i = 0 to 7                                  
        LOOKUP i,[$3F,$06,$5B,$4F,$66,$6D,$7D,$07],j    'Lookup the hex code for each display digit
        Low STB
          SHIFTOUT SDA, SCK, 0, [$C0 + 2*i,j]           'Send even number addresses for 7-segment digits & output digit
        High STB
        PAUSE 500                                       'Pause 1/2 second
      Next i
    
    GOSUB CLEARALL                                      'Clear the display to all "-" characters
    
    'This loop sends characters 8 thru F to the 8 digit display    
      For i = 0 to 7
        LOOKUP i,[$7F,$67,$77,$7C,$39,$5E,$79,$71],j    'Lookup the hex code for each display digit                                  
        LOW STB
          SHIFTOUT SDA, SCK, 0, [$C0 + 2*i,j]           'Send even number addresses for 7-segment digits & output digit
        HIGH STB
        PAUSE 500                                       'Pause 1/2 second
      Next i
    
    GOSUB CLEARALL                                      'Clear the display to all "-" characters
             
    'The following section of code reads the keypad byte. If no key is pressed the KEYNO byte default value is $00
    'Delete this section of code if keypad reading is not required         
    '*****************************************************************************************************************         
    
    READKEY:                                                'READKEY processes only single key inputs
        'Send the key read command and shiftin the 4 key matrix bytes
        LOW STB    
          SHIFTOUT SDA, SCK, 0, [CMD2]                      'Send the Read keypad command CMD2 = $42
        PAUSEUS 10
          SHIFTIN SDA, SCK, 1, [B1,B2,B3,B4]                'Shiftin all 4 keypad bytes   
        PAUSEUS 1                                           
        HIGH STB
        PAUSE 50                                            'Delay to allow for key release
        
        'Shift each byte left incrementally so all key codes appear in a single byte                                             
        KEYNO = B1 | B2<<1 | B3<<2 | B4<<3                  'KEYNO = shifted & OR'd 4 bytes for a composite single byte
                                                            'where each bit represents one of the 8 keys
                                                            
      IF KEYNO = $00 THEN                                   'If all Bn's = $00 this means no key is pressed
        GOTO READKEY                                        'If no key is pressed, loop to top and read key bytes again
                                                            
        ELSE                                                'When KEYNO is not $00, then assign a key number
        LOOKDOWN KEYNO,[$01,$02,$04,$08,$10,$20,$40,$80],i  'This converts the byte value to the key number 0 thru 7
      ENDIF                                                 'Ex: the byte value $08 is key #4, so i=3
        
        'Convert the key number 0 thru 7 to a 7-segment hex code 1 thru 8
        LOOKUP i,[$06,$5B,$4F,$66,$6D,$7D,$07,$7F],KEYNO    'Redefine KEYNO as the display hex code
        
        LOW STB                                             'Enable chip select                                
          SHIFTOUT SDA, SCK, 0, [CMD1]                      'This shifts out the 8-bit write command character $44
        HIGH STB                                            'for fixed mode (random access) addressing
        PAUSE 1
        
        'Display the key number at the corresponding display location and flash the LED that many times
        LOW STB
          SHIFTOUT SDA, SCK, 0, [$C0+(2*i),KEYNO]           'Shiftout the key number to the 7-segment digit at the
        HIGH STB                                            'corresponding key location
        PAUSE 1
        For j = 0 to i                                      'Flash the LED the number of times of the key 
        LOW STB
          SHIFTOUT SDA, SCK, 0, [$C1+(2*i),$01]             'Shiftout Address byte and data byte to discrete LED
        HIGH STB                                            'to turn ON the LED for 1/4 second
        PAUSE 250
        LOW STB
          SHIFTOUT SDA, SCK, 0, [$C1+(2*i),$00]             'Shiftout Address byte and data byte to discrete LED
        HIGH STB                                            'to turn OFF the LED for 1/4 second
        PAUSE 250                                           
        Next j                                              'Repeat the number of times equal to the key number
        
        LOW STB
          SHIFTOUT SDA, SCK, 0, [CMD3]                      'The output command sends CMD3 which turns on the display                                           
        HIGH STB
        
        GOSUB CLEARALL        
    
     GOTO READKEY                                           'Return to beginning and wait for another key press
     
    '******************************************* Subroutine CLEARALL ****************************************************
    'This section clears the LEDs and sets the 7-segments digits to "-" in TM1638's display memory
    CLEARALL:
        LOW STB                                        'Enable chip select                                
        SHIFTOUT SDA, SCK, 0, [CMD1]                   'This shifts out the 8-bit write command character $44
        HIGH STB                                       'for fixed mode (random access) addressing
        PAUSE 1
        
        For i = $C0 to $CF                             'This loop clears the display by writing 0x40 to each digit and LED
        LOW STB                                        'LEDs remain OFF with this input / 7-segments show "-"
        SHIFTOUT SDA, SCK, 0, [i,$40]                  '$40 is the 7-segment "-" character (0x40)
        HIGH STB
        PAUSE 1
        Next i
        
        LOW STB
        SHIFTOUT SDA, SCK, 0, [CMD3]                   'The output command sends CMD3 which turns on the display                                          
        HIGH STB                                       'and sets the brightness level
        
        PAUSE 500                                      'Wait 1/2 second after clearing the display
    RETURN    
        
    ' NOTES ON THE KEY MATRIX:    
    ' On this display/key assembly, the 8 switches are wired in the K3/KS1-KS8 matrix column (see TM1638 data sheet V1.3
    ' Figures(3)&(4) on page 7). This means switches S1-S4 activate the lsb "B0" in Bytes 1-4 respectively, and switches
    ' S5-S8 activate bit "B4" in Bytes 1-4 respectively. The code above shifts each of the 4 Bytes left by an incremental
    ' bit and ORs them to create a single byte KEYNO with all 8 switch bits present.
    '
    ' Bit Number:       B7      B6      B5      B4      B3      B2      B1      B0
    '  Switch #1         0       0       0       0       0       0       0       1    Byte 1 - not shifted left
    '  Switch #2         0       0       0       0       0       0       1       0    Byte 2 - shifted left 1 bit
    '  Switch #3         0       0       0       0       0       1       0       0    Byte 3 - shifted left 2 bits
    '  Switch #4         0       0       0       0       1       0       0       0    Byte 4 - shifted left 3 bits
    '  Switch #5         0       0       0       1       0       0       1       0    Byte 1 - not shifted left
    '  Switch #6         0       0       1       0       0       0       1       0    Byte 2 - shifted left 1 bit
    '  Switch #7         0       1       0       0       0       0       1       0    Byte 3 - shifted left 2 bits
    '  Switch #8         1       0       0       0       0       0       1       0    Byte 4 - shifted left 3 bits
    '
    ' KEYNO OR's Value   1       1       1       1       1       1       1       1    ONLY SINGLE KEY PRESSES ARE ALLOWED
    '                                                                                 SO ONLY 1 OF 8 BITS WILL = 1
    '
    ' LOOKDOWN SEARCH   $80     $40     $20     $10     $08     $04     $02     $01   LOOKDOWN converts KEYNO to key number
    ' LOOKDOWN VALUE     7       6       5       4       3       2       1       0
    '    
    ' Key:              S8      S7      S6      S5      S4      S3      S2      S1         Switch Numbers 1 - 8
    ' Matrix Loc:     K3/KS8  K3/KS6  K3/KS4  K3/KS2  K3/KS7  K3/KS5  K3/KS3  K3/KS1       TM1638 Matrix Location
    ' Byte Number:      B4      B3      B2      B1      B4      B3      B2      B1         Following Key Read 0x42 command
    ' Key Hex Code:    0x10    0x10    0x10    0x10    0x01    0x01    0x01    0x01        Non-shifted hex code
    ' Shifted Left:    0x80    0x40    0x20    0x10    0x08    0x04    0x20    0x01        Shifted hex code
    ' Key# Code:       0x7F    0x07    0x7D    0x6D    0x66    0x4F    0x5B    0x06        Digit value and LED 7 Seg Code
    ' Displayed Digit:   8       7       6       5       4       3       2       1
    ' 
    '************************************** End of Keypad Reading ****************************************************

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: TM1638 LED Display Driver/Controller

    Thank you for the nice contribution and detailed comments on the code.

    Ioannis

Similar Threads

  1. AY0438 Display Driver
    By tallen in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 27th April 2010, 21:48
  2. Replies: 2
    Last Post: - 14th July 2008, 23:11
  3. display driver
    By Klaus in forum General
    Replies: 7
    Last Post: - 17th April 2008, 23:16
  4. 7-segment display driver
    By Klaus in forum General
    Replies: 3
    Last Post: - 15th April 2008, 14:37
  5. Driver code for 14 segment LED display
    By Durward Searcy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th December 2004, 21:08

Members who have read this thread : 4

You do not have permission to view the list of names.

Posting Permissions

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