Problems with interrupt on 12F629

Hi

I’m new to this forum and looking for help with an interrupt on a PIC 12F629.

I have a digital signal on GP2, that I want to cause an interrupt. Signal goes from 0 to 5V and back to 0V all the time. I’m trying to make the PIC cause an interrupt on every 5V, so only on the rising edge, but I can’t get anything to happen.

I have a test code below. It’s a simple code, that should turn the LED on and off, but as I said, nothing happends. I have tried the program in ISIS and there is also doesn’t work. Can anyone tell me what I’m doing wrong?

#include <htc.h> 
 
#define _XTAL_FREQ 4000000    //oscillator frequency for 12f629
 
__CONFIG(FOSC_INTRCIO & WDTE_OFF & CPD_OFF & CP_OFF & MCLRE_OFF);

void main() 
{ 
//Initialisation 
TRISIO = 0b00001100;                // GP2 and GP3 as input. GP3 for a future button.
GPIO = 0b00000000;                  // All pins off
CMCON = 0b00000111;
OPTION_REG = 0b01000000;      // Interrupt on rising edge of GP2
INTCON = 0b10010000;             //Global interrupt enabled, GP2 external Interrupt Enabled
GPIE = 0;
INTF = 0;

    while(1) {

    }    
}

void interrupt isr(void) {

	 if(INTF)  
	{
        GP0 = ~GP0;           //Pin to turn on and off
        __delay_ms(50);       //to make the change more visible.
        INTF = 0;                                            
	}
}

Thanks for your time

Bad idea to have a delay in an int. If you want to slow things down put in a counter in there, and change GP0 every few counts, something like:

void interrupt isr(void)
{
  static int count;
  if(INTF)  
  {
    INTF = 0;                                            
    count++;
    if(count > 42)
    {
      count = 0;
      GP0 = ~GP0;
    }
  }
}

Should your reference to GPIE actually be GPIF ?

Haven’t had time to go through the rest of your code, but hopefully a start.

What frequency are you toggling GP2 at ?

What is ISIS (apart from a CP/M like OS :slight_smile:

regards,

mark

Hi

ISIS/Proteus is for simulating electronics :slight_smile:

I’m only using the delay there, to make it more visible if anything happends, but it’s like it never gets into any interrupts at all

Inputs shared with a peripheral function like an ADC need to have it turned off. I haven’t checked if that is the case in your code.

Get rid of the interrupt and do things manually. When that works, switch it over to an interrupt driven sub.

I agree with skimask - simplify initially, then add the int

Also, if you have a delay within an int, and a second in occurs while the int is delaying, you may have an interrupt pile-up. This is a really quick way to overflow the stack and crash everything to short order.

What freq. is the interrupt running at ?

Inputs shared with a peripheral function like an ADC need to have it turned off

There is no ADC on this PIC.

Get rid of the interrupt and do things manually. When that works, switch it over to an interrupt driven sub.

Not sure what I should try. It’s working fine outside the interrupt. Using a timer as interrupt also works fine.

What freq. is the interrupt running at ?

Umm not sure :slight_smile: PIC is running 4GHz

Rendar:
There is no ADC on this PIC.

But there is a comparator module which by default is turned on. Also, Timer-0 uses GP2 as an input which is on by default. Your code "CMCON = 0b00000111;" turns off the comparator and "OPTION_REG = 0b01000000;" turns off the Timer input.

Rendar:
Umm not sure :slight_smile: PIC is running 4GHz

That's a fast PIC! You mean 4MHz which gives you have a 1MHz instruction clock. The PIC (or any microcontroller) has limitations on the switching speed of an incoming signal. You really need to verify that what you are sending to GP2 doesn't violate any timing specs.

Your code seems to initialize the interrupt just fine. Are you positive that you formatted your ISR correctly so that the compiler treats it like one?

The only other thing I see that may be an issue is a potential read-modify-write condition when you toggle GP0. I would expect the compiler to handle it correctly, but you never know.

-Bill