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

Disabled timer1 interrupt function affects I2C

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



Joined: 28 Jun 2007
Posts: 9

View user's profile Send private message Visit poster's website

Disabled timer1 interrupt function affects I2C
PostPosted: Thu Jun 28, 2007 10:39 am     Reply with quote

Hi All,

I have some working code on a PIC18F4525 that receives Intel hex records via the I2C and reprograms the PIC flash memory. After adding a Timer 1 interrupt function (unrelated to the I2C), I noticed the I2C started occasionally missing characters. I tried disabling the timer interrupt but characters were still missed. I finally tracked it down to local variables in the (disabled!) timer interrupt routine; if I make them global the I2C runs Ok!

Here's the code (you could chop out all but the variable declarations and still see the problem):
Code:

/******************************************************************************/
/* Timer 1 Handler - used to debounce synth lock                              */
/******************************************************************************/
// INT8 lockCount = LOCK_DEBOUNCE_COUNT;  // Works if these declarations are used
// INT8 lockShadow;

#INT_TIMER1
void timer1_handler() {

static INT8 lockCount = LOCK_DEBOUNCE_COUNT; // Problems when using local varables!
INT8 lockShadow;

  set_timer1(T1_POLL_TIME);

   lockShadow = LOCK;

   if( lockDebounced == lockShadow )
      lockCount = LOCK_DEBOUNCE_COUNT;
   else
   {
      if( lockCount )
            lockCount--;
      else
            lockDebounced = lockShadow;
   }   
}


I checked the timer interrupt enable flag is clear in the I2C routine and a breakpoint put in the timer handler never triggers.

Comparing .lst output files the only difference is when variables are local to the interrupt there are MOVLB 1 and MOVLB 0 instructions, but as the timer interrupt is never called, why would this affect the I2C interrupt?

Could this be a PIC gotcha, something to do with the way global/local variables are handled in the PIC?

Any help greatly appreciated!

Regards,
John Mackenzie

PS Complier version is 3.245. Using MPLAB ICD 2 to debug.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jun 28, 2007 10:52 am     Reply with quote

Sounds like a memory corruption problem. Most likely somewhere in your code you are writing out of the array bounds and overwriting another variable. Often these kind of errors pop up directly when testing code the first time but sometimes they are masked because you are overwriting a variable that is not used at that time. Changing the code has shuffled the variables around and now the old bug becomes visible.

My first suspect would be the I2C ISR routine as the local variables of all interrupt routines are sharing the same memory region.
jmac_link



Joined: 28 Jun 2007
Posts: 9

View user's profile Send private message Visit poster's website

PostPosted: Fri Jun 29, 2007 10:34 am     Reply with quote

Thanks for your suggestion ckielstra.

I checked all variable use, pointer sizes, memory allocation etc. All looks Ok and its still not working!

What I have found looking at the .sym file is the timer1_handler() variable lockShadow shares a memory location with a variable in the i2c interrupt:

    1AE write2ProgramMemory.@SCRATCH
    1AF i2c_interrupt.dummy <= shared location
    1AF timer1_handler.lockShadow <=shared location
    1B0 i2c_interrupt.inData
    1B1 complete_i2c_incoming.dummy

This looks like it could be causing the problem but I cant see why - the i2c interrupt is only called once at the start of the program ( it receives a command byte to enter reprogram mode ) after which the i2c interrupt is disabled and the SSPIF flag is polled to read in bytes from the i2c. Also timer1 is disabled so timer1 and i2c the interrupts should never occur at the same time.

Will ponder this over the weekend...
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