SlickSteiner
Newbie level 5
I am a beginner at programming microcontrollers in C using MPLABX and HI-TECH C
I am having a problem with a code that I made where 4 LEDs will blink one after another, and when an Interrupt is detected it should stop at the position where the LED is on and blink 3 times.
The problem that I am encountering is when I service the interrupt, the LED doesnt blink.
Here is my code:
.
By the way, I am using the PIC16F84A microcontroller
I am having a problem with a code that I made where 4 LEDs will blink one after another, and when an Interrupt is detected it should stop at the position where the LED is on and blink 3 times.
The problem that I am encountering is when I service the interrupt, the LED doesnt blink.
Here is my code:
Code:
/*************************************************************************
*Description: LED Flash Interrupt *
* *
* Services an Interrupt when the external interrupt is asserted *
* *
* *
* Four LEDs blinks in succession until an interrupt is asserted where *
* it will blink in the specified LED for three times, and resumes after *
* ***********************************************************************
* Pin Assignments: *
* BUTTON - RB0 *
* *
* **********************************************************************/
#include <htc.h>
/*CONFIGURATION*/
__CONFIG(FOSC_XT & CP_OFF & WDTE_OFF & PWRTE_ON);
#define _XTAL_FREQ 4000000 //Crystal in Hz
/*PIN ASSIGNMENTS*/
#define BUTTON RB0 //BUTTON as RB0
/*GLOBAL VARIABLES*/
unsigned char sPORTB; //Shadow copy of PORTB
unsigned char store_LED = 1; //Stores the LED value
/*FUNCTION PROTOTYPING*/
void init();
/*MAIN FUNCTION*/
void main()
{
init();
//Main Loop
for(;;)
{
__delay_ms(500); //Delay of 500ms
sPORTB = (1<<store_LED); //Turns on the LED on the position specified by store_LED
store_LED++; //Increments the value of Store LED
if(store_LED == 5) //If store_LED is 5
{
store_LED = 1; //RESET store_LED to 1
}
PORTB = sPORTB; //UPDATE PORTB
sPORTB = ~(1<<store_LED); //RESET the previous position of store_LED to 0
}
}
void init()
{
TRISB = 0b11100001; //SET RB1 to RB4 as OUTPUTS
PORTB = 0b00000000; //SET PORTB to 0
sPORTB = 0b00000000; //SET Shadow of PORTB to 0
/*CONFIGURE INTERRUPTS*/
INTEDG = 0; //Trigger on Falling Edge
INTE = 1; //ENABLE External Input
ei(); //ENABLE Global Interrupt
}
void interrupt isr(void)
{
unsigned int led_count = 0; //led_count Variable
INTF = 0; //RESET Interrupt Flag
for(led_count=0;led_count <3;led_count++) //Repeat 3 times
{
__delay_ms(500); //Delay of 500ms
sPORTB ^= (1<<store_LED); //Toggles store_LED
}
}
By the way, I am using the PIC16F84A microcontroller
Last edited: