Thread: Passing a global variable from Main.c to an ISR file

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    3

    Passing a global variable from Main.c to an ISR file

    I am writing my first C program. It is going very good but I have run into one problem... This is being written in Codewarrior for a Freescale MCU.

    I have declared a variable in Main.c

    Code:
    volatile int seconds=10;    //a seconds counter
    Then in Main.c loop, I check the value of counter and turn on an LED if it is zero:

    Code:
     if (!seconds)
    {
      led_blue_on();
    }
    I have an ISR that is running and clocking at 1 seconds that decrements the variable 'seconds'. Here is the ISR code:

    Code:
    */
    __interrupt void isrVrtc(void)
    {
      /* Write your interrupt code here ... */
    #include "MyEquates.h"
    volatile int seconds;       //have to add this to compile this file
    
    RTCSC_RTIF=1;             //reset RTC flag
     
     if (seconds!=0){           //update seconds counter
      seconds--;                    //reduce count
     }
    }
    /* end of isrVrtc */
    So my problem is that the variable 'seconds' is not the same in Main.c as it is in my ISR file. So 'seconds' in main never gets decremented. But I can see that 'seconds' in my ISR does decrement in another part of memory.

    How can I declare my variable 'seconds' so that its value is the same in Main.c and my ISR file? I tried adding 'volatile', which I thought would solve the problem by making it global. But it did not.

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Assuming the variable is actually global in main.c, then in isr.c you need this at the global scope also.
    extern volatile int seconds; //have to add this to compile this file
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    3

    Wink

    Quote Originally Posted by Salem View Post
    Assuming the variable is actually global in main.c, then in isr.c you need this at the global scope also.
    extern volatile int seconds; //have to add this to compile this file

    That is perfect! I tried it and it solved the problem. Thank you!

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    But once the seconds variable reaches 0 you need to reload the value else it will execute only once.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    3
    Quote Originally Posted by Satya View Post
    But once the seconds variable reaches 0 you need to reload the value else it will execute only once.
    Yes...fully understood. This code was just to get it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing global variable in Main Method
    By Jimmy589 in forum C Programming
    Replies: 1
    Last Post: 04-13-2017, 01:16 AM
  2. Replies: 2
    Last Post: 03-25-2016, 05:34 AM
  3. string declaration as global vs a main local variable...
    By Jim Iwannou in forum C Programming
    Replies: 4
    Last Post: 11-14-2013, 03:12 AM
  4. how to scanf a global variable outside the main ?
    By Islam Assi in forum C Programming
    Replies: 3
    Last Post: 09-07-2013, 04:45 AM
  5. How to declare a global variable in Main()
    By vnrabbit in forum C Programming
    Replies: 2
    Last Post: 06-20-2002, 12:59 PM

Tags for this Thread