I have seen volatile keyword tells the compiler that the value of the variable may change at any time. volatile keyword can be used in interrupt service routine
I want to see how volatile variable used in project. How to use volatile keyword in following code
Timer interrupt code PIC16F877A
I want to see how volatile variable used in project. How to use volatile keyword in following code
Timer interrupt code PIC16F877A
C:
:
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include <xc.h>
#define Timer_Load_Value 0
void initialize (void)
{
//Make all PORTD pins low
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
//Configured Port pin as input
TRISB = 0b00000001; //RB0 as input PIN for button
// Configured PORT pins as Output
TRISA = 0b00000000;
TRISC = 0b00000000; //RC7 pin as out for LED
TRISD = 0b00000000;
TRISE = 0b00000000;
}
void main(void)
{
//initialize port pins
initialize ();
// Timer0 with internal instruction clock and 64 as prescalar
OPTION_REG = 0b00000101;
TMR0 = Timer_Load_Value; // Load the time value
TMR0IF = 0; // clear timer interrupt flag updated line
TMR0IE = 1; //Enable timer interrupt bit
GIE = 1; //Enable Global Interrupt
while(1)
{
}
return;
}
void interrupt timer_isr()
{
if(TMR0IF==1)
{
TMR0 += Timer_Load_Value; //Load the timer Value
TMR0IF=0; // Clear timer interrupt flag
if(RB0 == 1) // check sensor is working
RC7 = 1; // LED ON
else
RC7 = 0; // LED OFF
}
}