Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 18th August 2008, 05:00 AM   (permalink)
Default Timer1 interrupts (PIC 16F886)

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
laxmidd50 is offline  
Old 18th August 2008, 05:06 AM   (permalink)
Default

Quote:
Originally Posted by laxmidd50 View Post
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.

Quote:
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.

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

Quote:
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.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 18th August 2008 at 03:55 PM.
futz is online now  
Old 18th August 2008, 05:11 AM   (permalink)
Default

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.
Pommie is offline  
Old 18th August 2008, 05:14 AM   (permalink)
Default

Quote:
Originally Posted by futz View Post
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.
Pommie is offline  
Old 18th August 2008, 05:17 AM   (permalink)
Default

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

Mike
Mike, K8LH is offline  
Old 18th August 2008, 05:18 AM   (permalink)
Default

Hmm,
bcf PIR1,TMR1IF doesn't seem to do the trick. Is this right?
laxmidd50 is offline  
Old 18th August 2008, 05:19 AM   (permalink)
Default

Quote:
Originally Posted by Pommie View Post
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...
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 18th August 2008 at 05:21 AM.
futz is online now  
Old 18th August 2008, 05:21 AM   (permalink)
Default

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...
Mike, K8LH is offline  
Old 18th August 2008, 05:25 AM   (permalink)
Default

Quote:
Originally Posted by Mike, K8LH View Post
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.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================
futz is online now  
Old 18th August 2008, 05:26 AM   (permalink)
Default

Ok thanks, got it working (for now at least).
laxmidd50 is offline  
Old 18th August 2008, 05:28 AM   (permalink)
Default

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 by laxmidd50; 18th August 2008 at 05:29 AM.
laxmidd50 is offline  
Old 18th August 2008, 05:29 AM   (permalink)
Default

Quote:
Originally Posted by futz View Post
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.

Last edited by Mike, K8LH; 18th August 2008 at 05:39 AM.
Mike, K8LH is offline  
Old 18th August 2008, 05:33 AM   (permalink)
Default

Quote:
Originally Posted by laxmidd50 View Post
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 by Mike, K8LH; 18th August 2008 at 05:39 AM.
Mike, K8LH is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
16F886/16F887 Serial Boot Loader Mike, K8LH Micro Controllers 10 29th November 2008 01:16 PM
Resetting Timer1 in a PIC richacm Micro Controllers 11 14th July 2008 12:37 PM
Next kit (maybe) Dragonfly Seven segment & 16F886 blueroomelectronics Micro Controllers 29 12th July 2007 09:38 AM
Timer1 richb Micro Controllers 5 27th November 2006 11:44 PM
Interrupts or not interrupts ? That is the question ! ZERS Micro Controllers 9 1st March 2005 08:26 PM



All times are GMT. The time now is 04:22 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker