Jump to content
dpat123

Variable Protection In Interrupt

Recommended Posts

is there any instruction for variable protection in interrupt

 

 

void interrupt()

{

 

if(intcon.TMR0IF)

{

 

TickCounter++;

intcon.TMR0IF=0;

tmr0l=7; //reset timer0

 

}

}

 

 

i am using TickCounter to get ticks in my program . sometimes it gets corrupted as i also reset this any time

Share this post


Link to post
Share on other sites

You can set semaphores indicate a variable is currently being used to prevent the variable being changed when it is being used by some other part of the program.

 

You could set up critical regions which will disable interrupts in the main program so that the interrupt routine cannot change the variable whitst being used.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
is there any instruction for variable protection in interrupt

 

 

void interrupt()

{

 

if(intcon.TMR0IF)

{

 

TickCounter++;

intcon.TMR0IF=0;

tmr0l=7; //reset timer0

 

}

}

 

 

i am using TickCounter to get ticks in my program . sometimes it gets corrupted as i also reset this any time

 

Disabling interrupts around the code that uses the shared variable will prevent corruption:

void interrupt()
{

if(intcon.TMR0IF)
  	{

	TickCounter++;
	intcon.TMR0IF=0;
	tmr0l=7; //reset timer0

  	} 
}

void main()
{
while( 1 )
{
	if( someEvent )
	{
		// prevent corruption by disabling interrupts
		intcon.gie = 0;
		TickCounter = 0;
		intcon.gie = 1;
	}
}
}

 

Regards

Dave

Share this post


Link to post
Share on other sites
You can set semaphores indicate a variable is currently being used to prevent the variable being changed when it is being used by some other part of the program.

Reynard, what do you mean by semaphores?

Edited by soft2

Share this post


Link to post
Share on other sites

A semaphore is a flag that you use when using a shared resourse such as a printer, database record or variables. A semaphore indicates to some other task that the resource is currently in use by the current task and is therefore not available at this time to be used or modified by another.

 

When a task is finished with the resourse it will reset the semephore allowing another task to use it. The other task will set the semaphore to indicate it is using the resouce.

 

The main() loop and interrupt() function are two different tasks which can use the same global variables.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...