Volatile variable with interrupt routines

Thread Starter

Gajyamadake

Joined Oct 9, 2019
254
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
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
    
    } 
}
 

dl324

Joined Mar 30, 2015
9,331
Any global variable that's used in an interrupt needs to be declared volatile so the compiler won't make optimizations that would cause that variable to not have the correct value when the interrupt routine is called.
 

nsaspook

Joined Aug 27, 2009
6,595
The SFR(s) used in the program are defined as volatile in a header file.

Example:
C:
/*
 * C Header file for the Microchip PIC Microcontroller
 * PIC16F877A
 */
#ifndef __XC8
#warning Header file pic16f877a.h included directly. Use #include <xc.h> instead.
#endif
.....

// Register: TMR0
#define TMR0 TMR0
extern volatile unsigned char           TMR0                __at(0x001);
#ifndef _LIB_BUILD
asm("TMR0 equ 01h");
#endif
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
254
Understand all the tools in your C toolbox:
I have spent enough time to understand volatile keyword. I have searched code example in c language but I couldn't find the complete code. I have read book but there are also no complete code. It's really difficult for me to understand use of volatile variable with timer code.
 

BobaMosfet

Joined Jul 1, 2009
891
I have spent enough time to understand volatile keyword. I have searched code example in c language but I couldn't find the complete code. I have read book but there are also no complete code. It's really difficult for me to understand use of volatile variable with timer code.
No, you haven't. Otherwise you wouldn't be asking :p - You're over-thinking it.

'volatile' is a keyword to the compiler. It means that the variable's value can be changed without warning by anything else. The compiler therefore must add additional code in the stackframe for house-keeping to restore the value before each use. Interrupt routines are allocated on the stack when needed, and discarded when exited. Their existence is temporary and thus volatile.
 
Last edited:
Top