1

I am having a difficult time passing variables to functions--especially functions that are not in the same source file. I suspect these two problems are actually the same problem. I am sure this is somewhere on the internet, but I have done a lot of searching and I am now even more confused. Mostly I need someone to give me some direction on what I should be reading/searching for.

PROBLEM 1:

Say I have a source file named main.c. After the #includes and #defines, I declare a variable

int count;

I then declare a function

void increment () {
count++;
}

Within function main(); I call the function increment();, and then update PORTA to display it in LEDs. Both "count" and PORTA are assigned zero before main(); runs.

void main () {
     increment();
     PORTA = count;
}

The problem is that there appears to be two versions of "count". If this program was run, PORTA would never light an LED. However, if "PORTA = count;" were moved inside the function, it would increment properly. Furthermore, all hardware writes (Port, tris, etc) work fine inside the function, but variables I thought I declared globally do not. Thus, I assume the compiler is making a copy of "count" for the function call, and forgetting it when it returns.

I would normally just return a value from the function to get around this, but interrupt routines for the PIC cannot return a value, and I must use an interrupt.

What do I do? Surely I am missing a major concept!

PROBLEM 2: Example of a common issue

Say I am using the MLA device library and load the demo material for the HID_Mouse. Though it has ten million folders and source and header files that include each other, I am able to edit some of the subroutines and make it do my bidding. However, I need to declare a variable that is used both in main.c and modified by a function in app_device_mouse.c. How do I declare this thing so that it gets globally read/written, but I don't get declaration errors from the compiler?

../src/app_device_mouse.c:306: error: (192) undefined identifier "position_x"

i.e "You didn't declare 'int position_x' in app_device_mouse.c, even though you did in main.c

I'm not sure of the result of declaring it in both places, but something tells me that's a bad idea.

Thanks so much in advance for your time. I have learned a lot from this community!
-GB

GB Ward
  • 23
  • 1
  • 4
  • Try to limit your question to one language and one example. Problem 1 would appear to be an issue with you understanding the difference between local and global variables. Check out this question: http://stackoverflow.com/questions/13415321/difference-between-static-auto-global-and-local-variable-in-the-context-of-c-a – pedwards Aug 13 '15 at 19:31
  • How about learning C? This is treated in any book. – too honest for this site Aug 13 '15 at 20:14
  • Thanks @pedwards that was very helpful. I will also limit my questions in the future. – GB Ward Aug 13 '15 at 20:59

2 Answers2

1

For anyone who comes behind, the code in PROBLEM 1 was actually working code. My error instead was carelessly initializing my TRISC to 1 instead of 0xff; which means I was trying to run a button off an output. I should know better than that.

However, I was having this problem on other occasions by declaring my variables in main(); instead of outside the functions. This means I was trying to modify local variables inside a function that had not declared it - this was giving me nulls and garbage. Pedwards correctly identified that I was having trouble with global vs local variables; and "scope" was a really helpful keyword.

Declaring a variable as volatile is necessary for the variable to be modified by the ISR. After Oled's comment I was able to find this information on page 169 of the XC8 compiler manual.

GB Ward
  • 23
  • 1
  • 4
0

What you're missing is called "scope". It's not specific to XC8, any C book will help you.

PIC interrupts won't take/return anything for a reason. Define a global in the same file as your ISR is defined and read/change that. If you're going to write to it from the ISR declare it 'volatile':

volatile int foo = 0x00;

If you need to access it from another file (beginners shall avoid this) declare it 'extern' in this file (or include):

extern int foo;
Oleg Mazurov
  • 497
  • 5
  • 10
  • Dear goodness thank you. All I had to do was google "scope" and it all made sense. Also, adding "volatile" to my declare was all I needed to do to get my interrupt working. – GB Ward Aug 13 '15 at 20:57