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!
Best Answer
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.