Increment & Decrement within interrupts

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
jj_lighting
Posts: 2
Joined: Tue Oct 28, 2008 4:32 pm
Contact:

Increment & Decrement within interrupts

Post by jj_lighting »

Hoping some one can help. The following program is a simple program which increments a variable when a external interrupt(RB0) occurs. If a "rb change" interrupt occurs then the same variable is decremented. The main program is
checking if the variable is greater than 0. If so then PortA (0) is high. It seems the program isn't decrementing. The probelm seems to be the RB change interrupt. The increment doesn't register. The interrupt will do other things but it won't increment or decrement.

Any help would be great.

Code: Select all


#include <system.h>

void setup_hardware (void)
	{

		trisb = (0xff); // PortB are inputs
		trisa = (0x00); // PortA are outputs
		porta = 0x00;	
		portb = 0x00;
		option_reg =0xc0; // setup external interrupt on rising edge
		intcon =0x98;	// turn on external interrupt on RB0 & change on RB Interrupts
	}


void delay (unsigned int value)  // Delay
	{
	  unsigned int i; // create char for delay
	  for (i = 0; i< value; i++);
	}

char count1=0;
char count2=0;


void coil1 (void)		
	{
		
		count1++;
		
	
	}

void coil2 (void)		
	{
		
		count1--;
		
	}
	
void interrupt (void)  // Interrupt handler
	{
			
		if (intcon & 0x02)	// check INTF interrupt flag
		  {
			  intcon= intcon & 0xfd;	// clear flag
			  coil1();		// Call display handler		  		
		  }
		    
		if (intcon & 0x01)			//check RBIF interrupt flag
		  {
			  intcon= intcon & 0xfe; // clear RBIF bit
			  
					coil2(); 				// call coil 2 handler 
		  }
	}		  
		
void main (void)
		{
		setup_hardware();
		
		
			while(1)
				{
				
					if (count1 > 0)
						{	
							porta = 0x01;
						}
					else
						{
							porta= 0x00;
						}
						
				}		
		}
		
Last edited by jj_lighting on Thu May 13, 2010 1:02 pm, edited 2 times in total.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Increment & Decrement within interrupts

Post by Sean »

The program looks ok. There may be some practical problems with testing.

The listed program will turn RA0 on if count1 is > 0 (not > 1 as stated). So it will only be off when count1 = 0.

Interrupt responses are extremely fast so, if you are using switches or other electrically noisy signal source, the program might be counting additional pulses from switch bounce. This would corrupt the expected position of the zero count.

The INT input will respond to one edge of any input pulse, whereas the RB change interrupt will respond to both edges of an input pulse, possibly giving an asymetric count problem.

If unused RB<4:7> pins are left floating, they might be contributing additional interrupt signals due to noise.

jj_lighting
Posts: 2
Joined: Tue Oct 28, 2008 4:32 pm
Contact:

Re: Increment & Decrement within interrupts

Post by jj_lighting »

Thanks,

Was meant to say greater than 0. Anyway interrupt works fine if i just want to put porta high. such as this.

Code: Select all

if (intcon & 0x01)	// check interrupt flag
		  {
			  intcon= intcon & 0xfe;	// clear flag
			 
                                      porta =0x01;
 
			} 
		  		
This proves that the interrupt is activated, but it won't increment or decrement the variable. Yet the RB0 interrupt is fine when incrementing.


Post Reply