SlickSteiner
Newbie level 5
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);
}
}