• AVR Freaks

Hot!TMR stops when changing periods

Author
fsonnichsen
Super Member
  • Total Posts : 305
  • Reward points : 0
  • Joined: 2009/08/13 11:00:11
  • Location: 0
  • Status: offline
2020/09/19 10:51:49 (permalink)
0

TMR stops when changing periods

I am running code which I reduced to the small stretch below. (complete config attached as zip).
It runs sporadically. Usually it sets the first pulse loop fine, but when changing speeds, the TMR stops and the pin in the interrupt either sits at a 1 or 0 (e.g. I assume the interrupt fails.)  Seems like I have done this before with 18s and 33s.
Any ideas appreciated-
Fritz
==================

#include "mcc_generated_files/system.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
void TMR1_intp(void);
long int i;
int prd;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
SYSTEM_Initialize();

TMR1_SetInterruptHandler(TMR1_intp);

TMR1_Start();
while(1)
{
prd=200;
TMR1_Period16BitSet(prd);
TMR1_Start();
for(i=0; i < 100000; i++){}
 
prd=1;
TMR1_Stop();
TMR1_Period16BitSet(prd);
TMR1_Start();
for(i=0; i < 100000; i++){}

} //end while

} //end main
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// INTERRUPTS and CallBacks
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TMR1_intp(void) { IO_RD4_Toggle(); }; //PUMP A
#1

3 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3769
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: TMR stops when changing periods 2020/09/20 19:56:52 (permalink)
    0
    To save others having the trouble to download the zip file to find this out, the device is a PIC24FJ128GA010.
    A couple of things (looking at the code snipped you have shown here):
    - in the ISR you need to clear the T1IF bit or the ISR will keep triggering (unless this is something the MCC generated code does - I've not bothered with MCC for these simple peripherals)
    - your 'for' loops may not be doing what you think - any half decent compiler will note that the loop is empty and that  the 'i' variable is not volatile and so it is free to collapse the whole loop to an effective 'nop'
    How are you checking that the pin stays at '0' or '1'? Have you set a breakpoint in the ISR to check that it is being called?
    Susan
    #2
    fsonnichsen
    Super Member
    • Total Posts : 305
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: TMR stops when changing periods 2020/09/21 04:34:53 (permalink)
    0
    Thank you Susan-
     
    The ISR is cleared by the TMR1.c code generated by MCC. I paraphrase it here:
      void __attribute__ ( ( interrupt, no_auto_psv ) ) _T1Interrupt ( )  {
      if(TMR1_InterruptHandler)
      { TMR1_InterruptHandler(); }
      tmr1_obj.count++;
      tmr1_obj.timerElapsed = true;
      IFS0bits.T1IF = false;}
     
    Regarding the loops--apparently the PIC compiler is not that decent as you say- The loop is executed. To be fair I have optimization set to zero.
        It is immaterial however-I simply used the loops to streamline the test code I present here-in my full code for this I use __delay_ms statements. I removed them from the test code to insure there is no type of interference although I believe delay does something very similar to the loop. (Keep in mind that the test code fails identically to my full code-I just use Occam's razor to narrow down the problem)
     
    Not sure what you mean by setting a breakpoint in the ISR or why I would do this? In my experience the Mchip debugger is a bit "wonky" at best and especially so when putting traps in ISR code so I avoid this especially with time sensitive problems.
       Looking at the oscilloscope the pins clearly toggle, at least some of the time. The code for the toggle command is:
    #define IO_RD4_Toggle()           (_LATD1 ^= 1)
    I believe the LATD1 insures the change is transferred from the software to the hardware register so I think it is good (microchip generates this--not me). And since I see the "toggle" working before I change the value I expect it is working.
     
    Something strange I noticed on the scope the other night-If I wait long enough eventually the pin starts to toggle.
     
    Only other thing on my mind is that if you look in the MCC of the zip you will see I have all four TMRs running (1, 2, 3 and 4). Nothing in the datasheet implies that this cannot be done. I assume I am not getting a missed interrupt since they are queued--e.g. if a second interrupt occurs after the first but before the first does the toggle-the second one waits and still is serviced. Even if it takes precedence the first interrupt would eventually be serviced.
     
    Thanks for the attention to this.
    Fritz
     
     
     
    #3
    fsonnichsen
    Super Member
    • Total Posts : 305
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: TMR stops when changing periods 2020/09/21 06:33:01 (permalink)
    0
    OK--got down to the lab and an update:
    1) I removed the other three timers and UART interrupts to insure there isn't some type of interrupt conflict with them.
    2) I upped the system speed from 1mhz to 8mhz.
    I still get the same results--it seems that on the scope, I see good results as the period increases, but I get a "flatline" when I decrease it. Interestingly the pin toggles up and down at times but  I never get back to the behaviour expressed in the code.
     
    I have attached the further pared down code without the additional timers
    Fritz
    #4
    Jump to:
    © 2020 APG vNext Commercial Version 4.5