No announcement yet.

Interference of Timer5 on the 35T

  • Filter
  • Time
  • Show
Clear All
new posts

  • Interference of Timer5 on the 35T

    I have discovered that there is something interfering with the IFS0bits.T5IF event flag
    Here is the set up:
    A clean signal generator, square wave, that I'm watching with an O-scope, and let's say for this instance it's 6Khz

    I have this code:

    // timer is on
    while (TMR5 == 0); //wait here until the signal goes high
    while (IFS0bits.T5IF ==0); //wait here until the falling edge of the signal, which sets the T5IF high and TMR5 counter has already stopped
    tft.print(counter_base/TMR5); //in this case my counter_base is 20,000,000 and TMR5 holds the count; I chose a 2:1 pre scale on PBCLK in this case
    // then turn everything off

    Most of the samples shows a value close to 6000; I don't need that much accuracy

    then, on some occasions I'll see 31,554 or 500,000 or 21,332 or some equally strange number and then it will go back to something close to 6000 again

    I'll keep at this, unless engineering knows that I can never get there from here :-)

  • #2
    If you want to post your entire code I'll try and replicate the setup here.


    • #3
      Matt, thanks, this is designed as debugging code, attached, no other unique header files or libraries, just your code
      Attached Files


      • #4
        Ahh, reading further in the Microchip Section 14:Timers, to reset the timer you have to be careful and write to both high and low bytes of TMR5


        • #5
          You might be better implementing an interrupt routine to trigger on T5IF instead of polling it - just capture the value in the ISR and use it as a flag in the main loop. Keep the priority high (6 or 7 is best) and then it won't be interfered with by the core timer interrupt. You can then enable the interrupt and wait for the captured time variable to be set.


          • #6
            I have added gated timer mode to my Timer library, and written an example FrequencyTimer that makes use of it using the interrupt. Seems to measure a 491Hz PWM output perfectly stably and runs completely in the background so you don't need to do anything in your loop to "run" it.



            • #7
              Oh, and I've just added another example that uses a pair of timers, one in gated mode, to calculate not only the frequency, but the duty cycle as well. And all interrupt based.


              • #8
                Matt, most grateful. I did rewrite my code and you were right, I got stability with an interrupt. I wanted to confirm that your Timer examples don't actually run "out of the box", as they don't for me on the 35T and I'm looking at the logic and, as far as I can tell as I transition from C to C++, there is some glue needed. I will figure it out.


                • #9
                  I'm running on a much newer version of the compiler than anyone else round here - the interrupt system has changed slightly in that. When I release the next beta of UECIDE the new compiler will be the default, so interrupts should be slightly easier to work with.