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

Interrupts changing variables as they are being used

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







Interrupts changing variables as they are being used
PostPosted: Sat Nov 10, 2007 6:42 pm     Reply with quote

Hi,

I am doing a comparison using an if statement on a 32-bit variable. There is a hardware interrupt that will change this variable. Is it possible for the interrupt to happen in the middle of the comparison? Could this be a problem?

Code:


int32 counter = 0;

#int_ext
void ext_isr(){
counter++;
}

void main(){

while(TRUE){

if(counter > 50000)
    run_task();

}
}



Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 10, 2007 8:59 pm     Reply with quote

Here's a thread with several explanations of it.
http://www.ccsinfo.com/forum/viewtopic.php?t=17352

This post has an example. Look in the while() loop in main().
It copies the interrupt variable to a local variable, and it disables
interrupts while doing that. Then the local variable is used by
the rest of the code in main().
http://www.ccsinfo.com/forum/viewtopic.php?t=31470&start=6
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Nov 10, 2007 9:01 pm     Reply with quote

Yes, that is a common problem with multi-byte comparisons and interrupts. The solution is to disable relevant interrupts while the comparison is made.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Sun Nov 11, 2007 8:32 am     Reply with quote

Better yet, disable ints, copy the variable to a temp, enable ints, then do the compare (in the interest of keeping ints off as short a time as possible)

Ken
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