cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 5, 3 & 1

PAB
New Contributor II

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. 

0 Likes
1 Solution
MotooTanaka
Esteemed Contributor

Hi,

In your program, you are doing

           while(n<sizeof(value))

            {

                value=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=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

View solution in original post

0 Likes
6 Replies
Bob_Marlowe
Expert II

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

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

Bob

0 Likes
PAB
New Contributor II

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.

0 Likes
AnkitaS_51
Employee

This link has similar discussion that can be useful:

scope - Updating a global variable in C - Stack Overflow

0 Likes
PAB
New Contributor II

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 + 1;

    while ((buff2 != '#') && (buff2 != '_'))

    {

      syntax = buff2;

      b = b + 1;

      a = a + 1;

    }

    while (a < sizeof(syntax))

    {

      syntax = 0;

      a = a + 1;

    }

check:  if (buff2 == '_')

    {

      b = b + 1;

      while ((buff2 != '#') && (buff2 != '_'))

      {

        if (buff2 == '-')

        {

          sign = 1;

          b = b + 1;

        }

        val = val * 10 + (buff2 - 48);

        b = b + 1;

      }

      if (sign)

      {

        val = 0 - val;

      }

      value = val;

      sign = 0;

      val = 0;

      n++;

      goto check;

    }

    else if (buff2 == '#')

    {

      val_count = n;

      while (n < sizeof(value))

      {

        value = 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");

      }

    }

  }

}

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

In your program, you are doing

           while(n<sizeof(value))

            {

                value=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=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

View solution in original post

0 Likes
PAB
New Contributor II

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=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=0 where n=5 it will store 0 to val_count.

thus error is generated.

Thank You

All

BoB, Anks, Motoo tanaka .

0 Likes