+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2011
    Location
    Bangalore, India
    Posts
    17

    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 13:15.

  2. #2
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    234

    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
    1,470

    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 14:45.
    This is more entertaining than Free to Air TV

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

    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
    1,470

    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   
    This is more entertaining than Free to Air TV

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    1,470

    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
    This is more entertaining than Free to Air TV

Similar Threads

  1. TM1637 - display module include example
    By longpole001 in forum Code Examples
    Replies: 2
    Last Post: - 9th October 2018, 11:14
  2. 3642bh display module pinouts on tm1637 module
    By longpole001 in forum Schematics
    Replies: 0
    Last Post: - 31st July 2018, 18:09
  3. LED Matrix display
    By Art in forum General
    Replies: 13
    Last Post: - 5th October 2013, 04: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: - 12th July 2009, 23:15
  5. TV Display Chip
    By zadok in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 17th April 2008, 22:17

Members who have read this thread : 14

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