1
\$\begingroup\$

I am trying to decipher a C code in which Timer1 is being used for measuring the period of a digital signal, using dsPIC33EP256MC506. The timer interrupt is not being enabled, and there is no interrupt service routine for this timer. I'll post only the relevant portions of the code, since the actual code is Huge! The program is just initializing the timer as follows in the main() routine:

    volatile unsigned int ContinueTimeStep __attribute__ ((near)) = 0;
    int main() {
          PR1 = 0xFFFF;        /* Period */
          T1CON = 0x8030;      /* enable timer, 1:256 input clock prescaler */
     /* Main Loop */
      for (;;) {
        while (!ContinueTimeStep) ;
        asm("BCLR.b _ContinueTimeStep,#0");/* ContinueTimeStep--; */
        MyFunction();
      }                                    /* End for(;;) */
    }

In MyFunction() nothing is being done with the timer. The timer reading is only utilized in period calculation on rising edge. This done inside the change-notification ISR as follows:

void __attribute__((__interrupt__,__auto_psv__)) _CNInterrupt(void)
{
      /* Change Notification on Port F0*/
      if (portF_Xor & 1) {
        if (portF_Copy & 1)                /* rising edge detection*/
        {
          CN80_RisingPeriode = Timer1 - TimerCN80_Old;
          TimerCN80_Old = Timer1;
          CN80_ChangeDetectedFlag++;
        }

        TimerCN80_Old = Timer1;
        }
}

As I mentioned, I looked around in the code, but could not find a Timer 1 ISR. So I am pretty sure there isn't one. My question is, is this even valid? Will the timer automatically overflow and start from zero, once the period is reached? What happens if the interrupt occurs right after the timer 1 has overflown? The period will be calculated as negative? Thank you for your valuable input!

\$\endgroup\$
  • \$\begingroup\$ Made some changes to the code ISR, to show the if statement, showing rising edge detection. \$\endgroup\$ – Adeel Apr 15 '15 at 20:54
1
\$\begingroup\$

There's nothing wrong with enabling a timer but not its interrupt.
It will just overflow and carry on counting from 0.

Yes you are correct that if a CN interrupt occurs after an overflow then the calculation will produce a negative result.
Ideally this should be caught and corrected in the code.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I did not include the if statement before, showing the rising edge detection. I edited the post. Does the updated ISR make any difference? \$\endgroup\$ – Adeel Apr 15 '15 at 20:54
  • \$\begingroup\$ No I don't think that will make any significant difference. Your "CN80_RisingPeriode" can still end up containing a negative number if Timer1 overflows in between CN interrupts. \$\endgroup\$ – brhans Apr 16 '15 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.