I am using timer1 of a pic 16f886 to make a clock. However, the interrupt code only runs once. I think I have to clear the interrupt flag to get it to run again? Do I also have to reset the timer, or does it rollover itself? Also, do i need to disable the timer to clear the interrupt flag? And can i get the timer running again while im still in the interrupt code (don't want to lose any time)?
Thanks,
Scott
I am using timer1 of a pic 16f886 to make a clock. However, the interrupt code only runs once. I think I have to clear the interrupt flag to get it to run again?
The timer runs constantly once started. Just don't disable it and you'll be fine. Just because the interrupt flag isn't clear doesn't mean that the timer has stopped. It hasn't, unless you wrote code to stop it.
You have to clear the interrupt flag (PIR1,TMR1IF - NOT PIE1,TMR1IE) in the interrupt. The timer will just keep running and interrupt whenever it rolls over. If you want it to interrupt after a certain count then have a read about the CCP module, especially the special event trigger.
This is how I understand it - and practical experience seems to agree: The flag gets set when the interrupt is triggered. While the flag is set, that interrupt is disabled. So if you don't clear the flag, you don't get any more interrupts on that one.
EDIT: Two against one! Sounds like I'm wrong. Oh well. I always cleared the flag anyway, so it never mattered...
No, that's not correct. Remember the little PWM-32 project bug where I forgot the "pir1.TMR2IF = 0;" instruction? Your code in Main wasn't running because the program was spending all its time in the ISR...
No, that's not correct. Remember the little PWM-32 project bug where I forgot the "pir1.TMR2IF = 0;" instruction? Your code in Main wasn't running because the program was spending all its time in the ISR...
That's great! I love it when I learn something new to correct a bad assumption. So if the flag isn't cleared the thing will just interrupt over and over and be stuck.
That's great! I love it when I learn something new to correct a bad assumption. So if the flag isn't cleared the thing will just interrupt over and over and be stuck.
Yes. As soon as the 'retfie' instruction is executed interrupts are re-enabled and the processor sees what it thinks is a pending interrupt flag which needs to be processed.
Please use labels 'W' and 'F' instead of '0' and '1'.
Your code is fine. You could also use movf Second,F and btfsc STATUS,Z instructions if you need to preserve the W register.
If the value you want to test is in W then you can use andlw b'11111111' and then test the Z flag. This becomes necessary when you need to test for a zero value from a table lookup because the retlw instruction does not modify the Z flag.