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 CCS Technical Support

interrupts

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







interrupts
PostPosted: Tue Jul 19, 2005 11:44 am     Reply with quote

does anybody knows if you can change a general variable in a interrupts so you can use it in the main program?and how you can make that?


Thanks a lot...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 19, 2005 12:55 pm     Reply with quote

Declare a global variable. Set it in the isr, and read it in your main
program. See the sample program in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=17041
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

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

PostPosted: Tue Jul 19, 2005 1:08 pm     Reply with quote

If the variable is declared outside/before main() then the scope will be global and it can be used in a ISR.

Code:

#include <18f452.h>
#case
#use delay(clock=16000000)
#fuses hs,nowdt,noprotect,nolvp
#use rs232(baud=19200,xmit=PIN_B4,invert,stream=DEBUG)
#zero_ram
BOOLEAN idle_bus;
//---------------- MAIN --------------------
void main(void)
{
  idle_bus=FALSE;
  setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
  set_timer1(INT_TIMER1);
  enable_interrupts(INT_TIMER1);
  enable_interrupts(GLOBAL);
  fprintf(DEBUG,"Starting\n\r");
  while(1)
  {
    fprintf(DEBUG,".");
    if(idle_bus)
    {
      fprintf(DEBUG,"Bus is idle\n\r");
      idle_bus=FALSE;
    }
  }
}


//=======================timer 1 ISR============================//
#INT_TIMER1       // This is called when timer 1 overflows
void timer1_isr()
{
  idle_bus=TRUE;
}
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Tue Jul 19, 2005 1:29 pm     Reply with quote

Keep in mind that when you do use a global variable, especially a 16-bit variable, that it takes more than one assembly instruction to access it. In other words, access to that variable is not "atomic".

So when I do access 16-bit variables outside an isr, I disable interrupts briefly.

e.g.

Code:

disable_interrupts(GLOBAL);
if (GLOBAL_my_16bit_var < 2000)
{
...
}
enable_interrupts(GLOBAL);


The reason why I disable interrupts is because the interrupt can hit anywhere in the assembly code while in the 'middle' of the if() statement above, causing unexpected results.
nash7
Guest







PostPosted: Wed Jul 20, 2005 3:55 am     Reply with quote

thanks a lot, I had a mistake and I thought that maybe it was because of the interrupt's variable, and it wasn't as you told me...

thanks again Very Happy
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