TM1637 code - Page 2


+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17

Thread: TM1637 code123 days old

  1. #16
    Prolific Poster See_Mos's Avatar
    Join Date
    Feb 2004
    Posts
    1,327
    Thumbs Up
    Received: 19
    Given: 0
    Total Downloaded
    1.01 GB

    1 Not allowed!

    Default Re: TM1637 code

    That is the same as the second one that I bought. If you modify Bob's code the colon lights. The same method should work for the decimal point displays when they arrive.

    Code:
                For Index = 3 To 0 Step -1           'Will need to modify if using displays with more than 4 digits.
                    BitPattern = LRead SegmentData + TempArray[Index]
                    If Index = 2 Then
                        BitPattern = BitPattern + 128
                    EndIf
                    TM1637_WriteByte(BitPattern)  
                    TM1637_Ack() 
                Next index
    Last edited by See_Mos; 10th April 2020 at 14:24.
    My RAM is failing

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #17
    Prolific Poster See_Mos's Avatar
    Join Date
    Feb 2004
    Posts
    1,327
    Thumbs Up
    Received: 19
    Given: 0
    Total Downloaded
    1.01 GB

    0 Not allowed!

    Default Re: TM1637 code

    I bought one of the cheap TM1637 modules that have six digits and six buttons.

    Be sure to check the connections to the board as they vary between the different types.

    Buttons 0 and 1 are not used on the PCB and on my board K2 for the external buttons is marked as being at the right of the connector but is actually at the other end which is marked as SG1

    There is a new procedure for Keyscan and a modified procedure UpdateDisplay as I could not get the leading zeroís to blank with Bob's code

    I also use an LCD to show the key press while the main loop is counting up

    The code below is a modification to Bobís code above.

    Code:
      
    ' See_mos version for 6 digit display plus buttons
    
    '            Include "Amicus18.inc"     'Setup the program to run on an
                                            ' Amicus18 board(PIC18F25K20 at 64MHz)
            '----------------------------------------------------------------------                                        
            ' use See_mos .inc which adds LCD by port                                
            $define _LCD_PORT_ 1                    ' LCD PortA = 0, PortB = 1, PortC = 2
            Include "18F25K22 IntOsc 64.inc"        ' setup LCD etc.
            '----------------------------------------------------------------------
                 
            'Setup the default pins for SDA and SCL
            $define TM1637_SDA_Pin PORTC.4           'Set the SDA pin 
            $define TM1637_SCL_Pin PORTC.3           'Set the SCL pin 
                
            '----------------------------------------------------------------------
            'Delay a fixed number of microseconds
            'It worked At 2 in ISIS VSM but didn't work at 50 on real hardware.
            'Unsure why it did not work but this is fast enough.
            $define TM1637_hDelay() DelayUS 100      
                                                     
            '----------------------------------------------------------------------  
            'Variables
            ' change from from Word to DWord        
                Dim TestData As Dword
                Dim Keypress As Byte
            '---------------------------------------------------------------------
            'Send a start condition
            'Input     : None
            'Output    : None
            'Notes     : A Start condition is high to low of SDA when SCL is high
            '
            Proc TM1637_Start()
                Input TM1637_SDA_Pin            'Make the SDA pin an Input (High)
                Input TM1637_SCL_Pin            'Make the SCL pin an Input (High)
                TM1637_hDelay()                 
                Output TM1637_SDA_Pin           'Make the SDA pin an Output (Low)
                TM1637_hDelay()
                Output TM1637_SCL_Pin           'Make the SCL pin an Output (Low)
            EndProc
    
            '-------------------------------------------------------------------
            'Initiate an acknowledge
            'Input     : None
            'Output    : None
            'Notes     : None
            '
            Proc TM1637_Ack()
                Output TM1637_SDA_Pin          'Make the SDA pin an Output (Low)
                Input TM1637_SCL_Pin           'Make the SCL pin an Input (High)
                TM1637_hDelay()
                Output TM1637_SCL_Pin          'Make the SCL pin an Output (Low)
                DelayUS 5                      'After the falling edge of the 
                                               ' eighth clock delay For 5us.
                Input TM1637_SDA_Pin           'Make the SDA pin an Input (High)
            EndProc
    
            '---------------------------------------------------------------------
            'Send a stop condition
            'Input     : None
            'Output    : None
            'Notes     : A Stop condition is low to high of SDA when SCL is high
            '
            Proc TM1637_Stop()
                Output TM1637_SDA_Pin            'Make the SDA pin an Output (Low)
                TM1637_hDelay()
                Input TM1637_SCL_Pin             'Make the SCL pin an Input (High)
                TM1637_hDelay()
                Input TM1637_SDA_Pin             'Make the SDA pin an Input (High)
                TM1637_hDelay()
            EndProc
    
            '-------------------------------------------------------------------
            'Send single bytes to the TM1637 module.
            'Input     : pData holds the byte to write
            'Output    : None
            'Notes     : LSB first
            '
            Proc TM1637_WriteByte(pData As Byte)
    
                Dim bIndex As Byte = 8
                Repeat                           'Create a loop for the 8-bits
                    If pData.0 = 1 Then
                        Input TM1637_SDA_Pin     'Make the SDA pin an Input (High)
                    Else
                        Output TM1637_SDA_Pin    'Make the SDA pin an Output (Low)
                    EndIf
                    TM1637_hDelay()
                    Input TM1637_SCL_Pin         'Make the SCL pin an Input (High)
                    TM1637_hDelay()
                    Output TM1637_SCL_Pin        'Make the SCL pin an Output (Low)
                    TM1637_hDelay()
                    pData = pData >> 1
                    Dec bIndex
                Until bIndex = 0
    
            EndProc
    
            '---------------------------------------------------------------------
            'Set brightness
            'Send a single byte to the TM1637 module for the brightness.
            'Input     : pData1 holds the brightness data, pData2 is the "On/Off" control.
            'Output    : None
            'Notes     :     
            'Values > 7 will turn display off.
            'Address bits b7 / b6 = 10
            'b5 / b4 don't care, fill with 0
            'b3 On / Off control - 1 = On / 0 = Off
            'b2 - b0 is the data
            '000 = 1/16
            '001 = 2/16 
            '010 = 4/16
            '011 = 10/16 
            '100 = 11/16 
            '101 = 12/16
            '110 = 13/16 
            '111 = 14/16 
            Proc SetBrightness(pData1 As Byte, pData2 As Byte)  
            Dim DataByte As Byte
            Dim ControlBit As Byte
            DataByte = pData1
            Controlbit = pData2
    
            'Check if the display should be On or Off.
                If ControlBit = 1 Then 
                DataByte = Databyte | 001000  
                Else
                DataByte = 000000
                EndIf
            'Send the data    
                TM1637_Start() 
                TM1637_WriteByte(DataByte)         
                TM1637_Ack() 
                TM1637_Stop() 
            EndProc
    
            '---------------------------------------------------------------------
            Proc UpdateDisplay(pData As Dword)        'Write display register 
                Dim index As Byte
                Dim BitPattern As Byte
    
            'Set data command mode.
            '40H is automatic increment by 1.
            '44H is fixed address.
                TM1637_Start() 
                TM1637_WriteByte(0x40)  
                TM1637_Ack() 
                TM1637_Stop() 
            'Set the first address    
                TM1637_Start() 
                TM1637_WriteByte(0xc0)  
                TM1637_Ack()
            'Send the data 
            
                For Index = 5 To 0 Step -1
                    BitPattern = LRead SegmentData + Dig pdata,index
    
                    If pData < 100000 And Index = 5 Then BitPattern = 0
                    If pData < 10000 And Index = 4 Then BitPattern = 0
                    If pData < 1000 And Index = 3 Then BitPattern = 0
                    If pData < 100 And Index = 2 Then BitPattern = 0
                    If pData < 10 And Index = 1 Then BitPattern = 0
                    
                    If Index = 2 Then                ' add decimal point
                        BitPattern = BitPattern + 128
                    EndIf
                    TM1637_WriteByte(BitPattern)  
                    TM1637_Ack() 
                Next index
                
                TM1637_Stop() 
            EndProc    
    
            '---------------------------------------------------------------------
        Proc Scankey(),Byte
            Dim Key_Press As Byte
            Dim Scan_Bit As Byte
            TM1637_Start() 
            TM1637_WriteByte(0x42)  
            TM1637_Ack() 
    
            Input TM1637_SDA_Pin
    
            For Scan_Bit = 0 To 7
                Input TM1637_SCL_Pin
                Key_Press = Key_Press >> 1 
                DelayUS 30
                Output TM1637_SCL_Pin
                
                If TM1637_SDA_Pin = 1 Then
                    Key_Press=Key_Press|0x80
                Else
                    Key_Press=Key_Press|0x00
                EndIf
                
                DelayUS 30
            Next
    
            TM1637_Ack() 
            TM1637_Stop()
            
    ' the returned value is from 247 for the left key to 242 for the right key on
    ' the PCB and 231 to 238 for the external connector
            If Key_Press = $FF Then                 ' no key pressed
                Result = 0
            Else
    '            Key_Press = Key_Press & 000111   ' remove the high bits PCB only
                Key_Press = Key_Press & 001111    ' or all sixteen
                Result = Key_Press - 1              ' change from 2 - 7 to 1 - 6
            EndIf    
        EndProc
        
            '--------------------------------------------------------------------- 
            'Start
            '---------------------------------------------------------------------            
                Low TM1637_SDA_Pin           'Make the TM1637 SDA pin an output low
                Low TM1637_SCL_Pin           'Make the TM1637 SCL pin an output low
            
                DelayMS 200                ' allow LCD to stabilise            
                Cls
                Print At 1,1, "6 Digit plus buttons"
            '---------------------------------------------------------------------
    Main:  
        SetBrightness(2,1)              'Set the brightness 0 - 7, 1 = On, 0 = Off.
        TestData = 0                    ' start at 0
        UpdateDisplay(TestData)         ' Send data to the TM1637 module         
        DelayMS 2000                    ' see it on the display
        
        While 1 = 1
            Inc TestData
            UpdateDisplay(TestData)     'Send data to the TM1637 module 
                                 
            If TestData > 123456 Then   'Rollover to test the preceeding zeroes
                DelayMS 2000
                TestData = 0
                DelayMS 2000
            EndIf
            
            Keypress = Scankey()
            Print At 2,1,"Key ",Dec Keypress,"   "
        Wend            
            '-----------------------------------------------------------------------
               
            'Bit patterns for numbers 0 - 9
            'Some of my TM1637 modules do not have a DP but have a colon,
            ' I will not be using either 
            'so I have not done the code for that at the moment.
            'This was copied from some other code somewhere.
            '       __A__
            '      |     |
            '      F     B
            '      |__G__|
            '      |     |
            '      E     C
            '      |__D__| X
            '      
            'XGFEDCBA             0          1          2          3          4          5          6          7         8           9    
            SegmentData:
                LData As Byte 111111, 000110, 011011, 001111, 100110, 101101, 111101, 000111, 111111, 101111
    Click image for larger version

Name:	P1020170.jpg
Views:	11
Size:	92.3 KB
ID:	3962
    Last edited by See_Mos; 1st June 2020 at 19:58.
    My RAM is failing

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Delay time not used in source code - pse read code for mistake
    By scdhome in forum Proton Plus Compiler v3
    Replies: 6
    Last Post: 8th October 2012, 17:55
  2. [SOLVED !] convert Basic stamp 2 code to PDS code
    By billyminor in forum Proton Plus Compiler v3
    Replies: 31
    Last Post: 24th June 2007, 20:07
  3. CODE CHALLENGE: SCP1000 Barometric Sensor Code
    By ohararp in forum Proton Plus Compiler v3
    Replies: 23
    Last Post: 26th April 2007, 00:32
  4. Self modifying code / Writing to code memory
    By geraintwalesuk in forum Proton Plus Compiler v3
    Replies: 0
    Last Post: 16th September 2006, 00:34
  5. [SOLVED !] Device Code/button Code
    By insury in forum Proton-IR
    Replies: 3
    Last Post: 14th December 2004, 21:38

Posting Permissions

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