CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Unreliable Interrupt

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jack0218



Joined: 10 May 2008
Posts: 1

View user's profile Send private message

Unreliable Interrupt
PostPosted: Sat May 10, 2008 10:55 pm     Reply with quote

I have a device that, among other things, uses the Timer0 interrupt to operate a one second duration LED "metronome" at a period of 5s. The "metronome" itself works as it is supposed to, but sometimes has an initial delay of around 1.5 minutes before it begins its cycle. This only happens occasionally, happens more often for some chip writings than others, and can sometimes be resolved by turning the power off and on once or a few times. I think that this may be due to a problem with initialization or some related topic, but the consistency of the delay is concerning. I would just like this function to be reliable; thanks in advance for your help.

Code:
#include "18F2321.h"
#fuses INTRC_IO,NOFCMEN,NOIESO,NOPUT,NOBROWNOUT,NOWDT,CCP2C1,NOPBADEN,LPT1OSC,NOMCLR,NOSTVREN,NOLVP,NOPROTECT,NOCPB
#use delay(clock=8000000)

#INT_TIMER0
int metronome(int two_Hz_timing_interrupt)
{
two_Hz_timing_interrupt=two_Hz_timing_interrupt+1;

if (two_Hz_timing_interrupt==1)
   {
     output_high(PIN_A5);
   }
else if (two_Hz_timing_interrupt==3)
   {
     output_low(PIN_A5);
   }
else if (two_Hz_timing_interrupt==10)
   {
    two_Hz_timing_interrupt=0;
   }

return two_Hz_timing_interrupt;
}

void main(void)
{
int two_Hz_timing_interrupt=0;

SETUP_TIMER_0(RTCC_DIV_16|RTCC_INTERNAL);
ENABLE_INTERRUPTS(INT_TIMER0);
ENABLE_INTERRUPTS(GLOBAL);
SET_TIMER0(0);

while (TRUE)
{***other unrelevant stuff***}

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 11, 2008 12:43 am     Reply with quote

There are several problems:

1. An interrupt service routine (isr) can't have a variable passed to it as
a function parameter. It has to be a global variable instead.
A global variable will be visible to both the isr and the code in main().

2. An isr can't return a value.

The code below shows the required changes in bold:
Quote:

int two_Hz_timing_interrupt = 0; // Add this line, in this location.


#INT_TIMER0
void metronome(void) // No parameters or return values allowed
{
two_Hz_timing_interrupt=two_Hz_timing_interrupt+1;

if (two_Hz_timing_interrupt==1)
{
output_high(PIN_A5);
}
else if (two_Hz_timing_interrupt==3)
{
output_low(PIN_A5);
}
else if (two_Hz_timing_interrupt==10)
{
two_Hz_timing_interrupt=0;
}

return two_Hz_timing_interrupt; // *** Delete this line
}


void main(void)
{
int two_Hz_timing_interrupt=0; // Move this line above the isr

SETUP_TIMER_0(RTCC_DIV_16|RTCC_INTERNAL);
SET_TIMER0(0); // Move this line up to here.
// Do all initialization of hardware modules before you enable ints.

ENABLE_INTERRUPTS(INT_TIMER0);
ENABLE_INTERRUPTS(GLOBAL);

while (TRUE)
{***other unrelevant stuff***}

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group