Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Timer1 interrupts (PIC 16F886)

Status
Not open for further replies.

laxmidd50

New Member
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?
Yes, until you clear the flag the interrupt is disabled, so it won't interrupt again until you do.
EDIT: Incorrect. See rest of thread.

Do I also have to reset the timer, or does it rollover itself?
No. It takes care of itself. If you preloaded your timer for some reason then you'd have to re-preload it in the ISR, but otherwise it's automatic.

Also, do i need to disable the timer to clear the interrupt flag?
No.

And can i get the timer running again while I'm still in the interrupt code (don't want to lose any time)?
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.
 
Last edited:
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.

Mike.
 
Yes, until you clear the flag the interrupt is disabled, so it won't interrupt again until you do.

I thought it was the opposite of this. If you don't clear the flag you will get another interrupt immediately after the retfie is executed.

Mike.
 
You're correct Mike (Pommie). Clear the flag to prevent another immediate interrupt after the return from interrupt.

Mike
 
I thought it was the opposite of this. If you don't clear the flag you will get another interrupt immediately after the retfie is executed.
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...
 
Last edited:
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. :D So if the flag isn't cleared the thing will just interrupt over and over and be stuck.
 
As a side note, what is the best way to test if something is zero? I'm doing
clrw
iorwf Second,0
btfsc STATUS,Z
Is there an easier way than that?
 
Last edited:
That's great! I love it when I learn something new to correct a bad assumption. :D 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.
 
Last edited:
As a side note, what is the best way to test if something is zero? I'm doing
clrw
iorwf Second,0
btfsc STATUS,Z
Is there an easier way than that?

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.

Mike
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top