Here is example code - PIC12F683 4-dig 7-Segment display - driven by TM1637


+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2007
    Posts
    78

    Default Here is example code - PIC12F683 4-dig 7-Segment display - driven by TM1637

    Hello,
    Below is some PBP3 code - to control a 4 digit 7-Segment display - driven by a TM1637
    The basic flow was written by a previous submitter (My sincere thanks!)
    I added functionality to display any specific 4-digit number

    The eventual goal is to have the 12F683 receive a value via soft_serial and display it.
    A kind of serial-to-7segment backpack - if you will

    If anyone feels inclined to add that functionality - please do!!
    We can all benefit from the results :-]

    '================================================= =============================
    ' Compiler: PBP3
    ' Device: PIC12F683
    ' Description: 4 digit 7-Segment Display with TM1637 driver
    ' Device Oscillator: 8MHz internal
    ' MCLR off

    '1 VDD
    '2 GP5/T1CKI/OSC1/CLKIN
    '3 GP4/AN3/T1G/OSC2/CLKOUT
    '4 GP3/MCLR/VPP
    '5 GP2/AN2/T0CKI/INT/COUT/CCP1
    '6 GP1/AN1/CIN-/VREF/ICSPCLK
    '7 GP0/AN0/CIN+/ICSPDAT/ULPWU
    '8 VSS

    'TM1637 NOTES:
    'Displaying any value requires 4 transactions
    'Each transaction incorporates a specific byte sent to the TM1637
    'A startcondition alerts TM1637 to be ready for a byte transaction
    'A stopcondition alerts TM1637 transaction byte is complete
    '1st transaction byte value must = x40
    '2nd transaction byte value designates which digit to display 192-195 = digit A-D
    '3rd transaction byte value designates which segments turned on
    '4th transaction byte value stipulates brightness 0-7


    #CONFIG
    __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG

    'INTCON = %10001000 'Internal oscillator
    OSCCON = %01110101 'Sets internal osc to 8 Mhz (Default) and stable
    ANSEL = 0 ' Set all digital
    CMCON0 = 7 ' Analog comparators off
    TRISIO = %0010000 'set all pin output
    'OPTION_REG.7 = 0 ' Enable internal pull ups

    DEFINE OSC 8

    'INIT Variables
    DIO var GPIO.0
    CLK var GPIO.1
    TrisDIO VAR TRISIO.0
    LED con 2
    B0 var word
    'B1 var byte
    DispNum var byte
    Cnt var byte
    ByteToSend var byte 'Command or Data byte to be sent
    DispCmd var byte
    Brt8Cmd var byte
    cntr var byte
    TempNum var word
    digitnum var byte[3]
    Dig1Addr var byte
    displaynums var byte

    dispcmd = 64 '0x40
    brt8cmd = 136 'Display brightness
    '136 = mid bright 10001000
    '143 = very bright 10001111

    'Diagnostic routine
    'DiagRoutine:
    'CLK = 1
    'for cnt = 1 to 10 'Blink LED 4 times.
    'HIGH LED
    'pause 100
    'low LED
    'pause 100
    'next cnt

    goto MAIN

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

    '====================== *** SendTheByte *** ==========================
    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 *** ========================
    StopCondition:
    clk = 0
    dio = 0
    clk = 1
    pauseus 50
    dio = 1 'DIO goes high when CLK is high.
    pauseus 50
    return

    '========================== *** MAIN *** =============================
    MAIN:

    'what number do we want the system to display
    B0 = 9000

    ' Ascertain number of digits to be displayed
    if B0 >= 0 THEN cntr = 1
    if B0 > 9 then cntr = 2
    if B0 > 99 then cntr = 3
    if B0 > 999 then cntr = 4

    TempNum = B0

    'Ascertain 1st digit value to be displayed
    if cntr > 3 then
    digitnum[0] = TempNum / 1000
    TempNum = TempNum - (digitnum[0] * 1000)
    else
    digitnum[0] = 0
    endif

    'Ascertain 2nd digit value to be displayed
    if cntr > 2 then
    digitnum[1] = TempNum / 100
    TempNum = TempNum - (digitnum[1] * 100)
    else
    digitnum[1] = 0
    endif

    'Ascertain 3rd digit value to be displayed
    if cntr > 1 then
    digitnum[2] = TempNum / 10
    TempNum = TempNum - (digitnum[2] * 10)
    else
    digitnum[2] = 0
    endif

    'Ascertain 4th digit value to be displayed
    if cntr > 0 then
    digitnum[3] = TempNum
    else
    digitnum[3] = 0
    endif

    for displaynums = 0 to 3
    dig1addr = 192 + displaynums

    LOOKUP digitnum[displaynums],[63,48,91,79,102,109,125,7,127,111],dispnum

    'Values which produce segments for numbers 0-9
    '0: 63
    '1: 48
    '2: 91
    '3: 79
    '4: 102
    '5: 109
    '6: 125
    '7: 7
    '8: 127
    '9: 111

    '================== Display 4 digits ========================
    '1st transaction value must always be x40
    bytetosend = 64
    gosub startcondition
    gosub sendthebyte
    gosub stopcondition

    '2nd transaction value must be which digit (A-D)to update
    ' The following values represent which digit
    ' One of these valuse will be loaded into Dig1Addr
    ' 192 = 11000000 digit A
    ' 193 = 11000000 digit B
    ' 194 - 10100000 digit C
    ' 195 - 10010000 digit D

    bytetosend = Dig1Addr
    gosub startcondition
    gosub sendthebyte

    ' 3rd transaction value represents which number to display
    ' within the designated digit
    bytetosend = dispnum
    gosub sendthebyte
    gosub stopcondition

    ' 4th transaction value stipulates LED brightness
    bytetosend = brt8cmd
    gosub startcondition
    gosub sendthebyte
    gosub stopcondition


    next displaynums
    pause 1000
    'next B0




    END

  2. #2
    Join Date
    Feb 2013
    Posts
    868


    Did you find this post helpful? Yes | No

    Default Re: Here is example code - PIC12F683 4-dig 7-Segment display - driven by TM1637

    Thanks!
    Nice to see anyone doing new PBP code in 2021.
    What are the advantages over the MAX7219 modules?

  3. #3
    Join Date
    Jan 2007
    Posts
    78


    Did you find this post helpful? Yes | No

    Default Re: Here is example code - PIC12F683 4-dig 7-Segment display - driven by TM1637

    Thanks I just saw this.
    I'm not familiar with the MAX chip.
    But you're mentioning it - made me google it - and I do see one or two of these already integrated into a 7 seg display.
    Perhaps I'll get a chance to play with this
    I'm working on a 7 segment driven by an HT16k33

    There are times when developing something is easier in the Arduino platform - with the intention of porting it over to PIC.
    PICs are awesome because of their small foot-print.

  4. #4
    Join Date
    Nov 2019
    Posts
    20


    Did you find this post helpful? Yes | No

    Default Re: Here is example code - PIC12F683 4-dig 7-Segment display - driven by TM1637

    Quote Originally Posted by CuriousOne View Post
    Thanks!
    Nice to see anyone doing new PBP code in 2021.
    What are the advantages over the MAX7219 modules?
    Probably no functional advantages, but the 4-digit 7-segment TM1637 displays are dirt cheap. I have found them for about $1 on ebay from China. I used them in a small engine RPM indictor, and they worked great.

Similar Threads

  1. TM1637 - display module include example
    By longpole001 in forum Code Examples
    Replies: 5
    Last Post: - 13th April 2020, 18:19
  2. How to use TM1637 chip for LED Display?
    By Balachandar in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 18th October 2018, 04:14
  3. 3642bh display module pinouts on tm1637 module
    By longpole001 in forum Schematics
    Replies: 0
    Last Post: - 31st July 2018, 18:09
  4. 6 digit - 7 segment display code
    By lerameur in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th June 2014, 06:36
  5. Driver code for 14 segment LED display
    By Durward Searcy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th December 2004, 20:08

Posting Permissions

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