View previous topic :: View next topic |
Author |
Message |
Srigopal007
Joined: 13 Jun 2005 Posts: 11
|
Please help!! |
Posted: Wed Feb 14, 2007 4:58 pm |
|
|
Lets say I have two files FILE2.C and FILE3.c
File2.C is calling File3.C by doing the following
In File2.C I have defined a global variable called FLAGA and FLAGB by doing the following
Code: | #define FLAGA 0
#define FLAGB 0 |
Now when I try to use these two variables (FLAGA and FLAGB) in FILE3.C it gives me the following error.
"Error[12] C:\Documents and Settings\Desktop\PROJECT1\File3.c 703 : Undefined identifier FlagA
1 Errors, 0 Warnings.
Halting build on first failure as requested.
BUILD FAILED: Wed Feb 14 14:49:05 2007"
Can someone please help. I am trying to use the global variable (FLAGA and FLAGB) that are already defined in the main file (FILE2.C) I dont see what the problem is?
Any help on this would be greatly appreciated, thanks
Srig |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 14, 2007 5:14 pm |
|
|
Remember a 'define', only occurs sequentially. It is not retroactive. As I read it, you have 'file2', which includes 'file3'. You then have some defines in file2. T make these have effect in 'file3', you need (in file2):
Code: |
#define FLAGA 0
#define FLAGB 0
#include <File3.c>
|
Not ethe order. Because the defines occur _before_ 'file3', they will be valid inside it.
Best Wishes |
|
|
Srigopal007
Joined: 13 Jun 2005 Posts: 11
|
|
Posted: Wed Feb 14, 2007 5:20 pm |
|
|
Great, that error went away. Another one popped up
I am using these two variables (FLAGA and FLAGB) in File2.c and also in file3.c . I want to use these two variables as CHARS ot INT8. Is there anyway I can define the two FLAG variable so that I can use them at logic variables 1 or 0?
I hope I am clear |
|
|
Srigopal007
Joined: 13 Jun 2005 Posts: 11
|
|
Posted: Wed Feb 14, 2007 5:45 pm |
|
|
I want to be able to change the value of FLAGA and FLAGB in both of these files File2.C and File3.c How do I declare this type of variable.
Can someone please help, thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 14, 2007 6:12 pm |
|
|
One way to do it, is to declare the individual bits of a byte as flags.
The #bit directive can do this.
Code: | #include <16F877.H>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
int8 my_flags;
#bit flag0 = my_flags.0
#bit flag1 = my_flags.1
#bit flag2 = my_flags.2
#bit flag3 = my_flags.3
// etc.
//========================================
void main()
{
flag0 = 1;
flag1 = 0;
flag2 = 0;
flag3 = 0;
if(flag0)
{
printf("Hello World");
}
while(1);
} |
You could also do it with a structure and bitfields, as shown in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=28773&highlight=bitfields |
|
|
Srigopal007
Joined: 13 Jun 2005 Posts: 11
|
|
Posted: Wed Feb 14, 2007 6:35 pm |
|
|
PCM I think you misunderstood my question and the intention of what I plan to do.
Here is a snap of my code: I do not want to get people confused here
This is FILE2.C
Code: | #define LOAD Pin_C1
#define Reset Pin_C2
#define FlagA 0
#define FlagB 0
#include <File3.c>
void main()
{
char FLAGA;
char FLAGB;
while(1)
{
if(input (LOAD)==1)
FlagA = 1;
else
FlagA = 0;
if(input (RESET) == 1)
FlagB = 1;
else
FlagB = 0;
} |
Now I want to use the status of FLAGA and FLAGB in File3.C
similar to this
File3.C:
Code: | while(1)
{
if ((FlagA == 0) || (FLAGB == 0)) break;
delay_ms(25); //pausing for 25 ms
if ((FlagA == 0) || (FLAGB == 0)) break;
delay_ms(25);
if ((FlagA == 0) || (FLAGB == 0)) break;
delay_ms(25);
if ((FlagA == 0) || (FLAGB == 0)) break;
delay_ms(25);
if ((FlagA == 0) || (FLAGB == 0)) break;
delay_ms(25);
} |
If someone can please help me with this, that would be great, thanks |
|
|
rwskinner
Joined: 08 Dec 2006 Posts: 125 Location: Texas
|
|
Posted: Wed Feb 14, 2007 7:07 pm |
|
|
I'm pretty new to C, but to me it looks like you define local variables in your Main, therefor they will not be available outside of the Main Function.
The Defines above your include are precompiler constants, not variables.
Try maybe to define your "global" variable before you include your file3.c
byte FlagA = 0
byte FlagB = 0
#include <File3.c> |
|
|
Guest
|
|
Posted: Wed Feb 14, 2007 9:32 pm |
|
|
RWskinner,
you gave me a good idea. I looked this concept up in the C reference book and you seem to be right. I will try to do this later when I get a chance, and then post the results here there after. Thanks everyone for your contribution.
Srig |
|
|
Srigopal007
Joined: 13 Jun 2005 Posts: 11
|
|
Posted: Thu Feb 15, 2007 10:57 am |
|
|
Rwskinner, thanks for your input, The advise which you gave me works like a charm. Global Variables declared the way you mentioned works in all the files.
thanks everyone who contributed and special thanks to rwskinner for the advise.
Srig |
|
|
rwskinner
Joined: 08 Dec 2006 Posts: 125 Location: Texas
|
|
Posted: Thu Feb 15, 2007 11:01 am |
|
|
Your very welcome, glad I could help. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
Re: PLEASe help!! |
Posted: Thu Feb 15, 2007 11:31 am |
|
|
Srigopal007 wrote: | Lets say I have two files FILE2.C and FILE3.c
File2.C is calling File3.C by doing the following
In File2.C I have defined a global variable called FLAGA and FLAGB by doing the following
Code: | #define FLAGA 0
#define FLAGB 0 |
|
By the way, these are not "variables" they are used as tags for the pre-processor to substitue a zero everwhere it finds FLAGA or FLAGB.
I see below that you found a solution to your problem but please be more careful and take the time to understand the difference between variables and constants and #defines. Also learn the basic rules of variable scope. Those will save you LOTS of trouble in the future.
Get a copy of Kernigan & Richie's "The C Lanugage" 2nd edition. VERY VERY useful book in these situations. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|