Reentrancy problem

Status
Not open for further replies.
I have one question what is the value of Previous? is there something missing before this statement?

Code:
      #define X      1
#define Y      2
#define Z      4	//now powers of 2
#define W      8	//I.E bit values

unsigned short SWITCH = 0;
unsigned char Previous;


void Interrupt()
unsigned char Edges,Pins;
{
  if(INTCON.INTF)
  {
       SWITCH |= W;
       PORTA.F1 = 1;
       INTCON.INTF = 0;
  }
  if(INTCON.RBIF)
  {
      Pins=PORTB;		//read the inputs
      Pins=~Pins;		//invert so active low		
      Edges=Pins^[B]Previous[/B];	//find bits that have changed
      Edges&=Pins;		//and are now high
      Previous=Pins;		//keep copy for next time
      INTCON.RBIF = 0;
 
Previous is the value that the port was at the last interrupt. It should be initialized to zero at power up. I normally initialize variables in code rather than letting the compiler do it.

Mike.
 
Hi Mike,

I've tried the code since yesterday. the code went crazy upon execution. everything just turned ON and I cant find a way to debug it.
 
By the way, how do you use SPI_READ? in mikroc this is the function declaration:
Code:
unsigned short Spi_Read(unsigned short buffer);

what do I equate the buffer with? is it PORTC.4 of the PIC? since it is the SDI? I tried but it can't read the data im suppose to have.
 
I can't see anything wrong with that code (except the opening brace being in the wrong place in the interrupt code). If you post code that messes up I will have a look.

Afraid I can't help with MicroC as I don't have it.

Mike.
 
Code:
#define X      1
#define Y      2
#define Z      4	//now powers of 2
#define W      8	//I.E bit values

unsigned short SWITCH = 0;
unsigned char Previous;


void Interrupt()
{
  unsigned char Edges,Pins;

  if(INTCON.INTF)
  {
       SWITCH |= W;
       PORTA.F1 = 1;
       INTCON.INTF = 0;
  }
  if(INTCON.RBIF)
  {
      Pins=PORTB;		//read the inputs
      Pins=~Pins;		//invert so active low		
      Edges=Pins^Previous;	//find bits that have changed
      Edges&=Pins;		//and are now high
      Previous=Pins;		//keep copy for next time
      INTCON.RBIF = 0;
      if(Edges.5 == 0)                 
      {
         SWITCH |= X;		//set X bit
      }
      if(Edges.6 == 0)             
      {
         SWITCH |= Y;
      }
      if(Edges.7 == 0)
      {
         SWITCH |= X;
      }
   }
}

void main()
{ 
   init_all();

   do {
	if(SWITCH & X){		//if X bit set then
            //call function here
            //do something here
            SWITCH&=255-X;	///reset X bit
        }
	if(SWITCH & Y){
            //call function here
            //do something here
            SWITCH&=255-Y;
        }
	if(SWITCH & Z){
            //do something here
            SWITCH&=255-Z;
        }
	if(SWITCH & W){
            //do something here
            SWITCH&=255-W;
        }

     } while(1);

this is the code. everytime i turn ON the circuit, everything labeled
Code:
//do something here
is executed.
 
regarding the SPI_READ.. Do you have any idea where the received data is stored in the PIC? so that when I call SPI_READ I will point it to that address to read the received data.
 
What happens if you change it to,
Code:
void main()
{ 
   init_all();
[COLOR="Red"]   INTCON.INTF = 0;
   INTCON.RBIF = 0;
   SWITCH=0;[/COLOR]
   do {
	if(SWITCH & X){		//if X bit set then
            //call function here
            //do something here
            SWITCH&=255-X;	///reset X bit
        }

Mike.
 
After starting up the circuit, nothing happens(which should be the case). But when I try to interrupt it everything is executed again, regardless of which port was interrupted.
 
Last edited:
So, when the code starts up all the interrupt lines are high and then you take say bit 5 low and all the code is executed including the W code?

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…