News:

;) This forum is the property of Proton software developers

Main Menu

TM1637 problem

Started by Maxi, Aug 11, 2022, 11:29 AM

Previous topic - Next topic

Maxi

Hi, some help need
I use just 3 digit and change @Rizwan s shaikh code
https://protoncompiler.com/index.php/topic,830.0.html

its working 16F873A on the proteus well
but, this code problem on 12F1822
where is my mistake?

here is working code 16F873A
Device = 16F873A
Declare Xtal = 20
'****************************************************************
Config HS_OSC,WDT_OFF,PWRTE_Off,BODEN_On,LVP_OFF,WRT_OFF,CP_ALL
Declare All_Digital = TRUE
'****************************************************************
TRISA = %00000000:PORTA = %00000000
TRISB = %00000001:PORTB = %00000001
TRISC = %00000000:PORTC = %00000000
'****************************************************************
Dim DIG1_DATA1 As Byte
Dim DIG2_DATA2 As Byte
Dim DIG3_DATA3 As Byte
Dim DIG4_DATA4 As Byte
Dim DIG5_DATA5 As Byte
Dim DIG6_DATA6 As Byte
Dim OP_VAR1    As Byte
Dim DIGIT      As Byte
Dim PATTERN    As Byte
'****************************************************************
Declare HSDA_Pin PORTC.4
Declare HSCL_Pin PORTC.3
Declare Hbus_Bitrate 100
'***************************************************************
OP_VAR1 = 123
'****************************************************************
MAIN:
GoSub Update_Display
DelayMS 10

GoTo MAIN
'****************************************************************
Update_Display:
'----------------------------------------------------------------
DIGIT = Dig OP_VAR1,2
GoSub CONVERT
DIG2_DATA2 = PATTERN
'----------------------------------------------------------------
DIGIT = Dig OP_VAR1,1
GoSub CONVERT
DIG3_DATA3 = PATTERN
'----------------------------------------------------------------
DIGIT = Dig OP_VAR1,0
GoSub CONVERT
DIG4_DATA4 = PATTERN
'----------------------------------------------------------------

HBStart                  
HBusOut %01000100      'Data command setting
HBusAck

HBStart              
HBusOut %10001111     'Display control
HBusAck
HBStop
'----------------------------------------------------------------
HBStart              
HBusOut %10000011     'dig 2
HBusOut DIG2_DATA2
HBusAck
HBStop
'----------------------------------------------------------------
HBStart              
HBusOut %01000011     'dig 3
HBusOut DIG3_DATA3 + 1'(+ 1 FOR DP)
HBusAck
HBStop
'----------------------------------------------------------------
HBStart              
HBusOut %11000011     'dig 4
HBusOut DIG4_DATA4
HBusAck
HBStop
'----------------------------------------------------------------

Return
'****************************************************************
CONVERT:               ' 0  1   2   3   4   5   6  7   8   9
PATTERN=LookUpL DIGIT,[$FC,$60,$DA,$F2,$66,$B6,$BE,$E0,$FE,$F6]
Return
'****************************************************************


and here is the problem 12F1822 code
Device = 12F1822
Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_OFF, CP_ON, CPD_ON, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_OFF, STVREN_OFF, BORV_19, LVP_OFF
Declare Xtal=8

OSCCON.6=1
OSCCON.5=1
OSCCON.4=1
OSCCON.3=0

OSCCON.1=1
TRISA=%000000:PORTA=%000000
Declare All_Digital= true

'****************************************************************
Dim DIG1_DATA1 As Byte
Dim DIG2_DATA2 As Byte
Dim DIG3_DATA3 As Byte
Dim DIG4_DATA4 As Byte
Dim OP_VAR1    As Byte
Dim DIGIT      As Byte
Dim PATTERN    As Byte
'****************************************************************
Declare HSDA_Pin PORTA.2
Declare HSCL_Pin PORTA.1
Declare Hbus_Bitrate 100
'***************************************************************
OP_VAR1 = 123
'****************************************************************
MAIN:
GoSub Update_Display
DelayMS 10

GoTo MAIN
'****************************************************************
Update_Display:
'----------------------------------------------------------------
DIGIT = Dig OP_VAR1,2
GoSub CONVERT
DIG2_DATA2 = PATTERN
'----------------------------------------------------------------
DIGIT = Dig OP_VAR1,1
GoSub CONVERT
DIG3_DATA3 = PATTERN
'----------------------------------------------------------------
DIGIT = Dig OP_VAR1,0
GoSub CONVERT
DIG4_DATA4 = PATTERN
'----------------------------------------------------------------

HBStart                  
HBusOut %01000100      'Data command setting
HBusAck

HBStart              
HBusOut %10001111     'Display control
HBusAck
HBStop
'----------------------------------------------------------------
HBStart              
HBusOut %10000011     'dig 2
HBusOut DIG2_DATA2
HBusAck
HBStop
'----------------------------------------------------------------
HBStart              
HBusOut %01000011     'dig 3
HBusOut DIG3_DATA3 + 1'(+ 1 FOR DP)
HBusAck
HBStop
'----------------------------------------------------------------
HBStart              
HBusOut %11000011     'dig 4
HBusOut DIG4_DATA4
HBusAck
HBStop
'----------------------------------------------------------------

Return
'****************************************************************
CONVERT:               ' 0  1   2   3   4   5   6  7   8   9
PATTERN=LookUpL DIGIT,[$FC,$60,$DA,$F2,$66,$B6,$BE,$E0,$FE,$F6]
Return
'****************************************************************

keytapper

Declare HSDA_Pin PORTA.2
Declare HSCL_Pin PORTA.1

Without much tinkering I presume that the compile won't set the pins for you. It's better you check the APFCON setting and the TRISA relative pins.
See the relevant chapter on the datasheet regarding the I2C usage.
Ignorance comes with a cost

Maxi

@keytapper configs must be correct for 12F1822
because Im trying now @RGV250 codes, and 12F1822 works
but now I have DP problem :(
want 12.3

here is the working code for 12F1822 without DP (point less)
Device = 12F1822
Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_ON, MCLRE_OFF, CP_ON, CPD_ON, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_OFF, STVREN_OFF, BORV_19, LVP_OFF
Declare Xtal=8

OSCCON.6=1
OSCCON.5=1
OSCCON.4=1
OSCCON.3=0

OSCCON.1=1
TRISA=%000000:PORTA=%000000
Declare All_Digital= true
  
'Setup the default pins for SDA and SCL
$define TM1637_SDA_Pin PORTA.2           'Set the SDA pin
$define TM1637_SCL_Pin PORTA.1           'Set the SCL pin
  
'----------------------------------------------------------------------------------------------
'Delay a fixed number of microseconds
'
$define TM1637_hDelay() DelayUS 100      '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.
'----------------------------------------------------------------------------------------------  
'Variables
    Dim TestData As Byte
    TestData = 0
'----------------------------------------------------------------------------------------------
'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 | %10001000
    Else
    DataByte = %10000000
    EndIf
'Send the data  
    TM1637_Start()
    TM1637_WriteByte(DataByte)        
    TM1637_Ack()
    TM1637_Stop()
EndProc

'---------------------------------------------------------------------------------------------
Proc UpdateDisplay(pData As Word)        'Write display register
    Dim index As Byte
    Dim BitPattern As Byte
    Dim TempArray[4] As Byte             'Create a temporary array for individual characters
   
'Split the value into individual numbers for writing to display.
    If Dig pData,3 = 0 Then
    TempArray#3 = 10
    Else
    TempArray#3 = Dig pData,3
    EndIf
  
    If Dig pData,2 = 0 Then
    TempArray#2 = 0
    Else
    TempArray#2 = Dig pData,2
    EndIf  
  
    If Dig pData,1 = 0 Then
    TempArray#1 = 0
    Else
    TempArray#1 = Dig pData,1
    EndIf  
  
    If Dig pData,0 = 0 Then
    TempArray#0 = 0
    Else
    TempArray#0 = Dig pData,0
    EndIf
  
'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 = 3 To 0 Step -1           'Will need to modify if using displays with more than 4 digits.
        BitPattern = LRead SegmentData + TempArray[Index]
        TM1637_WriteByte(BitPattern)
        TM1637_Ack()
    Next index
   
    TM1637_Stop()
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

'Zero the display at the start.
'Send data to the TM1637 module   
    UpdateDisplay(TestData)        
      
'---------------------------------------------------------------------------------------------
Main:
    SetBrightness(4,1)                   'Set the brightness 0 - 7, 1 = On, 0 = Off.
   
'Send data to the TM1637 module   
    UpdateDisplay(TestData)        
  
    DelayMS 10
    'Inc TestData
  
    'If TestData > 2222 Then              'Rollover to test the preceeding zeroes
    'TestData = 0
    'EndIf
    TestData=123
   
    GoTo Main                            'Do forever.
  
'---------------------------------------------------------------------------------------------  
  
'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       10 empty
SegmentData:
    LData As Byte %00111111, %00000110, %01011011, %01001111, %01100110, %01101101, %01111101, %00000111, %01111111, %01101111, %00000000

RGV250

Hi Maxi,
I never bothered with DP as I did not need it for my project, I will have a look and see if I can give you any pointers. Let me know if you sort it in the mean time so I do not waste time.

Bob

RGV250

#4
Hi,
Looking at the other code it looks like all you need to do is add 1 to the actual data.
QuoteHBusOut DIG3_DATA3 + 1'(+ 1 FOR DP)

If the DP is fixed you could try this (commented out original code)
Not tested.

    If Dig pData,1 = 0 Then
    TempArray#1 = 1    '0
    Else
    TempArray#1 = Dig pData+1,1  'pData,1
    EndIf   

Bob

RGV250

Hi,
I had one thought, if this does work you will need to format your value to an integer before passing it to the procedure or modify the procedure to accept floats.

Bob

keytapper

Quote from: RGV250 on Aug 11, 2022, 01:33 PMHi Maxi,
I never bothered with DP
For the DP, just set the bit 7 high, via OR or via setbit. Unless one more set of 7SD definitions with the DP included.
I made a project that use a direct driving to the 7SD, but the way to add the DP is the same.
So at the digit that you expect a DP, just add 128 to the digit to be displayed, and the dot will appear, as long as you had connected them.
Ignorance comes with a cost

Maxi

TempArray#1 = Dig pData+1,1  'pData,1
not work, yesterday I tested
for example, second digit 2, now its 3


bit 7 to high not work too
I trying with setbit
for example, second digit 2, now its 130
(dec:2 = binary:10)
(dec:2 = with 7bit high binary:10000010 dec:130)
no any 130 in the ldata

but if I change 7. bit ldata it work
for example 2
%01011011 no dp
%11011011 dp on

what Im missing?

RGV250

Hi,
Have you tried adding 128 as Keytapper said, do the same as I suggested but 128 instead of 1.

Bob

Maxi

#9
Problem solved.
Thank you guys

Im add this line and welcome DP ;D


'---------------------------------------------------------------------------------------------
Proc UpdateDisplay(pData As Word)        'Write display register
    Dim index As Byte
    Dim BitPattern As Byte
    Dim TempArray[4] As Byte            'Create a temporary array for individual characters
   
'Split the value into individual numbers for writing to display.
    If Dig pData,3 = 0 Then
    TempArray#3 = 10
    Else
    TempArray#3 = Dig pData,3
    EndIf
 
    If Dig pData,2 = 0 Then
    TempArray#2 = 0
    Else
    TempArray#2 = Dig pData,2
    EndIf 
 
    If Dig pData,1 = 0 Then
    TempArray#1 = 0
    Else
    TempArray#1 = Dig pData,1 
    EndIf 
 
    If Dig pData,0 = 0 Then
    TempArray#0 = 0
    Else
    TempArray#0 = Dig pData,0
    EndIf
 
'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 = 3 To 0 Step -1          'Will need to modify if using displays with more than 4 digits.
       
        BitPattern = LRead SegmentData + TempArray[Index]
        If index=1 Then SetBit bitpattern,7
        TM1637_WriteByte(BitPattern)
        TM1637_Ack()
    Next index
   
    TM1637_Stop()
EndProc   

'---------------------------------------------------------------------------------------------



Rizwan s shaikh

Tray thih