Menu

InitTimer1

2019-01-21
2019-01-23
  • Jim giordano

    Jim giordano - 2019-01-21

    In another thread I posted about a problem but I think it got lost in the noise.

    When using the instruction-

    InitTimer1 ExtOsc,0

    on a 16F18426, it sets TMR1CLK incorrectly to 3 (HFINTOSC), it should be 7 (SOSC).

     
    • Anobium

      Anobium - 2019-01-21

      Yes, this is explained in the Help.

       
  • Jim giordano

    Jim giordano - 2019-01-21

    Yes, the help says
    "ExtOsc is an external oscillator which is the same source as SOSC"
    Which it is not being set to.

     
    • Anobium

      Anobium - 2019-01-21

      Can you create the smallest program to show this? Then, we may be able to resolve.

      Cheers.

       
  • Jim giordano

    Jim giordano - 2019-01-21

    Yes, but I want to test it to be sure. Should be ready in about 30 mins.

     
  • Jim giordano

    Jim giordano - 2019-01-21

    Here it is. A little larger than optimal, but I thought it was important to actually print out the value, and it takes a lot of code to set up the lcd display.

    #chip 16F18426,32
    
    '#config RSTOSC=HFINT32, MCLRE=OFF
    #config FEXTOSC=OFF ', CLKOUTEN=OFF
    #startup InitPPS, 85
    
    Sub InitPPS
      UNLOCKPPS
        SSP1CLKPPS = 0x0010    'RC0 > SCL1
        RC0PPS = 0x0013    'SCL1 > RC0 (bi-directional)
        SSP1DATPPS = 0x0011    'RC1 > SDA1
        RC1PPS = 0x0014    'SDA1 > RC1 (bi-directional)
      LOCKPPS
    End sub
    
    #define HI2C_BAUD_RATE 400
    #define HI2C_DATA PORTC.1
    #define HI2C_CLOCK PORTC.0
    Dir HI2C_DATA in  'I2C pins need to be input
    Dir HI2C_CLOCK in
    HI2CMode Master
    #define LCD_IO 10
    #define LCD_SPEED FAST 'FAST ' SLOW or MEDIUM
    
    DIR PORTA.4 IN  ' 32k crystal
    DIR PORTA.5 IN  ' 32k crystal
    
    InitTimer1 ExtOsc,0  ' 32768 crystal time keeper
    
     '' *** To see it run correctly, uncomment the next line.
    '  TMR1CLK=b'00000111'  'override incorrect InitTimer1 value
    
    cls
    locate 0,0
    print "TMR1CLK="
    print TMR1CLK
    
    On Interrupt Timer1Overflow Call CheckClock
    
    ClearTimer 1
    StartTimer 1
    
    locate 2,0
    print "Timer 1 is"
    locate 3,0
    print " not running"
    
    do
      if DoClock then
        DoClock=0
        locate 3,0
        print " running correctly."
      end if
    loop
    
    sub CheckClock
      TMR1H = 128 'TMR1H = 0x80    ' set up for one second delay
      DoClock=1   ' time to do something
    end sub
    
     
  • Anobium

    Anobium - 2019-01-21

    @Jim.

    Really good catch by you - we can resolve this but first some background.

    As Microchip creates new microcontrollers they do not always maintain a common approach. An example is the SMTTimer - this timer has four different truth tables to provide the same information. So, when we implemented SMTTimers we map the SMTTimer truth table to our microcontroller .dat file.

    Well, you found that the Timer sources are different also. So, know we know this the fix (for the everyone in the future) is relatively simple.

    Let us look at two microcontrollers - the 16f18426 and the 16f18855 - the Timer clock source is shown side by side below.

    So, now we know this information we create the correct information in the library for all the case - there are four cases.

    Test code - used a the baseline.

    #chip PIC16F18426,32
    InitTimer1 SOSC,0
    

    Release code generates a SOSC with a constant of 6.

            ;InitTimer1 SOSC,0
              movlw 6
              movwf TMRSOURCE
              clrf  TMRPRES
              call  INITTIMER1
    

    With the fix. Where SOSC is a constant of 7.

        ;InitTimer1 SOSC,0
            movlw   7
            movwf   TMRSOURCE
            clrf    TMRPRES
            call    INITTIMER1
    

    So, to resolve. We need to release new dat files (this then has the required key information) and a new timer library with the script to assign the correct constants.

    So, (@Jim) if you are up for a quick validate test I can provide the dat file and the h for you to confirm things.

    Anobium

     

    Last edit: Anobium 2019-01-21
  • Jim giordano

    Jim giordano - 2019-01-21

    Sounds good to me. We all appreciate your efforts to make GCB great!

     
    • Anobium

      Anobium - 2019-01-21

      You spot the variance not me... :-)

      All I do is digitise the knowledge into the libraries. :-)

      Send me a Personal Message with your email address. I will send a URL to your email address.

       
  • Anobium

    Anobium - 2019-01-22

    The root cause is a different implemention of the timer clock sources as described previously.

    Microchip creates new microcontrollers they do not always maintain a common approach (with respect to clock timer sources).

    The resolution is an adpation to the timer library to define constants to support this specific implementation.

    You will need to use an updated timer.h. Download here: This update will be include in releases after v0.98.04

    InitTimer1 ExtOsc,0 and InitTimer1 SOSC,0 where the clock is ExtOsc or SOSC are the same (as per the Help).

    Example and tested code for a chip in the same family.

    #chip 16F18446,32                'Specify the Device you are compiling for - this board is the 16F18446
    #config FEXTOSC=Off              'Required to support the use of the
                                     'Great Cow BASIC simplifies the configuration by setting the frequency and the configuration for you.
    #option Explicit                 'This option ensures that all variables are dimensioned in the user program.  See http://gcbasic.sourceforge.net/help/__option_explicit.html
    
    #define LED0 porta.2             'Set a constant to refer to the LED.  RA2 is a Yellow LED called LED0 Edge connector, see section 3.3.1 of the MPLAB-Xpress-PIC16F18446-Evaluation-Board.pdf
    dir LED0 out                     'Set LED as an output
    
    InitTimer1 SOSC,0                ' 32768 crystal time keeper
    On Interrupt Timer1Overflow Call CheckClock
    SetTimer 1, 0x8000                'TMR1H = 0x80    ' set up for one second delay.
    StartTimer 1
    
    do
    loop
    
    sub CheckClock
      SetTimer 1, 0x8000              'TMR1H = 0x80    ' set up for one second delay
    
      LED0=!LED0
    end sub
    
     

    Last edit: Anobium 2019-01-22
  • Anobium

    Anobium - 2019-01-23

    My reference MPLAB-X reference project. I used this to ensure I had the correct registers etc.

    Enjoy.

     
  • Jim giordano

    Jim giordano - 2019-01-23

    Thanks Evan. My timer1 problems have all been resolved.

     
  • Anobium

    Anobium - 2019-01-23

    @Jim. Thank you for your support and patience!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.