Having some probs working with interrupts

Thread Starter

raiyans

Joined May 6, 2015
3
Currently I have been revising my skills in programming PIC Micros. I have been simulating my projects first in Proteus and then implemening them using a PIC18F4620..

I am writing a very simple problem whereby I intend to use the INTO interrupt to switch off and initially on LED in Port D pin 0.
here is my C code;

Code:
========================================
# include<htc.h>

# define _XTAL_FREQ 8000000


// Define the interrupt function


void interrupt Flash_LED()
{
    if(INT0F)
    {
        RD0 = 0;

        INT0F = 0;
    
    }
}


void main()
{
    IPEN = 0; // disable interrupt priority
    GIE = 1;// Global interrut enable pit
    INT0F = 0;// rerset interrupt enable pit
    INT0IE = 1;// INTO interrupt enable pit
    //RBPU = 1; // disable pullups in port B
    TRISD0 = 0; // Configure pin 0 of the D port as an output pin
    TRISB0 = 1; // Configure pin 0 of the B port as an input pin
    RD0 = 1; // LED initially in on state
    while(1);
}
========================
and here is my Proteus schematics
proteus.jpg

Proteus schematics are also attached

When I use a pushbutton to enable the INTO interrupt nothing happens in the diode and therefore the interrupt is not functioning

does anybody have any idea??

Moderators note: Please use code tags for pieces of code.
 
Last edited by a moderator:

tshuck

Joined Oct 18, 2012
3,534
The default configuration bit states for this device makes your oscillator source, "0111 =External RC oscillator, port function on RA6".

I'd suggest you set your oscillator configuration.

When you say it isn't working, is this in simulation, or hardware?
 

JohnInTX

Joined Jun 26, 2012
4,787
A couple of things jump out. You need to:

Configure ADCON1 (write 0x0F to it) to disable the AD converter and make RB0 a digital input.
Make sure the CCP is turned off - write 0x00 to CCP1CON to avoid PORTD interactions.
Set INTEDG0 in INTCON2 to make the external interrupt on the rising edge as your button requires.
Set PEIE in INTCON to enable peripheral interrupts (including INT0) as well as GIE. Enabling GIE should be the LAST thing you do after configuring everything else.

Make sure the PIC is fully configured to run (all config bits configured, etc.) Start by just flashing the LED on and off to ensure that the IO is set up OK.
Consider debouncing the button to avoid multiple interrupts due to contact bounce.


Visit and READ the databook sections covering interrupts, IO ports, alternate pin functions (analog, etc). Then read everything else. The answers to all of your questions are right there.

Good luck!

EDIT: and what tshuck said while I was typing...
 

Thread Starter

raiyans

Joined May 6, 2015
3
The default configuration bit states for this device makes your oscillator source, "0111 =External RC oscillator, port function on RA6".

I'd suggest you set your oscillator configuration.

When you say it isn't working, is this in simulation, or hardware?
Thanks for your reply! well I tried to do what you said but it seems that my code is still not working. I am referring to the simulation now. I still have not prototyped the project.
 

Thread Starter

raiyans

Joined May 6, 2015
3
A couple of things jump out. You need to:

Configure ADCON1 (write 0x0F to it) to disable the AD converter and make RB0 a digital input.
Make sure the CCP is turned off - write 0x00 to CCP1CON to avoid PORTD interactions.
Set INTEDG0 in INTCON2 to make the external interrupt on the rising edge as your button requires.
Set PEIE in INTCON to enable peripheral interrupts (including INT0) as well as GIE. Enabling GIE should be the LAST thing you do after configuring everything else.

Make sure the PIC is fully configured to run (all config bits configured, etc.) Start by just flashing the LED on and off to ensure that the IO is set up OK.
Consider debouncing the button to avoid multiple interrupts due to contact bounce.

Visit and READ the databook sections covering interrupts, IO ports, alternate pin functions (analog, etc). Then read everything else. The answers to all of your questions are right there.

Good luck!

EDIT: and what tshuck said while I was typing...
Thanks mate.. did all what you said, my code is however still not working in the simulation. I still have not implemented the project on a breadboard.
 

tshuck

Joined Oct 18, 2012
3,534
Thanks for your reply! well I tried to do what you said but it seems that my code is still not working. I am referring to the simulation now. I still have not prototyped the project.
These simulators sometimes have trouble accounting for some of the things hardware does, but I hope it would be able to simulate an interrupt at least.

What is your current code?

Edit: What is your button connected to when it's pressed, it doesn't seem to be connected to Vdd...
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
We are still waiting on your definition of "not working".

Does it do anything? Can you step thru code? Does your PC explode into fire and ash?
 
Top