Supercap2F
- Mar 22, 2014
- 550
- Joined
- Mar 22, 2014
- Messages
- 550
Hello Everyone!
As most of you probably know, I have been trying to figure out PICs for a while now. Well I think I have made a lot of progress from googling around and reading a lot of data sheets. But right now I'm trying to figure out interrupts. I have read some articles and the section of the PIC18F1220 data sheet that explains it, and I think that I have an OK grasp on it (emphasis on 'think' ).
So, I decided to write up some code from what I understand about them, here it is:
What it's supposed to do is blink an LED on and off until a switch is pressed and an interrupt is created. The interrupt holds the LED on for two seconds and then returns to where the code was last. The LED is on pin RA0 and the switch is on RB0 (INT0). Here's my breadboard setup:
The LED blinks as expected, but when the switch is pressed nothing happens...
I wonder if one you guys could point me in the right direction? I really don't know what to do to fix it.
Thanks very much!!!
Dan
EDIT: Opps, forgot to say that the data sheet can be found here.
As most of you probably know, I have been trying to figure out PICs for a while now. Well I think I have made a lot of progress from googling around and reading a lot of data sheets. But right now I'm trying to figure out interrupts. I have read some articles and the section of the PIC18F1220 data sheet that explains it, and I think that I have an OK grasp on it (emphasis on 'think' ).
So, I decided to write up some code from what I understand about them, here it is:
Code:
/* This code was written on 1/21/15, and is made for
* leaning how to use an Interrupt. The author is
* Supercap2F.
*
* The code flashes an LED until a button on RB0 is
* pressed. Upon the press, the code interrupts and
* turns the LED on for two seconds, and then goes
* back to flashing the LED.
*/
// Main MCU Setup ********************************
#include <xc.h> //include main PIC header
#include <delays.h> //include delay file
/* | Turns off the Watch Dog Timer
* | Turns on the Power Up Delay
* | Turns the Internal RC Clock on.
*/
#pragma config OSC=INTIO2, PWRT=ON, WDT=OFF
/* | Turns off the low Voltage Programing Mode
* | Turns off In Circuit Debugging
* | Turns off the MCLRE function
*/
#pragma config MCLRE=OFF, DEBUG=OFF, LVP=OFF
// Interrupt code *********************************
void interrupt isr(void) //Interrupt service routine
{
INTCONbits.INT0IF=0; //set flag back to orignal state
PORTAbits.RA0=1; //turn on RA0
Delay10KTCYx(400); //delay for 2s
}
// Main Program ***********************************
void main(void)
{
// General Register Setup *********************
TRISA=0x00; //All RAx I/O pins are set as outputs
PORTA=0x00; //Turn off all PORTA outputs
TRISB=0x01; //RB0 is set as an input, all others are outputs
PORTB=0x00; //Turn off all PORTB outputs
// Oscillator Mode *****************************
OSCCONbits.IRCF0=1; //sets clock to 8MHz
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF2=1;
// Interrupt Setup ****************************
RCONbits.IPEN=0; //Turn off interrupt priority
INTCONbits.INT0IE=1; //Enables interrupt on INT0 change
INTCONbits.PEIE=0; //Disable all peripheral interrupts
INTCON2bits.INTEDG0=1; //Sets interrupt to trigger on the rising edge
//INTCONbits.INT0IF INT0 flag bit
INTCONbits.GIE=1; //enable interrupts
/**** Main Program ****/
while(1) //endless loop
{
PORTAbits.RA0=1; //turn on RA0
Delay10KTCYx(40); //delay for 0.1s
PORTAbits.RA0=0; //turn off RA0
Delay10KTCYx(40); //delay for 0.1s
}
}
The LED blinks as expected, but when the switch is pressed nothing happens...
I wonder if one you guys could point me in the right direction? I really don't know what to do to fix it.
Thanks very much!!!
Dan
EDIT: Opps, forgot to say that the data sheet can be found here.
Last edited: