View previous topic :: View next topic |
Author |
Message |
ljb
Joined: 19 Nov 2003 Posts: 30
|
timer1 interrupt on overflow |
Posted: Tue Sep 27, 2005 7:57 am |
|
|
My prog uses timer1 to interrupt on overflow, this increments a task counter which is tested by a case block to run the next task.
I am using an 18f2620 and trying to accomplish the following :
At the end of each task (case) I would like to test if a timer1 interrupt has occured (overflow) whilst the task was being processed. This would indicate that a task has taken longer than its alocated time and a flag could be raised.
a simplified version.
#int_timer1
void scheduler(void){
switch(--taskCntr){
case 0 : DO THIS TASK;
break;
case n : DO THIS TASK;
break;
}
TEST IF AN OVERFLOW OCCURED WHILST PERFORMING A TASK.
}
My first thought was to set a irs entered flag on a normal entry, clear the overflow interrupt and if the isr is re-entered, check the flag and clear it if the flag is already set then exit this second isr. Then on completion of the task, check the state of the flag to see if an overflow occured during the task.
Is this the best way? How do I test for an overflow ?
I hope this explanation makes sense and many thanks to anyone who can help.
Les |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Tue Sep 27, 2005 8:07 am |
|
|
The PIC is interrupt mechanism is non reentrant so you cannot interrupt the PIC inside an interrupt handler of the same priority level (the 18F has two priority levels - Normal and High).
You could clear the timer interrupt flag and at the end of the switch statement test the state of the flag again to see if it is still clear. If the flag is not clear then the task ran for too long. However - you may need to use the "no clear" option on the interrupt handler to make sure the compiler does not clear it before you want it to. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
ljb
Joined: 19 Nov 2003 Posts: 30
|
|
Posted: Tue Sep 27, 2005 10:27 am |
|
|
Let me see if I have this straight;
If I clear the interrupt with clear_interrupt(timer1) after entering the isr, then if the isr is still running a task when another overflow on timer1 occures the interrupt flag for timer1 will be set, which I can check for at the end of the task.
Looking in the data sheet, its the TMR1IF bit that is set on overflow, is this the bit that is cleared by the clear_interrupt(timer1) instruction?
How would I check this bit? I've never checked individual bits in registers before.
Name bit 7 bit 0
PIR1 PSPIF(1) ADIF RCIF TXIF SSPIF CCP1IF TMR2IF TMR1IF
^
/ \
Many thanks for your help
Les |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Tue Sep 27, 2005 10:50 am |
|
|
Quote: | Let me see if I have this straight;
If I clear the interrupt with clear_interrupt(timer1) after entering the isr, then if the isr is still running a task when another overflow on timer1 occures the interrupt flag for timer1 will be set, which I can check for at the end of the task. |
Correct
Quote: |
Looking in the data sheet, its the TMR1IF bit that is set on overflow, is this the bit that is cleared by the clear_interrupt(timer1) instruction?
How would I check this bit? I've never checked individual bits in registers before. |
Code: |
Boolean InterruptStatus(long InterruptSource)
///////////////////////////////////////////////////////////////////////////
//
// Subroutine InterruptStatus
//
// Tests the interrupt flag for the given Interrupt Type
// Returns TRUE if the corresponding Interrupt Flag is set (event has occurred), FALSE otherwise
//
///////////////////////////////////////////////////////////////////////////
{
Boolean status = false;
switch (InterruptSource)
{
case INT_TIMER0 :
status = bit_test(INTCON, TMR0IF);
break;
case INT_RDA :
status = bit_test(PIR1, RCIF);
break;
default:
break;
}
return(status);
}
|
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
ljb
Joined: 19 Nov 2003 Posts: 30
|
|
Posted: Tue Sep 27, 2005 11:07 am |
|
|
Thats great Andrew,
I try it tomorrow.
Thanks for your help
Les |
|
|
|