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