need some help with interrupts and the 16f887.
I'm using MPLAB and writing my code in C. I am using the 44-pin demo board from Microchip. There is a push button wired to RBO/INTO which is normally high, and pulled low when the button is pressed.
I've already verified that the input and push button are working, however I dont think I'm setting up the interrupt properly. When the button is pressed, I'm try to turn PORTD on, and when the interrupt returns, PORTD is turned off.
However when I run the code, PORTD turns on right away. Any ideas?
#include <pic16f887.h>
#include <htc.h>
__CONFIG (DEBUG_ON, LVP_OFF, FCMEN_OFF, IESO_OFF, BOREN_OFF,
CPD_OFF, CP_OFF, MCLRE_ON, WDTE_ON, FOSC_INTRC_NOCLKOUT, BOR4V_BOR21V);
void intMain() {
PORTD=0xff;
INTF=0;
GIE=1;
}
void main(void){
TRISB = 0xff; //set PORTB as inputs
TRISD = 0x00; // Set PORTD as an Output
ANSEL = 0x00;
ANSELH = 0x00;
GIE=1;
INTE=1;
PORTD=0x00;
while(1)
{
PORTD=0x00;
}
}
EDIT
I've updated my code based on some suggestions below. Here is the current code. I've written it so that, when the ISR is called, port D will count from 0 to 256, then clear and exit the ISR. I've written in a trap to stop the ISR from exiting and keep it in an endless loop. When i start the controller, the port counts to 256, clears and restarts repeatedly.
#include <pic16f887.h>
#include <htc.h>
__CONFIG (DEBUG_ON, LVP_OFF, FCMEN_OFF, IESO_OFF, BOREN_OFF,
CPD_OFF, CP_OFF, MCLRE_ON, WDTE_ON, FOSC_INTRC_NOCLKOUT, BOR4V_BOR21V);
void interrupt intMain() {
int i,k;
i=0;
k=0;
while(i<256)
{
while(k<3000) //delay
k++;
i++;
k=0;
PORTD=i;
}
while(1){} //trap
//INTF=0;
}
void main(void){
TRISB = 0xff; //set PORTB as inputs
TRISD = 0x00; // Set PORTD as an Output
ANSEL = 0x00;
ANSELH = 0x00;
INTE=1;
GIE=1;
INTEDG=0;
nRBPU=0;
WPUB0=1;
PORTD=0x00;
while(1)
{
PORTD=0x00;
}
}