Elapsed timer not working as expected at 64MHz


Closed Thread
Results 1 to 40 of 56

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working at 64MHz

    So I minimized the code:

    Code:
    asm
     __CONFIG    _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_OFF_1H & _FCMEN_ON_1H & _IESO_OFF_1H
     __CONFIG    _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
     __CONFIG    _CONFIG2H, _WDTEN_OFF_2H
     __CONFIG    _CONFIG3H, _CCP2MX_PORTC1_3H & _PBADEN_OFF_3H & _CCP3MX_PORTE0_3H & _HFOFST_OFF_3H & _T3CMX_PORTB5_3H & _P2BMX_PORTC0_3H & _MCLRE_EXTMCLR_3H
     __CONFIG    _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    endasm
    
    DEFINE OSC 16
    OSCCON  = %01010000
    '     R/W       111 = HFINTOSC – (16 MHz)
    '               110 = HFINTOSC/2 – (8 MHz)
    '               101 = HFINTOSC/4 – (4 MHz)
    
    OSCTUNE = %11000000
    OSCCON2 = %00000100
    INCLUDE "DT_INTS-18.bas"
    INCLUDE "ReEnterPBP-18.bas"
    INCLUDE "Elapsed_INT-18.bas"
    ASM
    INT_LIST  macro    ; IntSource,        Label,           Type,   ResetFlag?
            INT_Handler   TMR1_INT,   _ClockCount,   PBP,  yes
        endm
        INT_CREATE
    ENDASM
    ANSELA = %00000000
    ANSELB = %00000000
    ANSELC = %00000000
    ANSELD = %00000000
    ANSELE = %00000000
    ADCON0 = %00000000
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %00000000
    TRISD = %00000000
    TRISE = %00000000
    PORTA = %00000000
    PORTB = %00000000
    PORTC = %00000000
    PORTD = %00000000
    PORTE = %00000000
        pause 100
    @ INT_ENABLE  TMR1_INT 
        GOSUB ResetTime
    MainProgram:
    
        if T1CON.0 = 0 then
            PortD.3 = 1
            GOSUB StartTimer
        endif
    
        if ticks = 5 then
            PortD.3 = 0
            GOSUB StopTimer                    ' Stop the Elapsed Timer
            GOSUB ResetTime                     ' Reset Time to  0d-00:00:00.00
        endif
    
        goto MainProgram
    end
    5 TICK Timer

    OSC 16MHz (4MHz+PLL) 50ms
    OSC 32MHz (8MHz+PLL) 25ms
    OSC 64MHz (16MHz+PLL) 12.5ms

    I can't help but feel I'm doing something wrong, it's just too proportional a variance. If the Saleae probe would miss pulses, there wouldn't be a regular pattern.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,719


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    debug T1PS and TimerConst to see what's going on would be a good start

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Roger, oveur.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  4. #4
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    yep there was no elapsed-18_k22

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,719


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    seems to me the easiest way to restore correct function for "ticks" when the post scaler is in use is to move the ticks loop inside the post scale loop , no extra vars no extra overhead

    Code:
    ClockCount:
    @ RELOAD_TIMER                   ; Reload TIMER1
    
        T1Post = T1Post + 1
        IF T1Post = T1PS THEN
          T1Post = 0
         Ticks = Ticks + 1
            IF Ticks = 100 THEN
               Ticks = 0
               Seconds = Seconds + 1
               SecondsChanged = 1
               IF Seconds = 60 THEN
                  Seconds = 0
                  Minutes = Minutes + 1
                  MinutesChanged = 1
               ENDIF
               IF Minutes = 60 THEN
                  Minutes = 0
                  Hours = Hours + 1
                  HoursChanged = 1
               ENDIF
               IF Hours = 24 THEN
                  Days = Days + 1
                  DaysChanged = 1
                  Hours = 0
               ENDIF
           ENDIF
        ENDIF
    @ INT_RETURN                     ; Restore context and return from interrupt

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    I can only think that there was a reason why Darrel did it as he did. The dude could probably code this stuff while playing ping pong; the Bruce Lee of PBP and ASM.

    There's more than one way to solve a problem, he probably took the one that worked for all PICs (until the K22 series came along). And even that he's solved the problem, it just wasn't widely known. I can only think he had greater concerns on his mind. Can you imagine how much of a headache it must have been tracking the new releases versus his routines? Makes me appreciate and miss him even more.

    I now fear the day when Microchip releases a new product line. I love the K22 like I loved the 16F628 and 16F877, I'm not changing unless I'm forced to change.

    Shoot, I might even stay on PBP 2.6. LOL

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,719


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    it has nothing to do with the k22's its the problem exists whenever the T1PS value is >1 on any chip
    its all to do with the way he implemented the post scaling , the loops are inside out . its no bigie to fix . its only apparent if you need to use "ticks" .
    imho most of the time using ticks is pointless by the time you process the reading and then display it it's already meaningless and out of date . if you want to time events to such accuracy trigger times need to be allowed for and properly sync'ed , there are much better ways
    ps
    the k22 interrupt vagaries are a separate issue and unrelated to ticks not being 100th's of a sec
    Last edited by richard; - 24th December 2014 at 03:16. Reason: ps

  8. #8
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Quote Originally Posted by richard View Post
    debug T1PS and TimerConst to see what's going on would be a good start
    I started by making my version of the Elapsed include and adding 2 variables at the end of the timer calc:
    Code:
    ...
    ' -------------- calc timer reload Constants -------------------------------
    ASM
    T1PS = 1                             ; start with 1:1 postscaler
    TimerConst = ((OSC*1000000)/4/100)   ; how many timer ticks will it take
      while TimerConst > 65400           ; if it's more than the timer can count
    T1PS = T1PS * 2                      ;   double the postscaler
    TimerConst = TimerConst / 2          ;   halve the count
      endw
    TimerConst = 65536 - TimerConst + 8  ; final reload value
    ; ---------------------------- DEBUG SCALER --------------------------------
    DebugT1PS = T1PS
    DebugTC = TimerConst
    ; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    ...
    Before Main of my program I added:
    Code:
    DebugT1PS CON EXT
    DebugTC   CON EXT
    Right now I'm wiring up a LCD to display the 2 variables.

    Am I on the right track to look at them?

    I've never tried to display something from inside Darryl's includes before. My plan was to copy them into 2 words and then displaying the lower and upper bytes on the LCD.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,719


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Am I on the right track to look at them?
    probably not if yours fails try this

    Code:
    DISABLE DEBUG
    
    ; syntax =     Handler  IntSource,        Label, Type, ResetFlag?
    DEFINE  Elapsed_Handler  TMR1_INT,  _ClockCount,  asm,  yes
    ; the above define can be used in the INT_LIST macro, if desired (optional)
    
    Ticks            VAR BYTE   ; Counts timer Overflows
    T1Post           VAR BYTE   ; Timer1 postscaler
    Seconds          VAR BYTE
    Minutes          VAR BYTE
    Hours            VAR BYTE
    Days             VAR WORD
    DebugT1PS     var byte
    DebugTCL       var byte 
    DebugTCh       var byte 
    
    SecondsChanged   VAR BIT    ; idicates that the value has changed
    MinutesChanged   VAR BIT
    HoursChanged     VAR BIT
    DaysChanged      VAR BIT
    
    GOSUB ResetTime             ; initialize the Elapsed Timer
    
    Goto OverElapsed            ; skip over the routines
    
    ' -------------- calc timer reload Constants -------------------------------
    ASM
    T1PS = 1                             ; start with 1:1 postscaler
    TimerConst = ((OSC*1000000)/4/100)   ; how many timer ticks will it take
      while TimerConst > 65400           ; if it's more than the timer can count
    T1PS = T1PS * 2                      ;   double the postscaler
    TimerConst = TimerConst / 2          ;   halve the count
      endw
    TimerConst = 65536 - TimerConst + 8  ; final reload value
     _DebugT1PS =T1PS
     _DebugTCH=TimerConst /256 
     _DebugTCL = TimerConst - _DebugTCH*256
    
    ; -----------------  ADD TimerConst to TMR1H:TMR1L -------------------------
    ADD2_TIMER   macro

  10. #10
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Actually, it worked.

    I deleted my debug include 'cause I can work without it.

    Code:
    ...
    ;T1PS        CON EXT    ' Already defined in include
    TimerConst  CON EXT
    
    varT1PS   var word
    varTC     var word
        varT1PS = T1PS
        varTC   = TimerConst
        
        Lcdout $fe, 1, "Timer 1"
        Lcdout $fe, $94, "PS:", BIN8 varT1PS.byte1, " ", BIN8 varT1PS.byte0
        Lcdout $fe, $D4, "TC:", BIN8 varTC.byte1, " ", BIN8 varTC.byte0
    
    MainProgram:
    And I get:
    PS:00000000 00000001
    TC:01100011 11001000 <--- Dec=25544

    EDIT: Woooops, still using OSC 16, BRB.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  11. #11
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    OSC 64 (16 + PLL)

    PS:00000000 00000100 <--- Dec=4
    TC:01100011 11001000 <--- Dec=25544

    I had same results manually this afternoon.

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,719


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    ok next whats in T1CON ?

  13. #13
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Looking at the datasheet for 18F44K22, timer 1 has a 2 bit prescaler.

    4 (100) takes 3 bits.

    Gonna try timer 2.

    ...
    13.0 TIMER2/4/6 MODULE p.175
    ...
    • 8-bit Timer and Period registers (TMRx and PRx,
    respectively)
    • Readable and writable (both registers)
    • Software programmable prescaler (1:1, 1:4, 1:16)
    • Software programmable postscaler (1:1 to 1:16)
    ...

    EDIT: Darn, DT Elapsed include only has Timer1...

    Robert
    Last edited by Demon; - 23rd December 2014 at 04:31.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  14. #14
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working as expected at 64MHz

    Was looking at T1CON register and noticed this:

    TxCKPS<1:0>: Timer1/3/5 Input Clock Prescale Select bits
    11 = 1:8 Prescale value
    10 = 1:4 Prescale value
    01 = 1:2 Prescale value
    00 = 1:1 Prescale value
    So that is 2 bits.


    Code:
    bit 7-6 TMRxCS<1:0>: Timer1/3/5 Clock Source Select bits
      11 =Reserved. Do not use.
      10 =Timer1/3/5 clock source is pin or oscillator:
        If TxSOSCEN = 0:
          External clock from TxCKI pin (on the rising edge)
        If TxSOSCEN = 1:
          Crystal oscillator on SOSCI/SOSCO pins
      01 =Timer1/3/5 clock source is system clock (FOSC)
      00 =Timer1/3/5 clock source is instruction clock (FOSC/4)
    
    bit 5-4 TxCKPS<1:0>: Timer1/3/5 Input Clock Prescale Select bits
      11 = 1:8 Prescale value
      10 = 1:4 Prescale value
      01 = 1:2 Prescale value
      00 = 1:1 Prescale value
    
    bit 3 TxSOSCEN: Secondary Oscillator Enable Control bit
      1 = Dedicated Secondary oscillator circuit enabled
      0 = Dedicated Secondary oscillator circuit disabled
    
    bit 2 TxSYNC: Timer1/3/5 External Clock Input Synchronization Control bit
        TMRxCS<1:0> = 1X
      1 = Do not synchronize external clock input
      0 = Synchronize external clock input with system clock (FOSC)
        TMRxCS<1:0> = 0X
      This bit is ignored. Timer1/3/5 uses the internal clock when TMRxCS<1:0> = 1X.
    
    bit 1 TxRD16: 16-Bit Read/Write Mode Enable bit
      1 = Enables register read/write of Timer1/3/5 in one 16-bit operation
      0 = Enables register read/write of Timer1/3/5 in two 8-bit operation
    
    bit 0 TMRxON: Timer1/3/5 On bit
      1 = Enables Timer1/3/5
      0 = Stops Timer1/3/5
        Clears Timer1/3/5 Gate flip-flop
    T1CON=00000000

    Robert
    Last edited by Demon; - 23rd December 2014 at 04:42.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  15. #15
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working at 64MHz

    hi robbert
    it was not released specifically for the elapsed timer , as that code uses timer 1 ,
    and both versions must be maintained as the K22 version is specific to the registers that have changed for the K22 series

    the ints-18_K22 version was on His site from memory


    The modified version is used specifically on the 18fxxK22 ,for the following reasons

    1. TMR4 - changed from PIR3 to PIR5 in K22 ver
    2. TMR5 and TMR6 support added to support the addtional timers in the K22 series
    3. ccp3 , ccp4,ccp5 - changed PIR3 to PIR4 in the K22 range


    this is confirmed when comparing DT_INTS-18 AND dt_INTS-18_K22 of the code

    the addtional / changed code in K22 version is as follows


    regards

    Sheldon

    Code:
      #define TMR4_INT  PIR5, TMR4IF    ;-- TMR4 to PR4 Match
    #define TMR5_INT  PIR5, TMR5IF    ;-- TMR5 Overflow 
      #define TMR6_INT  PIR5, TMR6IF    ;-- TMR6 to PR6 Match
    ccp3- ccp5 , uses PIR4 in the K22 , where other f18 use PIR3

    Code:
     #define CCP3_INT  PIR4, CCP3IF    ;-- CCP3 
      #define CCP4_INT  PIR4, CCP4IF    ;-- CCP4 
      #define CCP5_INT  PIR4, CCP5IF    ;-- CCP5
    further support for the changed TMR4 and extra timers 5 and 6 in k22 range

    Code:
    ifdef TMR4IF  ;----{ TMR4 Overflow Interrupt }------------[PIR5, TMR4IF]---
          INT_Source  PIR5,TMR4IF, PIE5,TMR4IE, IPR5,TMR4IP
      endif
    
     ifdef TMR5IF  ;----{ TMR5 Overflow Interrupt }------------[PIR5, TMR5IF]---
          INT_Source  PIR5,TMR5IF, PIE5,TMR5IE, IPR5,TMR5IP
      endif
      ifdef TMR6IF  ;----{ TMR6 Overflow Interrupt }------------[PIR5, TMR6IF]---
          INT_Source  PIR5,TMR6IF, PIE5,TMR6IE, IPR5,TMR6IP
      endif
    again further support of the changed interupt register from normal 18f pics ( PIR3) to that of the K22 (PIR4) for the CCP3-5

    Code:
      ifdef CCP3IF  ;----{ CCP3 Interrupt Flag }----------------[PIR4, CCP3IF]---
          INT_Source  PIR4,CCP3IF, PIE4,CCP3IE, IPR4,CCP3IP
      endif
      ifdef CCP4IF  ;----{ CCP4 Interrupt Flag }----------------[PIR4, CCP4IF]---
          INT_Source  PIR4,CCP4IF, PIE4,CCP4IE, IPR4,CCP4IP
      endif
      ifdef CCP5IF  ;----{ CCP5 Interrupt Flag }----------------[PIR4, CCP5IF]---
          INT_Source  PIR4,CCP5IF, PIE4,CCP5IE, IPR4,CCP5IP
      endif

  16. #16
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: Elapsed timer not working at 64MHz

    ...this is confirmed when comparing DT_INTS-18 AND dt_INTS-18_K22 of the code...
    Ok, so the K22 version is for the main DT_INTS-18 include, not the DT_Elapsed-18 include. I hadn't understood that.

    Robert

Similar Threads

  1. I2CRead & I2CWrite not working as expected
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 30
    Last Post: - 27th October 2021, 19:36
  2. PORTB.3 Input not working as expected.
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th March 2013, 10:58
  3. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 18:39
  4. SPWM and Elapsed Timer
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 8th May 2008, 04:16
  5. DT Elapsed Timer
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th March 2008, 00:17

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

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