How to use TM1637 chip for LED Display?


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    May 2011
    Location
    Bangalore, India
    Posts
    33

    Default How to use TM1637 chip for LED Display?

    Hi All,

    For the past 10 days, I have been trying to make the TM1637 work with PBP 2.60C but till now I have not been successful. It's a real low cost module. The price of the TM1637 4-digit 7-segment 0.36" display module is about $ 0.60 in Ali Express. There is a 0.56" version and that costs about $ 1.25.
    The module works fine with Arduino, but all the important instructions to interface with TM1637 are in the library file (TM1637Display.h). After spending some time with the library file, I decided to study the datasheet of the chip and write the required code in PBP. The protocol followed by the chip is very similar to I2C with a few differences. Like I2C, it's a two-wire interface but you can have only one slave on the bus and hence there is no slave address. Secondly, while transmitting a byte, the LS bit is sent first.

    The code I wrote for PIC 12F1840 is shown below and it is extremely simple. It should make the number '2' appear on the leftmost digit of the display. When I tested the code, the diagnostic LED blinked, but after that nothing happened. I used Saleae Logic Analyzer to check whether the Clock & Data pulses and Ack were all OK. After some tweaking, I managed to get everything correct (Screenshot file attached). The only issue is, the display shows nothing! Since I know that the module is working fine with Arduino, it's my code in PBP that needs to be looked into.

    I would highly appreciate any helpful hint, suggestion or sample code.

    - Bala


    '---------------------------------------------------------------------------
    ' This code is for using TM1637 with Four-digit 7-segment LED Display
    ' with PIC12F1840. Osc is set at 16MHz.
    '---------------------------------------------------------------------------
    '---------------------------------------------------------------------------
    '12F1840 Pinout:
    ' -------------------------------------------------------
    ' VDD VSS
    ' PortA.5 = Not_used PortA.0 = LED
    ' PortA.4 = Not_used PortA.1 = DIO
    ' PortA.3 = Not_used PortA.2 = CLK
    ' -------------------------------------------------------

    '@ __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _BOREN_OFF
    '@ __CONFIG _CONFIG2, _PLLEN_OFF & _BORV_LO & _LVP_OFF

    ASM
    ifndef __12F1840
    error "12F1840 not selected"
    endif
    ENDASM

    OSCCON = %01111000 '16MHz HF, software PLL disabled
    DEFINE OSC 16

    'Port direction
    TRISA = %001000 'A.3 is input, rest are outputs.

    CM1CON0 = 0 'Disable comparator
    ANSELA = 0 'Disable A to D

    'List of Aliases
    LED var PortA.0
    DIO var PortA.1
    CLK var PortA.2
    TrisDIO VAR TrisA.1

    'List of variables
    Cnt var byte
    ByteToSend var byte 'Command or Data byte to be sent

    DispCmd var byte
    dispcmd = 64 'Command byte for displaying characters starting with the leftmost digit (0b01000000)

    Brt8Cmd var byte
    brt8cmd = 143 'Command byte for display on with maximum brightness (0b10001111)

    Dig1Addr var byte
    dig1addr = 192 'Address of digit 1 (left most) (0b11000000)

    DispNum var byte
    dispnum = 146 ' "2" (Common anode) (10010010)

    'Diagnostic routine
    DiagRoutine:
    for cnt = 1 to 4 'Blink LED 4 times.
    led = 1 : pause 100
    led = 0 : pause 100
    next cnt

    goto start

    'Subroutines
    StartCondition:
    clk = 0
    dio = 1
    clk = 1
    pauseus 50
    dio = 0 'DIO goes low when CLK is high.
    pauseus 50
    return

    SendTheByte:
    clk = 0
    for cnt = 7 to 0 step -1 'Send 8 bits of data, starting with the LSB.
    dio = bytetosend.0(cnt)
    pauseus 50
    clk = 1
    pauseus 50
    clk = 0
    Next cnt
    'dio = 1
    Trisdio = 1 ' Set Data pin direction to input to receive ACK.
    pauseus 50
    clk = 1
    pauseus 50
    clk = 0
    Trisdio = 0 ' Set Data pin direction back to output.
    return

    StopCondition:
    clk = 0
    dio = 0
    clk = 1
    pauseus 50
    dio = 1 'DIO goes high when CLK is high.
    pauseus 50
    return

    START:

    bytetosend = brt8cmd 'Brt8Cmd (10001111) (143) = Command for Display On with Maximum Brightness
    gosub startcondition
    gosub sendthebyte
    gosub stopcondition

    bytetosend = dispcmd 'DispCmd (01000000) (64) = Command byte with auto increment sent before sending the digit data
    gosub startcondition
    gosub sendthebyte
    gosub stopcondition

    bytetosend = dig1addr 'Dig1Addr (11000000) (192) = Address of digit 1 (leftmost digit)
    gosub startcondition
    gosub sendthebyte

    bytetosend = dispnum ' Number "2" (10010010) (146) (Common anode)
    gosub sendthebyte

    gosub stopcondition


    END
    Attached Images Attached Images  
    Last edited by Balachandar; - 10th October 2018 at 14:15.

  2. #2
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Don't know much about your TM1637, but the one thing that jumped out for me was:

    Code:
    for cnt = 7 to 0 step -1 'Send 8 bits of data, starting with the LSB. 
    dio = bytetosend.0(cnt)
    I think your FOR/NEXT would be shifting MSB first (bit7, bit6, bit5...)

    If you're getting bits banging out, maybe I'm wrong, but try:

    Code:
    for cnt = 0 to 7
    dio = bytetosend.(cnt)
    Haven't tried that code, not even in simulation.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,514


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Mike is correct
    try this , it works on a 12f1822
    note leds 2,3 and 4 show random crap since nothing is written to them
    and not using lat regs is asking for trouble

    nearly forgot dispnum = 146 ' "2" (Common anode) (10010010)
    not on my display

    dispnum = $5b ' "2" (Common anode) (01011101)



    Code:
    OSCCON = %01111000 '16MHz HF, software PLL disabled
     DEFINE OSC 16
    
     'Port direction
     TRISA = %001000 'A.3 is input, rest are outputs.
    
     CM1CON0 = 0 'Disable comparator
     ANSELA = 0 'Disable A to D
    
     'List of Aliases
     LED var PortA.0
     DIO var LATA.1
     CLK var LATA.2 
     TrisDIO VAR TrisA.1
    
     'List of variables
     Cnt var byte
     ByteToSend var byte 'Command or Data byte to be sent
    
     DispCmd var byte
     dispcmd = 64 'Command byte for displaying characters starting with the leftmost digit (0b01000000)
    
     Brt8Cmd var byte
     brt8cmd = 143 'Command byte for display on with maximum brightness (0b10001111)
    
     Dig1Addr var byte
     dig1addr = 192 'Address of digit 1 (left most) (0b11000000)
    
     DispNum var byte
     dispnum = $5B' "2" (Common anode) (10010010)
    
     'Diagnostic routine
     DiagRoutine: 
     CLK=1
     for cnt = 1 to 4 'Blink LED 4 times.
     led = 1 : pause 100 
     led = 0 : pause 100
     next cnt
    
     goto start
    
     'Subroutines
     StartCondition:
     clk = 0
     dio = 1
     clk = 1
     pauseus 50
     dio = 0 'DIO goes low when CLK is high.
     pauseus 50
     return
    
     SendTheByte:
     clk = 0
     for cnt = 0 to 7   'Send 8 bits of data, starting with the LSB. 
    dio = bytetosend.0(cnt)
    pauseus 50
    clk = 1 
    pauseus 50
    clk = 0
     Next cnt
     'dio = 1 
     Trisdio = 1 ' Set Data pin direction to input to receive ACK.
     pauseus 50
     clk = 1
     pauseus 50
     clk = 0
     Trisdio = 0 ' Set Data pin direction back to output.
     return
    
     StopCondition:
     clk = 0
     dio = 0
     clk = 1
     pauseus 50
     dio = 1 'DIO goes high when CLK is high. 
     pauseus 50
     return
    
     START:
    
    
    
     bytetosend = dispcmd 'DispCmd (01000000) (64) = Command byte with auto increment sent before sending the digit data
     gosub startcondition
     gosub sendthebyte 
     gosub stopcondition
    
     bytetosend = dig1addr 'Dig1Addr (11000000) (192) = Address of digit 1 (leftmost digit)
     gosub startcondition
     gosub sendthebyte
    
     bytetosend = dispnum ' Number "2" (10010010) (146) (Common anode) 
     gosub sendthebyte
    
     gosub stopcondition
       bytetosend = brt8cmd 'Brt8Cmd (10001111) (143) = Command for Display On with Maximum Brightness
     gosub startcondition
     gosub sendthebyte
     gosub stopcondition
    
     END
    Last edited by richard; - 10th October 2018 at 15:45.
    Warning I'm not a teacher

  4. #4
    Join Date
    May 2011
    Location
    Bangalore, India
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Mpgmike & Richard, thanks a lot for your comments and suggestions.

    I first tried 'FOR Cnt = 0 to 7'. Since that didn't work, I changed the code to ''FOR Cnt = 7 to 0 STEP -1' to see whether that would make a difference; still the display wouldn't turn on. I agree; if the LSB is to be sent first, it should be 'FOR Cnt = 0 to 7'.

    Richard, you are saying, not using LAT registers is asking for trouble. But since the bits for PortA 0, 1 & 2 are cleared in the TRIS register, they have been clearly defined as outputs right in the beginning of the code. Would it make any difference, whether you call it PortA.1 or LATA.1? Are you not referring to the Output in either case? Secondly, referring to the output as LAT results in compilation errors in PBP 2.60C for older PICs like 12F683.

    I corrected the code as per the suggestions given, but the display does not turn on. I also tried the same code with a 12F683 at 8MHz IntOsc. Again, there is no good news to report.

    In the absence of any working PBP code for TM1637, my only option seems to be that I need to do an in-depth study of the Arduino library files and get a clear understanding to be able to write a working PBP code.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,514


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    In the absence of any working PBP code for TM1637, my only option seems to be that I need to do an in-depth study of the Arduino library files and get a clear understanding to be able to write a working PBP code.
    the corrected version of your code I posted works for me


    the code I posted here works
    http://www.picbasic.co.uk/forum/showthread.php?t=23417


    sheldons code posted here works

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


    your code is a bit glitchy and about 30 times too slow but it can work

    see difference in these pics
    Attached Images Attached Images   
    Warning I'm not a teacher

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,514


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Richard, you are saying, not using LAT registers is asking for trouble. But since the bits for PortA 0, 1 & 2 are cleared in the TRIS register, they have been clearly defined as outputs right in the beginning of the code. Would it make any difference, whether you call it PortA.1 or LATA.1? Are you not referring to the Output in either case? Secondly, referring to the output as LAT results in compilation errors in PBP 2.60C for older PICs like 12F683.
    its all about rmw errors , 12f683 has no lat regs

    https://www.microchip.com/webinars.m...cName=en556253
    Warning I'm not a teacher

  7. #7
    Join Date
    May 2011
    Location
    Bangalore, India
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Thank you Richard, for the clarity you provided on LAT registers.
    Couple of days back, I downloaded PBP 3.1.1 with the idea of trying it out and to compile codes written for Ver 3 and above.
    I used your demo code (TM1637-DEMO.pbp.txt), made suitable changes for PIC 18F2620 at 32MHz, configured PortB pins 0, 6 & 7 for LED, DIO & CLK respectively and made the required changes in the ASM section for the same. But on compiling, many errors are reported like "Illegal opcode (FSR0)" and "Found label after column 1".
    Attached is my code and the screenshot showing the compilation errors. I am clueless as to where the issue is and your help is appreciated.


    Code:
    define      OSC 32    ' (8 x 4 = 32)
    
    ; --- *** Oscillator  *** ---------------------------------------------------
    
    OSCCON = %01110000      'Int Osc: 8MHz 
                            '(Bits 4, 5 & 6 determine the internal clock speed). 
                            ' 111 = 8MHz   110 = 4MHz
    'OSCTUNE = %10000000   'PLL is disabled.
    OSCTUNE = %11000000   'PLL is enabled.
    
    TRISA = 0
    TRISB = 0
    TRISC = 0
     
    CMCON = 7          'Disable the comparators of PortA
    ADCON0 = 0         'Disable ADC
    ADCON1 = 15        'All pins of Port A are digital I/O
    '    ANSELb = 0
    '    ANSELA = 0
    '    ANSELC = 0
    
        LED    VAR  PORTB.0  ' Assign name "LED" to 
             
        USERCOMMAND "DISPLED"    ; BUFFER{,COLON }
        USERCOMMAND "STRLEN"     ; BUFFER
        USERCOMMAND "RJUSTBUFF"  ; BUFFER
        
        TM_BIT        VAR BYTE BANK0
        TM_DAT        VAR BYTE BANK0
        TM_NAK        VAR BYTE BANK0       ;set to $80 if tx error occured
        TM_DIG        VAR BYTE BANK0
        TM_BRIGHT     VAR BYTE BANK0       ;display pwm level 0 to 7
        TM_COLON      VAR BYTE BANK0       ;display colon  0/1
        TM_TMP        VAR BYTE[2] BANK0
        str_len       VAR BYTE BANK0
        
        CMD_AUTO_ADDR CON $40 
        START_ADDR    CON $c0 
        NUM_DIGITS    CON 4                ;number of 7seg leds
        COLON_FLAG    CON $80 
        DISPLAY_ON    CON $88
        ;tm1637      DIO AND CLK NEED TO BE ON SAME PORT
        @TM_OUT_PORT = LATB       ;TM_DIO  OUT
        @TM_IN_PORT  = PORTB      ;TM_DIO  IN
        @TM_CLK      = 6          ;PIN
        @TM_DIO      = 7          ;PIN
        @TM_TRIS     = TRISB 
        
        BLINK         VAR BYTE
        LEDBUFF       VAR BYTE[NUM_DIGITS +1] 
        COUNTER       VAR byte
        
    GOTO OVERASM
      
    ASM  
    DISPLED?B  macro BUFFER       
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        L?CALL _TM_DISP4 
        endm  
    DISPLED?BC  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?CB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm    
    DISPLED?BB  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?BB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm 
    
    STRLEN?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        L?CALL   STRlen
        endm
    RJUSTBUFF?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        MOVE?CB low  BUF, _TM_TMP  ;load low byte
        MOVE?CB HIGH  BUF, _TM_TMP+1  ;load low byte
        L?CALL  STRlen
        L?CALL  _strpad
        endm  
        
               
    STRlen                     ;get buffer usage size 
        CLRF     _str_len    
    str_tok_chr 
        MOVIW    FSR0 ++       ; Get character
        btfsC    STATUS,Z     
        goto     exit_str_null ; EXIT ON Null char
        INCF     _str_len,F    ; not null so increment index
        goto     str_tok_chr
    exit_str_null
        return 
    
    _strpad           ;right justify by padding with spaces " "
        BANKSEL _str_len 
        movlw   _NUM_DIGITS+1     ;buffer size   
        subwf   _str_len,f        ;tx string size
        comf    _str_len,f        ;the difference is number of spaces to SHIFT in
        btfsc   STATUS, Z
        goto    expd              ;if zero difference then exit
    nxpd
        movf    _TM_TMP,w         ;addr of buffer l
        MOVWF   FSR0
        movf    _TM_TMP+1,w       ;addr of buffer h
        MOVWF   FSR0+1
    	movlw   _NUM_DIGITS         
        addwf   FSR0,f
        movf    FSR0,w
        movwf   FSR1
        movf    FSR0+1,w
        movwf   FSR1+1
        DECF    FSR0,F             ;offset pointer 0
        BANKSEL _TM_BIT
        movlw   _NUM_DIGITS        
        movwf   _TM_BIT
    nxby                          ;buffer shift right x 1
        MOVIW   FSR0 --
        MOVWI   FSR1 --
        DECFSZ  _TM_BIT,F
        GOTO    nxby
        movlw   0x20
        MOVWI   FSR1 --            ;poke a space in buff[0]
        BANKSEL _str_len 
        DECFSZ  _str_len,F         ;need another one ?
        goto    nxpd
    expd                           ;nah!
        return
    
      
    SEG_val
        CHK?RP  _TM_TMP
        MOVWF   _TM_TMP
        SUBLW   0x21
        btfsc   STATUS, C
        retlw   0   ;" "
        MOVF    _TM_TMP,W
        SUBLW   0x2f
        btfsc   STATUS, C
        retlw   64   ;"-"
        MOVF    _TM_TMP,W
        MOVLW   0X40
        SUBWF   _TM_TMP,W
        btfsC   STATUS, C
        GOTO    TM_ALPHA
        MOVF    _TM_TMP,W
        ANDLW   0X0F
        GOTO    TM_LU
    TM_ALPHA 
        ANDLW   0xdf    ;ucase
        SUBLW   6
        btfsS   STATUS, C
        retlw   0       ;ERROR
        MOVLW   0X37
        SUBWF   _TM_TMP,W
        ANDLW   0xdf    ;ucase
    TM_LU     
        BRW                                         
        retlw 	0X3F    ;0   
        retlw 	6            	
        retlw 	0X5B   
        retlw 	0X4F 
        retlw 	0X66
        retlw 	0X6D                        
        retlw 	0X7D  
        retlw 	7   
        retlw 	0X7F          
        retlw 	0X67    ;9
        retlw 	0X77    ;A
        retlw 	0X7C    ;b
        retlw 	0X39    ;C
        retlw 	0X5E    ;d
        retlw 	0X79    ;E
        retlw 	0X71    ;F
        
    TM_START    
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK
        BSF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_CLK
        NOP
        RETURN
        
    TM_STOP 
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        BCF    TM_OUT_PORT,TM_DIO 
        NOP
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        BSF    TM_OUT_PORT,TM_DIO 
        NOP
        RETURN
        
    TM_WRITE 
        MOVLW  8 
        CHK?RP _TM_BIT
        MOVWF  _TM_BIT
    NXBT   
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_DAT
        BTFSS  _TM_DAT,0
        GOTO   TM_0
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_DIO 
        GOTO   TM_NB
    TM_0    
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_DIO  
    TM_NB   
        CHK?RP _TM_DAT
        RRF    _TM_DAT,F
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_BIT
        DECFSZ _TM_BIT,F
        GOTO   NXBT
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS
        BSF    TM_TRIS,TM_DIO 
        CHK?RP TM_IN_PORT
        BTFSC  TM_IN_PORT,TM_DIO 
        BSF    _TM_NAK,7
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        NOP
        NOP
        NOP
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS 
        BCF    TM_TRIS ,TM_DIO 
        RST?RP
        RETURN
    _TM_INIT
        CHK?RP  _TM_COLON
        CLRF    _TM_COLON
        CLRF    _TM_BRIGHT
        CHK?RP TM_TRIS
        BCF    TM_TRIS ,TM_DIO
        BCF    TM_TRIS ,TM_CLK
        RST?RP
        RETURN
        
    _TM_DISP4 
        MOVLW   _CMD_AUTO_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CLRF    _TM_NAK
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _NUM_DIGITS          
        CHK?RP  _TM_DIG
        MOVWF   _TM_DIG
        MOVLW   _START_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
    NXDIG
        MOVIW   FSR0 ++
        CALL    SEG_val
        CHK?RP  _TM_DAT
        IORWF   _TM_COLON ,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CHK?RP  _TM_DIG
        DECFSZ  _TM_DIG,F
        GOTO    NXDIG
        CALL    TM_STOP
        CHK?RP  _TM_BRIGHT 
        MOVF    _TM_BRIGHT ,W
        ANDLW   7
        IORLW   _DISPLAY_ON
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        RST?RP
        RETURN
    ENDASM  
    
    OVERASM: 
     
        CALL TM_INIT
        
        
     ALOOP:   
        TOGGLE LED  
        TM_BRIGHT=TM_BRIGHT-1
        if TM_BRIGHT>7 then TM_BRIGHT=7
        ARRAYWRITE LEDBUFF ,["b- ",#TM_BRIGHT,0]
        DISPLED  LEDBUFF,0
        PAUSE 500
        FOR COUNTER=0 TO 255
            ARRAYWRITE LEDBUFF ,[hex COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        FOR COUNTER=0 TO 255
            IF  !(COUNTER//20) THEN BLINK = TM_COLON ^ $80
            ARRAYWRITE LEDBUFF ,[sdec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        goto ALOOP
    Screenshot showing errors:
    Name:  AsmErrors.jpg
Views: 4435
Size:  169.2 KB

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,514


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    MY code is for a pic16 with enhanced core it needs some changes to operate on older chips and/or pic18

    moviw fsrx ++ can be replaced with
    MOVF INDFx,W
    INCF FSRx

    brw becomes
    addwf PCL, F ; as long as you don't overflow pcl

    pic 18 has no rrf code
    repl with rrcf



    PIC 18F2620 version , not tested did compile ok


    Code:
    #CONFIG
      CONFIG  OSC=INTIO67, FCMEN=OFF, IESO=OFF, PWRT=OFF, BOREN=SBORDIS, BORV=3
      CONFIG  WDT=ON, WDTPS=512, CCP2MX=PORTC, PBADEN=OFF, LPT1OSC=OFF, MCLRE=ON
      CONFIG  STVREN=ON, LVP=OFF, XINST=OFF, DEBUG=OFF, CP0=OFF, CP1=OFF, CP2=OFF
      CONFIG  CP3=OFF, CPB=OFF, CPD=OFF, WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF
      CONFIG  WRTC=OFF, WRTB=OFF, WRTD=OFF, EBTR0=OFF, EBTR1=OFF, EBTR2=OFF
      CONFIG  EBTR3=OFF, EBTRB=OFF
    #ENDCONFIG
     
    
    define      OSC 32    ' (8 x 4 = 32)
    ; --- *** Oscillator  *** ---------------------------------------------------
    OSCCON = %01110000      'Int Osc: 8MHz 
                            '(Bits 4, 5 & 6 determine the internal clock speed). 
                            ' 111 = 8MHz   110 = 4MHz
    'OSCTUNE = %11000000   'PLL is disabled.   what??????????????
    OSCTUNE.6 = 1   'PLL is enabled.
    TRISA = 0
    TRISB = 0
    TRISC = 0
     
    'CMCON = 7          'Disable the comparators of PortA
    'ADCON0 = 0         'Disable ADC
    ADCON1 = 15        'All pins of Port A are digital I/O
    '    ANSELb = 0
    '    ANSELA = 0
    '    ANSELC = 0
        LED    VAR  PORTB.0  ' Assign name "LED" to 
             
        USERCOMMAND "DISPLED"    ; BUFFER{,COLON }
        USERCOMMAND "STRLEN"     ; BUFFER
        USERCOMMAND "RJUSTBUFF"  ; BUFFER
        
        TM_BIT        VAR BYTE BANK0
        TM_DAT        VAR BYTE BANK0
        TM_NAK        VAR BYTE BANK0       ;set to $80 if tx error occured
        TM_DIG        VAR BYTE BANK0
        TM_BRIGHT     VAR BYTE BANK0       ;display pwm level 0 to 7
        TM_COLON      VAR BYTE BANK0       ;display colon  0/1
        TM_TMP        VAR BYTE[2] BANK0
        str_len       VAR BYTE BANK0
        
        CMD_AUTO_ADDR CON $40 
        START_ADDR    CON $c0 
        NUM_DIGITS    CON 4                ;number of 7seg leds
        COLON_FLAG    CON $80 
        DISPLAY_ON    CON $88
        ;tm1637      DIO AND CLK NEED TO BE ON SAME PORT
        @TM_OUT_PORT = LATB       ;TM_DIO  OUT
        @TM_IN_PORT  = PORTB      ;TM_DIO  IN
        @TM_CLK      = 6          ;PIN
        @TM_DIO      = 7          ;PIN
        @TM_TRIS     = TRISB 
        
        BLINK         VAR BYTE
        LEDBUFF       VAR BYTE[NUM_DIGITS +1] 
        COUNTER       VAR byte
        
    GOTO OVERASM
      
    ASM  
    DISPLED?B  macro BUFFER       
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        L?CALL _TM_DISP4 
        endm  
    DISPLED?BC  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?CB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm    
    DISPLED?BB  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?BB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm 
    STRLEN?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        L?CALL   STRlen
        endm
    RJUSTBUFF?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        MOVE?CB low  BUF, _TM_TMP  ;load low byte
        MOVE?CB HIGH  BUF, _TM_TMP+1  ;load low byte
        L?CALL  STRlen
        L?CALL  _strpad
        endm  
        
               
    STRlen                     ;get buffer usage size 
        CLRF     _str_len    
    str_tok_chr 
        ;MOVIW    FSR0 ++       ; Get character
        MOVF INDF0,W
        INCF FSR0
        btfsC    STATUS,Z     
        goto     exit_str_null ; EXIT ON Null char
        INCF     _str_len,F    ; not null so increment index
        goto     str_tok_chr
    exit_str_null
        return 
    _strpad           ;right justify by padding with spaces " "
        BANKSEL _str_len 
        movlw   _NUM_DIGITS+1     ;buffer size   
        subwf   _str_len,f        ;tx string size
        comf    _str_len,f        ;the difference is number of spaces to SHIFT in
        btfsc   STATUS, Z
        goto    expd              ;if zero difference then exit
    nxpd
        movf    _TM_TMP,w         ;addr of buffer l
        MOVWF   FSR0
        movf    _TM_TMP+1,w       ;addr of buffer h
        MOVWF   FSR0+1
     movlw   _NUM_DIGITS         
        addwf   FSR0,f
        movf    FSR0,w
        movwf   FSR1
        movf    FSR0+1,w
        movwf   FSR1+1
        DECF    FSR0,F             ;offset pointer 0
        BANKSEL _TM_BIT
        movlw   _NUM_DIGITS        
        movwf   _TM_BIT
    nxby                          ;buffer shift right x 1
        ;MOVIW   FSR0 --
        MOVF INDF0,W
        DECF FSR0
        ;MOVWI   FSR1 --
        MOVF INDF1,W
        DECF FSR1
        DECFSZ  _TM_BIT,F
        GOTO    nxby
        movlw   0x20
        ;MOVWI   FSR1 --            ;poke a space in buff[0]  
        MOVF INDF1,W
        DECF FSR1
        BANKSEL _str_len 
        DECFSZ  _str_len,F         ;need another one ?
        goto    nxpd
    expd                           ;nah!
        return
      
    SEG_val
        CHK?RP  _TM_TMP
        MOVWF   _TM_TMP
        SUBLW   0x21
        btfsc   STATUS, C
        retlw   0   ;" "
        MOVF    _TM_TMP,W
        SUBLW   0x2f
        btfsc   STATUS, C
        retlw   64   ;"-"
        MOVF    _TM_TMP,W
        MOVLW   0X40
        SUBWF   _TM_TMP,W
        btfsC   STATUS, C
        GOTO    TM_ALPHA
        MOVF    _TM_TMP,W
        ANDLW   0X0F
        GOTO    TM_LU
    TM_ALPHA 
        ANDLW   0xdf    ;ucase
        SUBLW   6
        btfsS   STATUS, C
        retlw   0       ;ERROR
        MOVLW   0X37
        SUBWF   _TM_TMP,W
        ANDLW   0xdf    ;ucase
    TM_LU     
        addwf   PCL, F                                          
        retlw  0X3F    ;0   
        retlw  6             
        retlw  0X5B   
        retlw  0X4F 
        retlw  0X66
        retlw  0X6D                        
        retlw  0X7D  
        retlw  7   
        retlw  0X7F          
        retlw  0X67    ;9
        retlw  0X77    ;A
        retlw  0X7C    ;b
        retlw  0X39    ;C
        retlw  0X5E    ;d
        retlw  0X79    ;E
        retlw  0X71    ;F
        
    TM_START    
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK
        BSF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_CLK
        NOP
        RETURN
        
    TM_STOP 
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        BCF    TM_OUT_PORT,TM_DIO 
        NOP
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        BSF    TM_OUT_PORT,TM_DIO 
        NOP
        RETURN
        
    TM_WRITE 
        MOVLW  8 
        CHK?RP _TM_BIT
        MOVWF  _TM_BIT
    NXBT   
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_DAT
        BTFSS  _TM_DAT,0
        GOTO   TM_0
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_DIO 
        GOTO   TM_NB
    TM_0    
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_DIO  
    TM_NB   
        CHK?RP _TM_DAT
        RRCF    _TM_DAT,F
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_BIT
        DECFSZ _TM_BIT,F
        GOTO   NXBT
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS
        BSF    TM_TRIS,TM_DIO 
        CHK?RP TM_IN_PORT
        BTFSC  TM_IN_PORT,TM_DIO 
        BSF    _TM_NAK,7
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        NOP
        NOP
        NOP
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS 
        BCF    TM_TRIS ,TM_DIO 
        RST?RP
        RETURN
    _TM_INIT
        CHK?RP  _TM_COLON
        CLRF    _TM_COLON
        CLRF    _TM_BRIGHT
        CHK?RP TM_TRIS
        BCF    TM_TRIS ,TM_DIO
        BCF    TM_TRIS ,TM_CLK
        RST?RP
        RETURN
        
    _TM_DISP4 
        MOVLW   _CMD_AUTO_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CLRF    _TM_NAK
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _NUM_DIGITS          
        CHK?RP  _TM_DIG
        MOVWF   _TM_DIG
        MOVLW   _START_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
    NXDIG
        ;MOVIW   FSR0 ++
        MOVF INDF0,W
        INCF FSR0
        CALL    SEG_val
        CHK?RP  _TM_DAT
        IORWF   _TM_COLON ,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CHK?RP  _TM_DIG
        DECFSZ  _TM_DIG,F
        GOTO    NXDIG
        CALL    TM_STOP
        CHK?RP  _TM_BRIGHT 
        MOVF    _TM_BRIGHT ,W
        ANDLW   7
        IORLW   _DISPLAY_ON
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        RST?RP
        RETURN
    ENDASM  
    OVERASM: 
     
        CALL TM_INIT
        
        
     ALOOP:   
        TOGGLE LED  
        TM_BRIGHT=TM_BRIGHT-1
        if TM_BRIGHT>7 then TM_BRIGHT=7
        ARRAYWRITE LEDBUFF ,["b- ",#TM_BRIGHT,0]
        DISPLED  LEDBUFF,0
        PAUSE 500
        FOR COUNTER=0 TO 255
            ARRAYWRITE LEDBUFF ,[hex COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        FOR COUNTER=0 TO 255
            IF  !(COUNTER//20) THEN BLINK = TM_COLON ^ $80
            ARRAYWRITE LEDBUFF ,[sdec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        goto ALOOP
    Warning I'm not a teacher

  9. #9
    Join Date
    May 2011
    Location
    Bangalore, India
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    I used your 18F2620 version of the code for TM1637. It compiles OK, but in the subsequent test with the 18F2620 on a breadboard, the LED lights up and stays lit without anything happening to the LED module. In order to make sure that the PIC is working, I added a For-Next loop to make the LED blink 3 times for 500mS each as a diagnostic routine before the main code executes. Now, the LED blinks correctly telling me that the PIC is working properly at 32MHz. But this is what happens: 3 Blinks, gap of 1 second, 3 blinks and so on. I feel, the PIC is getting reset repeatedly.

    Then I tried your original code with 12F1840. The compilation goes through without any errors. But when tested with the LED module, the LED stays lit and the TM1637 LED module does not light up.

    Please let me know if you want me to post the code I used for the two PICs.

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,514


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Then I tried your original code with 12F1840. The compilation goes through without any errors. But when tested with the LED module, the LED stays lit and the TM1637 LED module does not light up.

    Please let me know if you want me to post the code I used for the two PICs.
    this works for me ,its for a 12f1822 and should run on a 12f1840 as is

    if you post your code I will look at it,


    Code:
    '****************************************************************
    '*  Name    :tm1637-demo.PBP                                     *
    '*  Author  : richard                                            *
    '*  Notice  :                                                    *
    '*          :                                                    *
    '*  Date    :                                                    *
    '*  Version :                                                    *
    '*  Notes   : PIC12f1822                                        *
    '*          :    dio on RA1  clk RA2                            *
    '****************************************************************
    
       #CONFIG
      __config  _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
      __config  _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_19 & _LVP_OFF
    #ENDCONFIG
       
     OSCCON = %01111000 '16MHz HF, software PLL disabled
     DEFINE OSC 16 
      ANSELA=0   
     
        
        USERCOMMAND "DISPLED"    ; BUFFER{,COLON }   TAKES 150uS FOR 4 DIGITS
        USERCOMMAND "STRLEN"     ; BUFFER
        USERCOMMAND "RJUSTBUFF"  ; BUFFER
        ;ALL NEED TO BE IN SAME BANK
        TM_BIT        VAR BYTE BANK0
        TM_DAT        VAR BYTE BANK0
        TM_NAK        VAR BYTE BANK0       ;set to $80 if tx error occured
        TM_DIG        VAR BYTE BANK0
        TM_BRIGHT     VAR BYTE BANK0       ;display pwm level 0 to 7
        TM_COLON      VAR BYTE BANK0       ;display colon  0/1
        TM_TMP        VAR BYTE[2] BANK0
        str_len       VAR BYTE BANK0
        ;tm1637 CONSTANTS
        CMD_AUTO_ADDR CON $40 
        START_ADDR    CON $c0 
        NUM_DIGITS    CON 4                ;number of 7seg leds
        COLON_FLAG    CON $80 
        DISPLAY_ON    CON $88
        ;tm1637      DIO AND CLK NEED TO BE ON SAME PORT
        @TM_OUT_PORT = LATA       ;TM_DIO  OUT
        @TM_IN_PORT  = PORTA      ;TM_DIO  IN
        @TM_CLK      = 2          ;PIN
        @TM_DIO      = 1         ;PIN
        @TM_TRIS     = TRISA 
        
        BLINK         VAR BYTE
        LEDBUFF       VAR BYTE[NUM_DIGITS +1] 
        COUNTER       VAR byte
        
    GOTO OVERASM
      
    ASM  
    DISPLED?B  macro BUFFER       
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        L?CALL _TM_DISP4 
        endm  
    DISPLED?BC  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        CHK?RP  _TM_COLON 
        MOVE?CB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm    
    DISPLED?BB  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        CHK?RP  _TM_COLON 
        MOVE?BB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm 
    STRLEN?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        L?CALL   STRlen
        endm
    RJUSTBUFF?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        CHK?RP   _TM_TMP 
        MOVE?CB low  BUF, _TM_TMP  ;load low byte
        MOVE?CB HIGH  BUF, _TM_TMP+1  ;load low byte
        L?CALL  STRlen
        L?CALL  _strpad
        endm  
        
               
    STRlen                     ;get buffer usage size 
        CHK?RP   _str_len 
        CLRF     _str_len    
    str_tok_chr 
        MOVIW    FSR0 ++       ; Get character
        btfsC    STATUS,Z     
        goto     exit_str_null ; EXIT ON Null char
        INCF     _str_len,F    ; not null so increment index
        goto     str_tok_chr
    exit_str_null
        BANKSEL 0
        return 
    _strpad           ;right justify by padding with spaces " "
        CHK?RP  _str_len 
        movlw   _NUM_DIGITS+1     ;buffer size 
        subwf   _str_len,f        ;tx string size  
        btfsc   STATUS, C         ;tx string size  gotcha
        goto    expd              ;if positive difference then exit buffer overrun will occur  ;tx string size  gotcha
        comf    _str_len,f        ;the difference is number of spaces to SHIFT in
        btfsc   STATUS, Z
        goto    expd              ;if zero difference then exit
        movlw   _NUM_DIGITS       ;buffer size 
        addwf   _TM_TMP,f         ;add  NUM_DIGITS to  addr of buffer
    nxpd
        movf    _TM_TMP,w         ;low (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0L
        movwf   FSR1L
        movf    _TM_TMP+1,w       ;high (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0H
        movwf   FSR1H 
        DECF    FSR0L,F            ;offset pointer 0
        movlw   _NUM_DIGITS 
        movwf   _TM_BIT
    nxby                          ;buffer shift right x 1
        MOVIW   FSR0 --
        MOVWI   FSR1 --
        DECFSZ  _TM_BIT,F
        GOTO    nxby
        movlw   0x20
        MOVWI   FSR1 --            ;poke a space in buff[0]
        DECFSZ  _str_len,F         ;need another one ?
        goto    nxpd
    expd                           ;nah!
        BANKSEL 0
        return
      
    SEG_val         ;VALID INPUT  0-9 ,A-F, a-f ," " ,-
        CHK?RP  _TM_TMP
        MOVWF   _TM_TMP
        SUBLW   0x21
        btfsc   STATUS, C
        retlw   0   ;" "
        MOVF    _TM_TMP,W
        SUBLW   0x2f
        btfsc   STATUS, C
        retlw   64   ;"-"
        MOVF    _TM_TMP,W
        MOVLW   0X40
        SUBWF   _TM_TMP,W
        btfsC   STATUS, C
        GOTO    TM_ALPHA
        MOVF    _TM_TMP,W
        ANDLW   0X0F
        GOTO    TM_LU
    TM_ALPHA 
        ANDLW   0xdf    ;ucase
        SUBLW   6
        btfsS   STATUS, C
        retlw   0       ;ERROR
        MOVLW   0X37
        SUBWF   _TM_TMP,W
        ANDLW   0xdf    ;ucase
    TM_LU     
        BRW                                         
        retlw  0X3F    ;0   
        retlw  6             
        retlw  0X5B   
        retlw  0X4F 
        retlw  0X66
        retlw  0X6D                        
        retlw  0X7D  
        retlw  7   
        retlw  0X7F          
        retlw  0X67    ;9
        retlw  0X77    ;A
        retlw  0X7C    ;b
        retlw  0X39    ;C
        retlw  0X5E    ;d
        retlw  0X79    ;E
        retlw  0X71    ;F
        
    TM_START    
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK
        BSF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_CLK
        ;NOP
        RETURN
        
    TM_STOP 
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        BCF    TM_OUT_PORT,TM_DIO 
        NOP
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        BSF    TM_OUT_PORT,TM_DIO 
        ;NOP
        RETURN
        
    TM_WRITE 
        MOVLW  8 
        CHK?RP _TM_BIT
        MOVWF  _TM_BIT
    NXBT   
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_DAT
        BTFSS  _TM_DAT,0
        GOTO   TM_0
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_DIO 
        GOTO   TM_NB
    TM_0    
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_DIO  
    TM_NB   
        CHK?RP _TM_DAT
        RRF    _TM_DAT,F
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_BIT
        DECFSZ _TM_BIT,F
        GOTO   NXBT
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS
        BSF    TM_TRIS,TM_DIO 
        CHK?RP TM_IN_PORT
        BTFSC  TM_IN_PORT,TM_DIO 
        BSF    _TM_NAK,7
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        NOP
        NOP
        NOP
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS 
        BCF    TM_TRIS ,TM_DIO 
        BANKSEL 0
        RETURN
    _TM_INIT
        CHK?RP  _TM_COLON
        CLRF    _TM_COLON
        CLRF    _TM_BRIGHT
        CHK?RP TM_TRIS
        BCF    TM_TRIS ,TM_DIO
        BCF    TM_TRIS ,TM_CLK
        BANKSEL 0
        RETURN
        
    _TM_DISP4
        CHK?RP  _TM_DAT
        MOVLW   _CMD_AUTO_ADDR
        MOVWF   _TM_DAT
        CLRF    _TM_NAK
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _NUM_DIGITS          
        CHK?RP  _TM_DIG
        MOVWF   _TM_DIG
        MOVLW   _START_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
    NXDIG
        MOVIW   FSR0 ++
        CALL    SEG_val
        CHK?RP  _TM_DAT
        IORWF   _TM_COLON ,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CHK?RP  _TM_DIG
        DECFSZ  _TM_DIG,F
        GOTO    NXDIG
        CALL    TM_STOP
        CHK?RP  _TM_BRIGHT 
        MOVF    _TM_BRIGHT ,W
        ANDLW   7
        IORLW   _DISPLAY_ON
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        BANKSEL 0
        RETURN
    ENDASM  
    OVERASM: 
     
        CALL TM_INIT
        
     ALOOP:   
         
        TM_BRIGHT=TM_BRIGHT-1
        if TM_BRIGHT>7 then TM_BRIGHT=7
        ARRAYWRITE LEDBUFF ,["b- ",#TM_BRIGHT,0]
        DISPLED  LEDBUFF,0
        PAUSE 500
        FOR COUNTER=0 TO 255
            ARRAYWRITE LEDBUFF ,[dec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,0
            PAUSE 50
        NEXT
        FOR COUNTER=0 TO 255
            IF  !(COUNTER//20) THEN BLINK = TM_COLON ^ COLON_FLAG 
            ARRAYWRITE LEDBUFF ,[sdec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        GOTO ALOOP
    Warning I'm not a teacher

  11. #11
    Join Date
    May 2011
    Location
    Bangalore, India
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Compiled your code for 12F1822 for 12F1840 after adding the 'Blink LED 3 times' routine. Compilation went through without a hitch followed by programming a 12F1840. When tested, the LED blinks 3 times and goes off. Nothing else happens.

    Compiled once again, this time changing the uC to 12F1822 in MC Studio. Again, the compilation went through fine. But on testing, the LED blinks and goes off, but the LED module does not come to life.

    There is one more strange thing I am encountering. A month back, I bought 5 pieces of 12F1840-I/SN (SOIC package) from Ali Exp for $ 3.22. The laser printing/etching on the chip clearly shows "12F1840" and it works at 5V without any problem. But PICKIT2 identifies it as 12LF1840 and the voltage will not go beyond 3.6V. Of course, even if I use an LF version of the PIC, the TM1637 LED module should work. Every now and then, I connect the module to a Nano and check in order to make sure that there is nothing wrong with the module. Whether the voltage is 5 or 3.3, the module works fine. Understandably, the brightness is less at 3.3V.

    Code used with 12F1840:

    Code:
    ' 12F1840 Pinout:
    '  -------------------------------------------------------  
    '  	VDD                         VSS 
    '  	PortA.5 = Not_used          PortA.0 = LED            
    '  	PortA.4 = Not_used          PortA.1 = DIO
    '  	PortA.3 = Not_used          PortA.2 = CLK
    '  -------------------------------------------------------                                                       
    
    #CONFIG
        __config _CONFIG1, _FOSC_INTOSC & _MCLRE_ON & _CP_OFF & _CPD_OFF
        __config _CONFIG2, _PLLEN_OFF & _LVP_OFF
    #ENDCONFIG
    
    OSCCON = %01111000 '16MHz HF, software PLL disabled
    DEFINE OSC 16 
    
    'Port direction
    TRISA = %001000         'A.3 is input, rest are outputs.
     
    ANSELA = 0				'Disable A to D
    CM1CON0 = 0             'Disable comparator
     
        
        USERCOMMAND "DISPLED"    ; BUFFER{,COLON }   TAKES 150uS FOR 4 DIGITS
        USERCOMMAND "STRLEN"     ; BUFFER
        USERCOMMAND "RJUSTBUFF"  ; BUFFER
        ;ALL NEED TO BE IN SAME BANK
        TM_BIT        VAR BYTE BANK0
        TM_DAT        VAR BYTE BANK0
        TM_NAK        VAR BYTE BANK0       ;set to $80 if tx error occured
        TM_DIG        VAR BYTE BANK0
        TM_BRIGHT     VAR BYTE BANK0       ;display pwm level 0 to 7
        TM_COLON      VAR BYTE BANK0       ;display colon  0/1
        TM_TMP        VAR BYTE[2] BANK0
        str_len       VAR BYTE BANK0
        
        ;tm1637 CONSTANTS
        CMD_AUTO_ADDR CON $40 
        START_ADDR    CON $c0 
        NUM_DIGITS    CON 4                ;number of 7seg leds
        COLON_FLAG    CON $80 
        DISPLAY_ON    CON $88
        ;tm1637      DIO AND CLK NEED TO BE ON SAME PORT
        @TM_OUT_PORT = LATA       ;TM_DIO  OUT
        @TM_IN_PORT  = PORTA      ;TM_DIO  IN
        @TM_CLK      = 2          ;PIN
        @TM_DIO      = 1          ;PIN
        @TM_TRIS     = TRISA 
        
        BLINK         VAR BYTE
        LEDBUFF       VAR BYTE[NUM_DIGITS +1] 
        COUNTER       VAR byte
        Cnt           var byte             ;for use in loops
    
    LED var LATA.0
    
    'Diagnostic routine
    DiagRoutine:                           
    for cnt = 1 to 3           'Blink LED 3 times.
    led = 1 : pause 500 
    led = 0 : pause 500
    next cnt
    
        
    GOTO OVERASM
      
    ASM  
    DISPLED?B  macro BUFFER       
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        L?CALL _TM_DISP4 
        endm  
    DISPLED?BC  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        CHK?RP  _TM_COLON 
        MOVE?CB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm    
    DISPLED?BB  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        CHK?RP  _TM_COLON 
        MOVE?BB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm 
    STRLEN?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        L?CALL   STRlen
        endm
    RJUSTBUFF?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        CHK?RP   _TM_TMP 
        MOVE?CB low  BUF, _TM_TMP  ;load low byte
        MOVE?CB HIGH  BUF, _TM_TMP+1  ;load low byte
        L?CALL  STRlen
        L?CALL  _strpad
        endm  
        
               
    STRlen                     ;get buffer usage size 
        CHK?RP   _str_len 
        CLRF     _str_len    
    str_tok_chr 
        MOVIW    FSR0 ++       ; Get character
        btfsC    STATUS,Z     
        goto     exit_str_null ; EXIT ON Null char
        INCF     _str_len,F    ; not null so increment index
        goto     str_tok_chr
    exit_str_null
        BANKSEL 0
        return 
    _strpad           ;right justify by padding with spaces " "
        CHK?RP  _str_len 
        movlw   _NUM_DIGITS+1     ;buffer size 
        subwf   _str_len,f        ;tx string size  
        btfsc   STATUS, C         ;tx string size  gotcha
        goto    expd              ;if positive difference then exit buffer overrun will occur  ;tx string size  gotcha
        comf    _str_len,f        ;the difference is number of spaces to SHIFT in
        btfsc   STATUS, Z
        goto    expd              ;if zero difference then exit
        movlw   _NUM_DIGITS       ;buffer size 
        addwf   _TM_TMP,f         ;add  NUM_DIGITS to  addr of buffer
    nxpd
        movf    _TM_TMP,w         ;low (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0L
        movwf   FSR1L
        movf    _TM_TMP+1,w       ;high (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0H
        movwf   FSR1H 
        DECF    FSR0L,F            ;offset pointer 0
        movlw   _NUM_DIGITS 
        movwf   _TM_BIT
    nxby                          ;buffer shift right x 1
        MOVIW   FSR0 --
        MOVWI   FSR1 --
        DECFSZ  _TM_BIT,F
        GOTO    nxby
        movlw   0x20
        MOVWI   FSR1 --            ;poke a space in buff[0]
        DECFSZ  _str_len,F         ;need another one ?
        goto    nxpd
    expd                           ;nah!
        BANKSEL 0
        return
      
    SEG_val         ;VALID INPUT  0-9 ,A-F, a-f ," " ,-
        CHK?RP  _TM_TMP
        MOVWF   _TM_TMP
        SUBLW   0x21
        btfsc   STATUS, C
        retlw   0   ;" "
        MOVF    _TM_TMP,W
        SUBLW   0x2f
        btfsc   STATUS, C
        retlw   64   ;"-"
        MOVF    _TM_TMP,W
        MOVLW   0X40
        SUBWF   _TM_TMP,W
        btfsC   STATUS, C
        GOTO    TM_ALPHA
        MOVF    _TM_TMP,W
        ANDLW   0X0F
        GOTO    TM_LU
    TM_ALPHA 
        ANDLW   0xdf    ;ucase
        SUBLW   6
        btfsS   STATUS, C
        retlw   0       ;ERROR
        MOVLW   0X37
        SUBWF   _TM_TMP,W
        ANDLW   0xdf    ;ucase
    TM_LU     
        BRW                                         
        retlw  0X3F    ;0   
        retlw  6             
        retlw  0X5B   
        retlw  0X4F 
        retlw  0X66
        retlw  0X6D                        
        retlw  0X7D  
        retlw  7   
        retlw  0X7F          
        retlw  0X67    ;9
        retlw  0X77    ;A
        retlw  0X7C    ;b
        retlw  0X39    ;C
        retlw  0X5E    ;d
        retlw  0X79    ;E
        retlw  0X71    ;F
        
    TM_START    
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK
        BSF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_CLK
        ;NOP
        RETURN
        
    TM_STOP 
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        BCF    TM_OUT_PORT,TM_DIO 
        NOP
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        BSF    TM_OUT_PORT,TM_DIO 
        ;NOP
        RETURN
        
    TM_WRITE 
        MOVLW  8 
        CHK?RP _TM_BIT
        MOVWF  _TM_BIT
    NXBT   
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_DAT
        BTFSS  _TM_DAT,0
        GOTO   TM_0
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_DIO 
        GOTO   TM_NB
    TM_0    
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_DIO  
    TM_NB   
        CHK?RP _TM_DAT
        RRF    _TM_DAT,F
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_BIT
        DECFSZ _TM_BIT,F
        GOTO   NXBT
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS
        BSF    TM_TRIS,TM_DIO 
        CHK?RP TM_IN_PORT
        BTFSC  TM_IN_PORT,TM_DIO 
        BSF    _TM_NAK,7
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        NOP
        NOP
        NOP
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS 
        BCF    TM_TRIS ,TM_DIO 
        BANKSEL 0
        RETURN
    _TM_INIT
        CHK?RP  _TM_COLON
        CLRF    _TM_COLON
        CLRF    _TM_BRIGHT
        CHK?RP TM_TRIS
        BCF    TM_TRIS ,TM_DIO
        BCF    TM_TRIS ,TM_CLK
        BANKSEL 0
        RETURN
        
    _TM_DISP4
        CHK?RP  _TM_DAT
        MOVLW   _CMD_AUTO_ADDR
        MOVWF   _TM_DAT
        CLRF    _TM_NAK
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _NUM_DIGITS          
        CHK?RP  _TM_DIG
        MOVWF   _TM_DIG
        MOVLW   _START_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
    NXDIG
        MOVIW   FSR0 ++
        CALL    SEG_val
        CHK?RP  _TM_DAT
        IORWF   _TM_COLON ,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CHK?RP  _TM_DIG
        DECFSZ  _TM_DIG,F
        GOTO    NXDIG
        CALL    TM_STOP
        CHK?RP  _TM_BRIGHT 
        MOVF    _TM_BRIGHT ,W
        ANDLW   7
        IORLW   _DISPLAY_ON
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        BANKSEL 0
        RETURN
    ENDASM  
    OVERASM: 
     
        CALL TM_INIT
        
     ALOOP:   
         
        TM_BRIGHT=TM_BRIGHT-1
        if TM_BRIGHT>7 then TM_BRIGHT=7
        ARRAYWRITE LEDBUFF ,["b- ",#TM_BRIGHT,0]
        DISPLED  LEDBUFF,0
        PAUSE 500
        FOR COUNTER=0 TO 255
            ARRAYWRITE LEDBUFF ,[dec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,0
            PAUSE 50
        NEXT
        FOR COUNTER=0 TO 255
            IF  !(COUNTER//20) THEN BLINK = TM_COLON ^ COLON_FLAG 
            ARRAYWRITE LEDBUFF ,[sdec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        GOTO ALOOP

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,514


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    Compiled your code for 12F1822 for 12F1840 after adding the 'Blink LED 3 times' routine. Compilation went through without a hitch followed by programming a 12F1840. When tested, the LED blinks 3 times and goes off. Nothing else happens
    .

    WORKS just fine for me, even at 3v




    pic18 version [can't believe how many changes it needed]

    Code:
    '****************************************************************
    '*  Name    :tm1637-demo.PBP                                     *
    '*  Author  : richard                                            *
    '*  Notice  :                                                    *
    '*          :                                                    *
    '*  Date    :                                                    *
    '*  Version :                                                    *
    '*  Notes   : PIC18F45K20                                     *
    '*          :    dio on RB0  clk RB1                           *
    '****************************************************************
     
    #CONFIG
      CONFIG  FOSC = INTIO67
      CONFIG  FCMEN = OFF
      CONFIG  IESO = OFF
      CONFIG  PWRT = OFF
      CONFIG  BOREN = SBORDIS
      CONFIG  BORV = 18
      CONFIG  WDTEN = ON
      CONFIG  WDTPS = 512
      CONFIG  CCP2MX = PORTC
      CONFIG  PBADEN = OFF
      CONFIG  LPT1OSC = OFF
      CONFIG  HFOFST = ON
      CONFIG  MCLRE = ON
      CONFIG  STVREN = ON
      CONFIG  LVP = OFF
      CONFIG  XINST = OFF
      CONFIG  DEBUG = OFF
      CONFIG  CP0 = OFF
      CONFIG  CP1 = OFF
      CONFIG  CP2 = OFF
      CONFIG  CP3 = OFF
      CONFIG  CPB = OFF
      CONFIG  CPD = OFF
      CONFIG  WRT0 = OFF
      CONFIG  WRT1 = OFF
      CONFIG  WRT2 = OFF
      CONFIG  WRT3 = OFF
      CONFIG  WRTC = OFF
      CONFIG  WRTB = OFF
      CONFIG  WRTD = OFF
      CONFIG  EBTR0 = OFF
      CONFIG  EBTR1 = OFF
      CONFIG  EBTR2 = OFF
      CONFIG  EBTR3 = OFF
      CONFIG  EBTRB = OFF
    #ENDCONFIG
    DEFINE OSC 32
       
       osccon=$60   '32 mhz
        OSCTUNE.6=1
       
        trisb.4=0
       ANSELH=0
        LED    VAR  PORTB.4  ' Assign name "LED" to 
             
        USERCOMMAND "DISPLED"    ; BUFFER{,COLON }
        USERCOMMAND "STRLEN"     ; BUFFER
        USERCOMMAND "RJUSTBUFF"  ; BUFFER
        
        TM_BIT        VAR BYTE BANK0
        TM_DAT        VAR BYTE BANK0
        TM_NAK        VAR BYTE BANK0       ;set to $80 if tx error occured
        TM_DIG        VAR BYTE BANK0
        TM_BRIGHT     VAR BYTE BANK0       ;display pwm level 0 to 7
        TM_COLON      VAR BYTE BANK0       ;display colon  0/1
        TM_TMP        VAR BYTE[2] BANK0
        str_len       VAR BYTE BANK0
        
        CMD_AUTO_ADDR CON $40 
        START_ADDR    CON $c0 
        NUM_DIGITS    CON 4                ;number of 7seg leds
        COLON_FLAG    CON $80 
        DISPLAY_ON    CON $88
        ;tm1637      DIO AND CLK NEED TO BE ON SAME PORT
        @TM_OUT_PORT = LATB       ;TM_DIO  OUT
        @TM_IN_PORT  = PORTB      ;TM_DIO  IN
        @TM_CLK      = 1          ;PIN
        @TM_DIO      = 0          ;PIN
        @TM_TRIS     = TRISB 
        
        BLINK         VAR BYTE
        LEDBUFF       VAR BYTE[NUM_DIGITS +1] 
        COUNTER       VAR BYTE
        
    GOTO OVERASM
      
    ASM  
    DISPLED?B  macro BUFFER       
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        L?CALL _TM_DISP4 
        endm  
    DISPLED?BC  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?CB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm    
    DISPLED?BB  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?BB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm 
    STRLEN?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        L?CALL   STRlen
        endm
    RJUSTBUFF?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        MOVE?CB low  BUF, _TM_TMP  ;load low byte
        MOVE?CB HIGH  BUF, _TM_TMP+1  ;load low byte
        L?CALL  STRlen
        L?CALL  _strpad
        endm  
     
          
         
      
               
    STRlen                     ;get buffer usage size 
        CLRF     _str_len    
    str_tok_chr 
        ;MOVIW    FSR0 ++       ; Get character
        MOVF INDF0,W
        INFSNZ FSR0L
        incf   FSR0H
        btfsC    STATUS,Z     
        goto     exit_str_null ; EXIT ON Null char
        INCF     _str_len,F    ; not null so increment index
        goto     str_tok_chr
    exit_str_null
        return 
      
     
    _strpad           ;right justify by padding with spaces " "
        BANKSEL 0
        MOVLW   5
        ;ADDWF   _str_field_size,w  ;buffer size
        subwf   _str_len,f         ;tx string size 
        btfsc   STATUS, C          ;tx string size  gotcha
        goto    expd ;if positive difference then exit buffer overrun will occur  ;tx string size  gotcha
        comf    _str_len           ;the difference is number of spaces to SHIFT in
        btfsc   STATUS, Z
        goto    expd               ;if zero difference then exit
        moVLW    4  ;buffer size
        addwf   _TM_TMP,f         ;add  NUM_DIGITS to  addr of buffer
        ;btfsc   STATUS, C
        ;INCF    _STR_TMP+1,f
    nxpd
        movf    _TM_TMP,w         ;low (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0L
        movwf   FSR1L
        DECF    FSR0L,F             ;offset pointer 0
        ;btfsc   STATUS, C 
        ;DECF    FSR0H,F 
        movf    _TM_TMP+1,w        ;high (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0H
        movwf   FSR1H
        MOVLW   4 
        movwf   _TM_BIT
    nxby                            ;buffer shift right x 1
       ifdef TBLPTRL                ;fsr 
          movf  POSTDEC0, W         ;[pic18]
          MOVWF POSTDEC1
       else 
          MOVIW FSR0 --
          MOVWI FSR1 --
       endif
        DECFSZ  _TM_BIT,F
        GOTO    nxby
        movlw   0x20
        ifdef TBLPTRL               ;fsr 
          MOVWF POSTDEC1
        else 
          MOVWI FSR1 --
        endif
        DECFSZ  _str_len,F          ;need another one ?
        goto    nxpd
    expd                            ;nah!
        BANKSEL 0
        return
    SEG_val
        CHK?RP  _TM_TMP
        MOVWF   _TM_TMP
        CLRF _TM_TMP +1
        SUBLW   0x21
        btfsc   STATUS, C
        retlw   0   ;" "
        MOVF    _TM_TMP,W
        SUBLW   0x2f
        btfsc   STATUS, C
        retlw   64   ;"-"
        MOVF    _TM_TMP,W
        MOVLW   0X40
        SUBWF   _TM_TMP,W
        btfsC   STATUS, C
        GOTO    TM_ALPHA
        MOVF    _TM_TMP,W
        ANDLW   0X0F
        GOTO    TM_LU
    TM_ALPHA 
        ANDLW   0xdf    ;ucase
        SUBLW   6
        btfsS   STATUS, C
        retlw   0       ;ERROR
        MOVLW   0X37
        SUBWF   _TM_TMP,W
        ANDLW   0xdf    ;ucase
    TM_LU  
        addlw   LOW TABLE_START
        movwf   TBLPTRL
        rlcf    _TM_TMP +1 ,w
        addlw   HIGH TABLE_START 
        movwf   TBLPTRH
        movlw   UPPER TABLE_START 
        movwf   TBLPTRU
        tblrd   *+
        movf   TABLAT,w    
        return
          
      
    TABLE_START 
     db    0X3F,6,0X5B,0X4F,0X66,0X6D,0X7D,7,0X7F,0X67,0X77 ,0X7C ,0X39 ,0X5E ,0X79 ,0X71                        
        
        
    TM_START    
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK
        BSF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_CLK
        NOP
        RETURN
        
    TM_STOP 
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        BCF    TM_OUT_PORT,TM_DIO 
        NOP
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        BSF    TM_OUT_PORT,TM_DIO 
        NOP
        RETURN
        
    TM_WRITE 
        MOVLW  8 
        CHK?RP _TM_BIT
        MOVWF  _TM_BIT
    NXBT   
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_DAT
        BTFSS  _TM_DAT,0
        GOTO   TM_0
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_DIO 
        GOTO   TM_NB
    TM_0    
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_DIO  
    TM_NB   
        CHK?RP _TM_DAT
        RRCF    _TM_DAT,F
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_BIT
        DECFSZ _TM_BIT,F
        GOTO   NXBT
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS
        BSF    TM_TRIS,TM_DIO 
        CHK?RP TM_IN_PORT
        BTFSC  TM_IN_PORT,TM_DIO 
        BSF    _TM_NAK,7
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        NOP
        NOP
        NOP
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS 
        BCF    TM_TRIS ,TM_DIO 
        RST?RP
        RETURN
    _TM_INIT
        CHK?RP  _TM_COLON
        CLRF    _TM_COLON
        CLRF    _TM_BRIGHT
        CHK?RP TM_TRIS
        BCF    TM_TRIS ,TM_DIO
        BCF    TM_TRIS ,TM_CLK
        RST?RP
        RETURN
        
    _TM_DISP4 
        MOVLW   _CMD_AUTO_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CLRF    _TM_NAK
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _NUM_DIGITS          
        CHK?RP  _TM_DIG
        MOVWF   _TM_DIG
        MOVLW   _START_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
    NXDIG
        ;MOVIW   FSR0 ++
        MOVF INDF0,W
        INFSNZ FSR0L
        incf   FSR0H
        CALL    SEG_val
        CHK?RP  _TM_DAT
        IORWF   _TM_COLON ,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CHK?RP  _TM_DIG
        DECFSZ  _TM_DIG,F
        GOTO    NXDIG
        CALL    TM_STOP
        CHK?RP  _TM_BRIGHT 
        MOVF    _TM_BRIGHT ,W
        ANDLW   7
        IORLW   _DISPLAY_ON
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        RST?RP
        RETURN
        
        
    _TM_DISP1     ; WRITES TO A DISP POSN TM_TMP HOLDS DATA FOR TM_DIG IS POSN  0-3
        CHK?RP  _TM_DAT
        MOVLW   _CMD_AUTO_ADDR
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _START_ADDR
        addwf   _TM_DIG,W
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
        CHK?RP  _TM_TMP
        MOVF    _TM_TMP,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CALL    TM_STOP
        BANKSEL 0
        RETURN 
        
        
        
        
        
        
        
    ENDASM  
    OVERASM: 
        FOR COUNTER=0 TO 3
         TOGGLE LED 
           PAUSE 500
        next
        CALL TM_INIT
      BLINK=0  
        
     ALOOP:   
         TOGGLE LED  
        TM_BRIGHT=TM_BRIGHT-1
        if TM_BRIGHT>7 then TM_BRIGHT=7
        ARRAYWRITE LEDBUFF ,["B- ",#TM_BRIGHT]
       
        DISPLED  LEDBUFF,0
        
        
        PAUSE 500
        FOR COUNTER=0 TO 255
            ARRAYWRITE LEDBUFF ,[hex COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,0
            PAUSE 50
        NEXT
        FOR COUNTER=0 TO 255
            IF  !(COUNTER//20) THEN BLINK = TM_COLON ^ $80
            ARRAYWRITE LEDBUFF ,[sdec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        goto ALOOP
    Warning I'm not a teacher

  13. #13
    Join Date
    May 2011
    Location
    Bangalore, India
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    WOW! FINALLY, IT HAPPENED!!

    The TM1637 LED display module came alive!!!
    I used your revised code for 18F devices and after I made a few changes in the CONFIG section, the compilation for 18F2620 was successful.
    I programmed and inserted the PIC in the breadboard and powered it up. After the LED blinked 3 times, it was a real pleasant sight to see the digits appear on the LED module.
    Thanks a lot Richard, for your patience and helping nature.

    The code I used for 18F2620 is reproduced below.
    OSCCON = $60; OSCTUNE.6 = 1 - These two settings in your code make the Osc 16Mhz (4MHz x 4). So, I changed the Osc speed from 32 to 16MHz. Later, I tried the code at 8MHz and 4MHz. It works beautifully at those speeds too.

    Code:
    '-------------------------------------------------------------------------------
    'PIC used: 18F2620
    'Pinout
    '                  ---------                                                        
    '   E.3 =         1         28  B.7 = 
    '   A.0 =         2         27  B.6 = 
    '   A.1 =         3         26  B.5 = 
    '   A.2 =         4         25  B.4 = LED
    '   A.3 =         5         24  B.3 = 
    '   A.4 =         6         23  B.2 = 
    '   A.5 =         7         22  B.1 = CLK
    '   Gnd           8         21  B.0 = DIO
    '   A.7 =         9         20  +5V 
    '   A.6 =        10         19  Gnd     
    '   C.0 =        11         18  C.7 = 
    '   C.1 =        12         17  C.6 = 
    '   C.2 =        13         16  C.5 = 
    '   C.3 =        14         15  C.4 =  
    '                  ---------
    '-------------------------------------------------------------------------------
    #CONFIG
      CONFIG  OSC = INTIO67         ; CONFIG  FOSC = INTIO67
      CONFIG  FCMEN = OFF
      CONFIG  IESO = OFF
      CONFIG  PWRT = OFF
      CONFIG  BOREN = SBORDIS
      CONFIG  BORV = 2              ; CONFIG  BORV = 18
      CONFIG  WDT = ON              ; CONFIG  WDTEN = ON
      CONFIG  WDTPS = 512
      CONFIG  CCP2MX = PORTC
      CONFIG  PBADEN = OFF
      CONFIG  LPT1OSC = OFF
    ;  CONFIG  HFOFST = ON        ;Not applicable to 18F2620
      CONFIG  MCLRE = ON
      CONFIG  STVREN = ON
      CONFIG  LVP = OFF
      CONFIG  XINST = OFF
      CONFIG  DEBUG = OFF
      CONFIG  CP0 = OFF
      CONFIG  CP1 = OFF
      CONFIG  CP2 = OFF
      CONFIG  CP3 = OFF
      CONFIG  CPB = OFF
      CONFIG  CPD = OFF
      CONFIG  WRT0 = OFF
      CONFIG  WRT1 = OFF
      CONFIG  WRT2 = OFF
      CONFIG  WRT3 = OFF
      CONFIG  WRTC = OFF
      CONFIG  WRTB = OFF
      CONFIG  WRTD = OFF
      CONFIG  EBTR0 = OFF
      CONFIG  EBTR1 = OFF
      CONFIG  EBTR2 = OFF
      CONFIG  EBTR3 = OFF
      CONFIG  EBTRB = OFF
    #ENDCONFIG
    
        OSCCON = $60   '4 mhz
        OSCTUNE.6 = 1  'PLL Enabled
       
    DEFINE OSC 16    '16MHz (4X4 = 16)
    
        TRISB.4=0
    '    ANSELH=0          'Not applicable to 18F2620
        ADCON1 = 15        'Disable ADC
    
        LED    VAR  PORTB.4  ' Assign name "LED" to 
             
        USERCOMMAND "DISPLED"    ; BUFFER{,COLON }
        USERCOMMAND "STRLEN"     ; BUFFER
        USERCOMMAND "RJUSTBUFF"  ; BUFFER
        
        TM_BIT        VAR BYTE BANK0
        TM_DAT        VAR BYTE BANK0
        TM_NAK        VAR BYTE BANK0       ;set to $80 if tx error occured
        TM_DIG        VAR BYTE BANK0
        TM_BRIGHT     VAR BYTE BANK0       ;display pwm level 0 to 7
        TM_COLON      VAR BYTE BANK0       ;display colon  0/1
        TM_TMP        VAR BYTE[2] BANK0
        str_len       VAR BYTE BANK0
        
        CMD_AUTO_ADDR CON $40 
        START_ADDR    CON $c0 
        NUM_DIGITS    CON 4                ;number of 7seg leds
        COLON_FLAG    CON $80 
        DISPLAY_ON    CON $88
        ;tm1637      DIO AND CLK NEED TO BE ON SAME PORT
        @TM_OUT_PORT = LATB       ;TM_DIO  OUT
        @TM_IN_PORT  = PORTB      ;TM_DIO  IN
        @TM_CLK      = 1          ;PIN
        @TM_DIO      = 0          ;PIN
        @TM_TRIS     = TRISB 
        
        BLINK         VAR BYTE
        LEDBUFF       VAR BYTE[NUM_DIGITS +1] 
        COUNTER       VAR BYTE
        
    GOTO OVERASM
      
    ASM  
    DISPLED?B  macro BUFFER       
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        L?CALL _TM_DISP4 
        endm  
    DISPLED?BC  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?CB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm    
    DISPLED?BB  macro BUFFER ,COLON      
        movlw   low BUFFER
        movwf   FSR0L
        movlw   high BUFFER
        movwf   FSR0H 
        MOVE?BB COLON ,_TM_COLON 
        L?CALL _TM_DISP4 
        endm 
    STRLEN?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        L?CALL   STRlen
        endm
    RJUSTBUFF?B  macro BUF      
        MOVE?CB high BUF, FSR0H ;load highbyte 
        MOVE?CB low  BUF, FSR0L ;load low byte
        MOVE?CB low  BUF, _TM_TMP  ;load low byte
        MOVE?CB HIGH  BUF, _TM_TMP+1  ;load low byte
        L?CALL  STRlen
        L?CALL  _strpad
        endm  
     
          
         
      
               
    STRlen                     ;get buffer usage size 
        CLRF     _str_len    
    str_tok_chr 
        ;MOVIW    FSR0 ++       ; Get character
        MOVF INDF0,W
        INFSNZ FSR0L
        incf   FSR0H
        btfsC    STATUS,Z     
        goto     exit_str_null ; EXIT ON Null char
        INCF     _str_len,F    ; not null so increment index
        goto     str_tok_chr
    exit_str_null
        return 
      
     
    _strpad           ;right justify by padding with spaces " "
        BANKSEL 0
        MOVLW   5
        ;ADDWF   _str_field_size,w  ;buffer size
        subwf   _str_len,f         ;tx string size 
        btfsc   STATUS, C          ;tx string size  gotcha
        goto    expd ;if positive difference then exit buffer overrun will occur  ;tx string size  gotcha
        comf    _str_len           ;the difference is number of spaces to SHIFT in
        btfsc   STATUS, Z
        goto    expd               ;if zero difference then exit
        moVLW    4  ;buffer size
        addwf   _TM_TMP,f         ;add  NUM_DIGITS to  addr of buffer
        ;btfsc   STATUS, C
        ;INCF    _STR_TMP+1,f
    nxpd
        movf    _TM_TMP,w         ;low (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0L
        movwf   FSR1L
        DECF    FSR0L,F             ;offset pointer 0
        ;btfsc   STATUS, C 
        ;DECF    FSR0H,F 
        movf    _TM_TMP+1,w        ;high (addr of buffer + NUM_DIGITS )
        MOVWF   FSR0H
        movwf   FSR1H
        MOVLW   4 
        movwf   _TM_BIT
    nxby                            ;buffer shift right x 1
       ifdef TBLPTRL                ;fsr 
          movf  POSTDEC0, W         ;[pic18]
          MOVWF POSTDEC1
       else 
          MOVIW FSR0 --
          MOVWI FSR1 --
       endif
        DECFSZ  _TM_BIT,F
        GOTO    nxby
        movlw   0x20
        ifdef TBLPTRL               ;fsr 
          MOVWF POSTDEC1
        else 
          MOVWI FSR1 --
        endif
        DECFSZ  _str_len,F          ;need another one ?
        goto    nxpd
    expd                            ;nah!
        BANKSEL 0
        return
    SEG_val
        CHK?RP  _TM_TMP
        MOVWF   _TM_TMP
        CLRF _TM_TMP +1
        SUBLW   0x21
        btfsc   STATUS, C
        retlw   0   ;" "
        MOVF    _TM_TMP,W
        SUBLW   0x2f
        btfsc   STATUS, C
        retlw   64   ;"-"
        MOVF    _TM_TMP,W
        MOVLW   0X40
        SUBWF   _TM_TMP,W
        btfsC   STATUS, C
        GOTO    TM_ALPHA
        MOVF    _TM_TMP,W
        ANDLW   0X0F
        GOTO    TM_LU
    TM_ALPHA 
        ANDLW   0xdf    ;ucase
        SUBLW   6
        btfsS   STATUS, C
        retlw   0       ;ERROR
        MOVLW   0X37
        SUBWF   _TM_TMP,W
        ANDLW   0xdf    ;ucase
    TM_LU  
        addlw   LOW TABLE_START
        movwf   TBLPTRL
        rlcf    _TM_TMP +1 ,w
        addlw   HIGH TABLE_START 
        movwf   TBLPTRH
        movlw   UPPER TABLE_START 
        movwf   TBLPTRU
        tblrd   *+
        movf   TABLAT,w    
        return
          
      
    TABLE_START 
     db    0X3F,6,0X5B,0X4F,0X66,0X6D,0X7D,7,0X7F,0X67,0X77 ,0X7C ,0X39 ,0X5E ,0X79 ,0X71                        
        
        
    TM_START    
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK
        BSF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_DIO
        NOP
        BCF    TM_OUT_PORT,TM_CLK
        NOP
        RETURN
        
    TM_STOP 
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        BCF    TM_OUT_PORT,TM_DIO 
        NOP
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        BSF    TM_OUT_PORT,TM_DIO 
        NOP
        RETURN
        
    TM_WRITE 
        MOVLW  8 
        CHK?RP _TM_BIT
        MOVWF  _TM_BIT
    NXBT   
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_DAT
        BTFSS  _TM_DAT,0
        GOTO   TM_0
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_DIO 
        GOTO   TM_NB
    TM_0    
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_DIO  
    TM_NB   
        CHK?RP _TM_DAT
        RRCF    _TM_DAT,F
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        CHK?RP _TM_BIT
        DECFSZ _TM_BIT,F
        GOTO   NXBT
        CHK?RP TM_OUT_PORT
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS
        BSF    TM_TRIS,TM_DIO 
        CHK?RP TM_IN_PORT
        BTFSC  TM_IN_PORT,TM_DIO 
        BSF    _TM_NAK,7
        CHK?RP TM_OUT_PORT
        BSF    TM_OUT_PORT,TM_CLK 
        NOP
        NOP
        NOP
        NOP
        BCF    TM_OUT_PORT,TM_CLK 
        CHK?RP TM_TRIS 
        BCF    TM_TRIS ,TM_DIO 
        RST?RP
        RETURN
    _TM_INIT
        CHK?RP  _TM_COLON
        CLRF    _TM_COLON
        CLRF    _TM_BRIGHT
        CHK?RP TM_TRIS
        BCF    TM_TRIS ,TM_DIO
        BCF    TM_TRIS ,TM_CLK
        RST?RP
        RETURN
        
    _TM_DISP4 
        MOVLW   _CMD_AUTO_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CLRF    _TM_NAK
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _NUM_DIGITS          
        CHK?RP  _TM_DIG
        MOVWF   _TM_DIG
        MOVLW   _START_ADDR
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
    NXDIG
        ;MOVIW   FSR0 ++
        MOVF INDF0,W
        INFSNZ FSR0L
        incf   FSR0H
        CALL    SEG_val
        CHK?RP  _TM_DAT
        IORWF   _TM_COLON ,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CHK?RP  _TM_DIG
        DECFSZ  _TM_DIG,F
        GOTO    NXDIG
        CALL    TM_STOP
        CHK?RP  _TM_BRIGHT 
        MOVF    _TM_BRIGHT ,W
        ANDLW   7
        IORLW   _DISPLAY_ON
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        RST?RP
        RETURN
        
        
    _TM_DISP1     ; WRITES TO A DISP POSN TM_TMP HOLDS DATA FOR TM_DIG IS POSN  0-3
        CHK?RP  _TM_DAT
        MOVLW   _CMD_AUTO_ADDR
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE
        CALL    TM_STOP
        MOVLW   _START_ADDR
        addwf   _TM_DIG,W
        CHK?RP  _TM_DAT
        MOVWF   _TM_DAT
        CALL    TM_START
        CALL    TM_WRITE 
        CHK?RP  _TM_TMP
        MOVF    _TM_TMP,W
        movwf   _TM_DAT
        CALL    TM_WRITE
        CALL    TM_STOP
        BANKSEL 0
        RETURN 
        
        
        
        
        
        
        
    ENDASM  
    OVERASM: 
        FOR COUNTER=0 TO 3
         TOGGLE LED 
           PAUSE 500
        next
        CALL TM_INIT
      BLINK=0  
        
     ALOOP:   
         TOGGLE LED  
        TM_BRIGHT=TM_BRIGHT-1
        if TM_BRIGHT>7 then TM_BRIGHT=7
        ARRAYWRITE LEDBUFF ,["B- ",#TM_BRIGHT]
       
        DISPLED  LEDBUFF,0
        
        
        PAUSE 500
        FOR COUNTER=0 TO 255
            ARRAYWRITE LEDBUFF ,[hex COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,0
            PAUSE 50
        NEXT
        FOR COUNTER=0 TO 255
            IF  !(COUNTER//20) THEN BLINK = TM_COLON ^ $80
            ARRAYWRITE LEDBUFF ,[sdec COUNTER,0]
            RJUSTBUFF  LEDBUFF 
            DISPLED  LEDBUFF,BLINK
            PAUSE 50
        NEXT
        goto ALOOP

  14. #14
    Join Date
    May 2013
    Location
    australia
    Posts
    2,514


    Did you find this post helpful? Yes | No

    Default Re: How to use TM1637 chip for LED Display?

    WOW! FINALLY, IT HAPPENED!!

    The TM1637 LED display module came alive!!!
    I used your revised code for 18F devices and after I made a few changes in the CONFIG section, the compilation for 18F2620 was successful.

    very good. you would have to wonder why the 12f1840 did not work

    I changed the Osc speed from 32 to 16MHz. Later, I tried the code at 8MHz and 4MHz. It works beautifully at those speeds too.
    the code works up t0 64mhz and can write the 4 digits in less than 50uS
    Attached Images Attached Images  
    Warning I'm not a teacher

Similar Threads

  1. TM1637 - display module include example
    By longpole001 in forum Code Examples
    Replies: 5
    Last Post: - 13th April 2020, 19:19
  2. 3642bh display module pinouts on tm1637 module
    By longpole001 in forum Schematics
    Replies: 0
    Last Post: - 31st July 2018, 19:09
  3. LED Matrix display
    By Art in forum General
    Replies: 13
    Last Post: - 5th October 2013, 05:06
  4. LED Bargraph chip (guitar LED bling-age) ..do with a PIC?
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 13th July 2009, 00:15
  5. TV Display Chip
    By zadok in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 17th April 2008, 23:17

Members who have read this thread : 0

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

Tags for this Thread

Posting Permissions

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