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

ISR global variable change

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







ISR global variable change
PostPosted: Wed Feb 02, 2005 1:42 pm     Reply with quote

I need to modify a variable down in an ISR routine. Do I need to use a pointer to allow the routine to access the variable? Thanks.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Wed Feb 02, 2005 2:10 pm     Reply with quote

Short answer is no.
Ex

#include...
#fuses...

int8 myownglobal;
#int_xxx
myownisr()
{
myownglobal=99;
}
main()
{
while(1);
}
valemike
Guest







one caveat of global variables and ISRs...
PostPosted: Wed Feb 02, 2005 4:16 pm     Reply with quote

Suppose we had an "unsigned long", i.e. anything bigger than a byte.

I would find it safer to disable interrupts when reading that variable, or especially changing it, while not in an ISR. Reason being, an interrupt might occur right in the middle of that multi-byte manipulation operation.

Maybe i'm being paranoid, but this practice arose back in my days of RTOSes, semaphores, globals, tasks accessing the same variable, etc.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Feb 03, 2005 9:34 am     Reply with quote

valemike is right with the caution about reading or writing variables that are more than int8 in length that are assigned within an isr. A possible method to mitigate this is to have the globals that are updated by isr code read inside a function
Ex template

int16 myisrglobalreader()
{
int16 temp;
disable_interrupts(the interrupt that writes to global);
temp=myglobalvar;
enable_interrupts(the interrupt that writes to global);
return temp;
}
main()
{
int16 value;
value=myisrglobalreader();
while(1);
}
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