MikroC Hardware Interrupt

chardwareinterruptspic

I am trying to program a PIC 16F876A using mikroC. I need the program to run when I push a button to a certain point, stop and wait till the button is pushed again before finishing.

void interrupt() // ISR 
{ 
 INTCON.INTF=0; // Clear the interrupt 0 flag 
 PORTC=~PORTC; // Invert (Toggle) the value at PortD 
 Delay_ms(1000); // Delay for 1 sec 
}

I have read that using a hardware interrupt is the way to do this but can not get mine to work.

Best Answer

You do not specify what is meant by "not working", so I'll just point out a few things.

The interrupt service routine posted does not seem to address the program function described. This routine toggles a port, waits a second, then goes back to whatever was running before the interrupt was triggered. Instead you say you want the program to wait for a button press, then do something, then wait for another button press.

If the program is doing nothing but waiting for an input, you don't really need an interrupt, just a polling loop. Interrupts should be used when you need to quickly respond to an event, or do something at a precise time. ISRs should finish quickly; never should one include a one second delay.

Another thing to consider: There is more to using an interrupt than writing an ISR. I'm not familiar with mikroC, but in general you will need to at least set up an interrupt vector, configure the I/O or peripheral to generate an interrupt, and enable interrupts. Have you done all this necessary initialization?

Also, upon entry to an interrupt routine, the CPU context, its status and registers, must be saved, and restored upon return. Typically the compiler is alerted to this by using a keyword, often interrupt, in the function definition. Does the compiler understand that this is an ISR?

Finally, make sure your watchdog doesn't timeout while you are waiting for input.

Oh, and don't forget: you may have to debounce the pushbutton.