Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interrupt on Change Problem

Status
Not open for further replies.

SlickSteiner

Newbie level 5
Joined
Jan 6, 2012
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,331
I am having a problem with implementing an IOC using the PIC16F84A where an LED should turn on when a push button is pressed. I can't seem to find as to what causes this problem, here is the code that I made.
Code:
#include <htc.h>

/*CONFIGURATION*/
__CONFIG(FOSC_XT & WDTE_OFF & CP_OFF & PWRTE_OFF);

/*PIN ASSIGNMENTS*/
#define BUTTON 4
#define bLED   0
/*GLOBAL VARIABLES*/
unsigned char sPORTB;           //Create Shadow copy of PORTB

/*FUNCTION PROTOTYPING*/
void init();
void toggle();

void main()
{
    init();                     //CALL Initialize Function
    //MAIN LOOP
    for(;;)
    {
        PORTB = sPORTB;         //Update PORTB
    }
}

/*INITIALIZE Function*/
void init()
{
    /*INITIALIZATIONS*/
    TRISB = ~(1<<bLED);         //SET RB0 as OUTPUT
    PORTB = 0x00;         //SET PORTB to 0
    sPORTB = 0x00;        //SET Shadow of PORTB to 0
    
    /*CONFIGURE INTERRPUT*/
    RBIE = 1;                   //ENABLE PORTB Interrupt-on-change
    ei();                       //ENABLE Global Interrupt
}

/*INTERRUPT SERVICE ROUTINE*/
void interrupt isr(void)
{
    RBIF = 0;                   //CLEAR interrupt FLAG
    if(!(PORTB & 1<<BUTTON))    //If BUTTON is Down
    {
        sPORTB ^= (1<<bLED);
    }
}
 

Maybe it is operator precedence?
Try using brackets.

Code:
if(RBIF == 1)
  {
  if(!(PORTB & (1 << BUTTON)))        // Use more brckets!
    {
    sPORTB ^= (1<<bLED);
    RBIF = 0;
    }
  }
 
I did what you said and it worked! Thank You Very Much!
 

I would like to ask, if I want to add multiple interrupt on change pins. how would I do it?
 

You have to look at the port in the interrupt routine to see which pin has changed.
 

goto Microchip.com and do a search for 00566b.pdf - It talks about the interrrupt on change feature of RB7-4 for the PIC16F877. I don't know - yours may be the same.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top