Page 1 of 1

variable transfer between functions - how?

Posted: Fri Feb 10, 2017 3:20 am
by hattila80
Hello All, please help.
I`m trying to figure out how can I give parameters to my function that is using global variables with microchip XC8 compiler.
My current method:

Code: Select all

near unsigned char gt1,gt2,gt3,gt4,gt5,gt6,gt7,gt8;
.....
void LCDPrint(unsigned char *pic)
      {     
            ......
            temp1=*(pA),pA=pA+SizeX;
            temp2=*(pA),pA=pA+SizeX;
            temp3=*(pA),pA=pA+SizeX;
            ....
            temp8=...
            LCDSend8x8(temp1,temp2,temp3.....);
      }

void LCDSend8x8(unsigned char a, unsigned char b,unsigned char c,unsigned char d,unsigned char e,unsigned char f,unsigned char g,unsigned char h)
     {
           unsigned char i;
           gt1=a,gt2=b,gt3=c,gt4=d,gt5=e,gt6=f,gt7=g,gt8=h;
           .....
     }

You can see my bad coding skills, but I understand that I`m using lot of RAM.
What I want to do instead is something like this:
void LCDSend8x8(first input -> gt1, second input -> gt2 ......

so basically when my LCDSend8x8 routine receives the 8 bytes, it should put them straight to my gt1-8 global variables. This is my question how to do this.
Instead as you can see I`m putting them into a,b,c,d,e,f,g,h and then copy them into my gt1-8 so I`m using double the RAM.

Thanks !

Re: variable transfer between functions - how?

Posted: Fri Feb 10, 2017 7:21 am
by phalanx
To make the variables global, make sure they are defined outside of any of your functions and if you are using multiple source files, mark their declaration with "EXTERN" so that the linker knows to look for the variable in other object files.

In your LCDPrint() function, you can get rid of your "temp" variables and directly use your "gt" global variables. Change your send function to not expect any passed variables (void LCDSend8x8(void)) and modify the code in it to refer directly to the globals.

Code: Select all

extern unsigned char gt1,gt2,gt3,gt4,gt5,gt6,gt7,gt8;
.....
void LCDPrint(unsigned char *pic)
      {     
            ......
            gt1=whatever1;
            gt2=whatever2;
            gt3=whatever3;
            ....
            gt8=whatever8...
            LCDSend8x8();
      }

void LCDSend8x8(void)
     {
           display_char(gt1);
           display_char(gt2);
           .....
     }
The simulator in MPLAB does a good job of letting you know if a variable is within the scope of a function. A global should always be within scope so you can access it anywhere.

-Bill