Having some probs working with interrupts

Discussion in 'Embedded Systems and Microcontrollers' started by raiyans, May 6, 2015.

  1. raiyans

    Thread Starter New Member

    May 6, 2015
    3
    0
    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 (Text):
    1.  
    2. ========================================
    3. # include<htc.h>
    4.  
    5. # define _XTAL_FREQ 8000000
    6.  
    7.  
    8. // Define the interrupt function
    9.  
    10.  
    11. void interrupt Flash_LED()
    12. {
    13.     if(INT0F)
    14.     {
    15.         RD0 = 0;
    16.  
    17.         INT0F = 0;
    18.    
    19.     }
    20. }
    21.  
    22.  
    23. void main()
    24. {
    25.     IPEN = 0; // disable interrupt priority
    26.     GIE = 1;// Global interrut enable pit
    27.     INT0F = 0;// rerset interrupt enable pit
    28.     INT0IE = 1;// INTO interrupt enable pit
    29.     //RBPU = 1; // disable pullups in port B
    30.     TRISD0 = 0; // Configure pin 0 of the D port as an output pin
    31.     TRISB0 = 1; // Configure pin 0 of the B port as an input pin
    32.     RD0 = 1; // LED initially in on state
    33.     while(1);
    34. }
    35. ========================
    36.  
    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: May 6, 2015
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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?
     
  3. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    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...
     
  4. raiyans

    Thread Starter New Member

    May 6, 2015
    3
    0
    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.
     
  5. raiyans

    Thread Starter New Member

    May 6, 2015
    3
    0
    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.
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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: May 12, 2015
  7. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    Has it occurred to you that sometimes simulators get it wrong?
    That is one reason I don't use simulators. I am a follower of the Bob Pease philosophy.
     
    MaxHeadRoom and JohnInTX like this.
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    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?
     
Loading...