You are here

Global variables not getting initialized or may be corrupted | Cypress Semiconductor

Global variables not getting initialized or may be corrupted

Summary: 4 Replies, Latest post by danaaknight on 19 Mar 2013 04:17 AM PDT
Verified Answers: 0
Last post
Log in to post new comments.
rockym's picture
24 posts


I am using a global variable x, declaring it in some c1 file. This c1 file has an  interrupt code. x is altered on interrupt.

Now in some  c2 file i am using this x

extern x ;

But the x is never seen altered in C2 even it is being altered in C1 file when interrupt comes.

I have used OCD  to verify this.

Now if i put the entire C1 file code into c2 file to avoid this extern, it works fine. 

What is the scope and lifetime  of global variable?

How do i communicate between files.

I spent a whole day on this crap. 

rockym's picture
24 posts

 Found the mistake


I was doing

extern variable_name;

But it should be

extern datatype variable_name ;



extern BYTE x;

user_1377889's picture
9294 posts

... and you may spend some more days, with such a fricken bug.


I can assure you that your environment as you described it will run as expected.


When reducing your code to the absolute necessary (a main.c with the variable declared as volatile extern and inspecting it, a second file with a timer-isr incrementing that global var here).


Did you qualify your variable as volatile? Have a look here in my favourite online C-manual what it is for:



rjvb's picture
Cypress Employee
90 posts

When we don't declare a datatype for a variable, compiler assumes it as "int" - a 2 byte variable. In c2 file, it is being treated as "int". This will cause problems. For instance, if you try to copy this variable to a byte type variable, it will take the data from a (address+1) location of "int" variable; but the data will be actually in (address).  Try it out with a simple program and look into the .lst file. 

user_14586677's picture
7646 posts

The size of INT is compiler dependent,and varies in ANSI from one revision level

to another. Basically stating an INT should be close to the HW implementation of

the machine. In Imagcraft and HiTech 16 bits.


I saw a reference current ANSI is 4 bytes for a 32 bit platform.


The only relevancy this has to PSOC 1 is porting in and out of PSOC 1.


Regards, Dana.



Log in to post new comments.