6 Replies Latest reply on Nov 18, 2018 9:59 PM by PaBr_3664931

    Global Variable Not Updating in Local Scope

    PaBr_3664931

      Hello All

       

      I am working on UART  on PSOC5  CY8C5868AXI-LP05 Device in this i had a Problem that the Global variable is not updating in  a function.

       

      let consider the data passed to device over uart is  $CWR_123_10_-12_999#.

       

      the local variable in the function uint16 n is updating the  Global variable uint16 val_count.

       

      when returning to main program from function the val_count is 0. 

        • 1. Re: Global Variable Not Updating in Local Scope
          JoMe_264151

          Can you please try to set the compiler optimization from "debug" to "none".

          Project->Build Settings->ARM->Compiler->Optimization

           

          Bob

          • 2. Re: Global Variable Not Updating in Local Scope
            PaBr_3664931

            Hello BoB.

             

             

            I tried as you suggested this solved my previous problem but because of this setting, it introduced problem in UART transmitting.

             

            The data i am transmitting is not received after applying this setting.

            • 4. Re: Global Variable Not Updating in Local Scope
              PaBr_3664931

              Hello Anks

               

              I already Refereed this discussion the problem i encounter is related to PSOC Creater IDE and its Compiler.

               

              The code attached  in previous post i run that code in Codeblocks IDE with fixed input data stored in buff2[] and works fine as it should work the global variable is updating. ! !

               

              #include<stdio.h>

              #include<string.h>

               

              char buff2[30] = {'$', 'C', 'C', 'W', 'R', '_', '1', '2', '_', '-', '3', '6', '0', '_', '9', '9', '#'}, syntax[10], temp_arr[10];

              unsigned int print = 0, cmd = 0, val_count = 0, cmd_error = 0, cmd_error_type = 0;

              int value[4];

               

              void process_data()

              {

                unsigned int a = 0, b = 0, sign = 0, n = 0;

                int val = 0;

               

                if (buff2[b] == '$')

                {

                  b = b + 1;

                  while ((buff2[b] != '#') && (buff2[b] != '_'))

                  {

                    syntax[a] = buff2[b];

                    b = b + 1;

                    a = a + 1;

                  }

                  while (a < sizeof(syntax))

                  {

                    syntax[a] = 0;

                    a = a + 1;

                  }

               

              check:  if (buff2[b] == '_')

                  {

                    b = b + 1;

                    while ((buff2[b] != '#') && (buff2[b] != '_'))

                    {

                      if (buff2[b] == '-')

                      {

                        sign = 1;

                        b = b + 1;

                      }

                      val = val * 10 + (buff2[b] - 48);

                      b = b + 1;

                    }

                    if (sign)

                    {

                      val = 0 - val;

                    }

                    value[n] = val;

                    sign = 0;

                    val = 0;

                    n++;

                    goto check;

                  }

                  else if (buff2[b] == '#')

                  {

                    val_count = n;

                    while (n < sizeof(value))

                    {

                      value[n] = 0;

                      n = n + 1;

                    }

                  }

                }

              }

               

              int main()

              {

                print = 1;

                if (print)

                {

                  process_data();

               

                  if (val_count > 3)

                  {

                    printf("value :- %d\n", val_count);

                    cmd_error = 1;

                    cmd_error_type = 1;

                  }

                  if ((!cmd_error) && (!cmd_error_type))

                  {

                    if (strcmp(syntax, "CWR") == 0)

                    {

                      cmd = 1;

                    }

                    if (strcmp(syntax, "CCWR") == 0)

                    {

                      cmd = 2;

                    }

                    if (strcmp(syntax, "SCAN") == 0)

                    {

                      cmd = 3;

                    }

                    if (strcmp(syntax, "SPEED") == 0)

                    {

                      cmd = 4;

                    }

                    printf("%c \n", syntax[0]);

                    printf("cmd = %d \n", cmd);

                    print = 0;

                  }

                  else

                  {

                    if (cmd_error == 1 && cmd_error_type == 1)

                    {

                      printf("Error_wrong data");

                    }

                  }

                }

              }

              • 5. Re: Global Variable Not Updating in Local Scope
                MoTa_728816

                Hi,

                 

                In your program, you are doing

                 

                           while(n<sizeof(value))

                            {

                                value[n]=0;

                                n++;

                            }

                 

                Before returning from process_data(), val_count was cleared to 0.

                And when I debugged, n came to 8 where you value was uint16_t value[4].

                 

                So I modified your program as

                 

                            while(n < 4)  // 4 should be the count of value[] not the size

                            {

                                value[n]=0;

                                n++;

                            }

                 

                Then val_count survived even after returning from the routine ;-)

                 

                May be you could have used

                 

                     while(n < (sizeof(value)/sizeof(uint16_t)))

                 

                moto

                • 6. Re: Global Variable Not Updating in Local Scope
                  PaBr_3664931

                  Thank you so much Motoo Tanaka

                   

                  because of logical error i by mistake write 0 value to address that is same of val_count.

                   

                  while(n<sizeof(value))

                              {

                                  value[n]=0;

                                  n++;

                              }

                  here the return val of sizeof (value) will be 8 bcoz its a 2 byte variable.

                   

                  the address of val_count is 0x1fff81B0   and address of the first address of the array is 0x1fff81A8  thus 5th position address will be same as val_count address thus value[n]=0 where n=5 it will store 0 to val_count.

                   

                  thus error is generated.

                   

                   

                  Thank You

                   

                  All

                   

                  BoB, Anks, Motoo tanaka .