- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
In your program, you are doing
while(n<sizeof(value))
{
value
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++;
}
Then val_count survived even after returning from the routine 😉
May be you could have used
while(n < (sizeof(value)/sizeof(uint16_t)))
moto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you please try to set the compiler optimization from "debug" to "none".
Project->Build Settings->ARM->Compiler->Optimization
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This link has similar discussion that can be useful:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
sign = 0;
val = 0;
n++;
goto check;
}
else if (buff2 == '#')
{
val_count = n;
while (n < sizeof(value))
{
value
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");
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
In your program, you are doing
while(n<sizeof(value))
{
value
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++;
}
Then val_count survived even after returning from the routine 😉
May be you could have used
while(n < (sizeof(value)/sizeof(uint16_t)))
moto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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++;
}
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
thus error is generated.
Thank You
All
BoB, Anks, Motoo tanaka .