Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interrupt on Change problem

Status
Not open for further replies.

Powzoom

New Member
I have a pic16F688 and I am trying to call an interrupt when the RA5 pin changes. I've read the datasheet, searched the web, and looked at this forum and all the info I got seemed to be saying that I'm doing it right... But it doesn't work. I have the pickit2 and I am using hi-tech pro lite. Any help would be great.

Code:
#define LED RC5
int on = 0;

void main ()
{
	OSCCON = 0b01111100;
	DelayMs(200);
        // ADC off, all digital I/O
        ANSEL = 0;
        CMCON0=0x07;
         
        // PORT direction register
        TRISA = 0b00111000;
        TRISC = 0b00000000;

        //Interupt on change
	GIE = 1;
	ei(); //Global Interrupts
	RAIF  = 0;
	IOCA5 = 1;
        int x = PORTA; //read port

	while (run == 1)
	{
		if (on==0)
		{
			LED=1;
		}
		else 
		{
			LED=0;
		}
	}
}

void interrupt isr(void)
{
		on=1;
		int x = PORTA; //read the port
		RAIF = 0;  // clear flag	
	
}
 
I have a pic16F688 and I am trying to call an interrupt when the RA5 pin changes. I've read the datasheet, searched the web, and looked at this forum and all the info I got seemed to be saying that I'm doing it right... But it doesn't work. I have the pickit2 and I am using hi-tech pro lite. Any help would be great.

Code:
#define LED RC5
int on = 0;

void main ()
{
    OSCCON = 0b01111100;
    DelayMs(200);
        // ADC off, all digital I/O
        ANSEL = 0;
        CMCON0=0x07;
         
        // PORT direction register
        TRISA = 0b00111000;
        TRISC = 0b00000000;

        //Interupt on change
    GIE = 1;
    ei(); //Global Interrupts
    RAIF  = 0;
    IOCA5 = 1;
        int x = PORTA; //read port

    while (run == 1)
    {
        if (on==0)
        {
            LED=1;
        }
        else 
        {
            LED=0;
        }
    }
}

void interrupt isr(void)
{
        on=1;
        int x = PORTA; //read the port
        RAIF = 0;  // clear flag    
    
}

I'm not a HiTech user, but one thing I see is that you have defined a local variable in your isr.

Rather than
Code:
void interrupt isr(void)
{
        on=1;
        int x = PORTA; //read the port
        RAIF = 0;  // clear flag    
    
}
try
Code:
void interrupt isr(void)
{
        on=1;
        x = PORTA; //read the port
        RAIF = 0;  // clear flag    
    
}
That way the ISR will use your global var to store PORTA, rather than the local x which you had defined in the ISR.

Ah, I don't think that is the problem, though; you aren't doing anything with x.
 
Last edited:
yea, the variable is just to read the port. It says in the datasheet you need to read the port to reset it or something so it knows the last state of RA5. It's weird but I saw it in some one else's code and thought it couldn't hurt.
 
I had a quick look at your code and notice you don't set the PortA interrupt enable bit RAIE. This bit must be set for PortA interrupts to work.
Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top