CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Global variables.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ebarnes



Joined: 16 Apr 2007
Posts: 11

View user's profile Send private message

Global variables.
PostPosted: Thu Apr 19, 2007 8:55 am     Reply with quote

I am using "Static" to define variable "RfidReq", but I still get this error message.

"Undefined identifier
The specified identifier is being used but has never been defined. Check the spelling."

I am declaring it in the Interupt, because when i declared it inthe main, i got the error in the interupt. *Sigh*

-----------------------------------------------------------
#int_RDA
void RDA_isr()
{
static char RfidReq[8];
static int CharCnt;
RfidReq[CharCnt]=getc();
putc(RfidReq[CharCnt]);
CharCnt++;
if(CharCnt>=8)
{
CharCnt=0;
}
}
---------------------------------------------------------
and using it here in Main.
---------------------------------------------------------
void main()
{
static int CharComp;
static char master[8];// this is the master key in decimal



setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
enable_interrupts(INT_RDA);
enable_interrupts(INT_SSP);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
setup_oscillator(OSC_4MHZ);


master[0]='0';///lsb
master[1]='6';
master[2]='9';
master[3]='1';
master[4]='2';
master[5]='7';
master[6]='1';
master[7]='5';///msb



while (CharComp >=-1)
{
if (RfidReq[CharComp] == master[CharComp])// this is the line the error occures on
CharComp++;
else
CharComp = -10;
}

}

----------------------------------------------------
when i compile, i get the error only on the specific variable.
Question
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Apr 19, 2007 9:23 am     Reply with quote

Your poll is useless, by it's asynchronous nature an interrupt can never return a variable.
Explanation: As you can never know when the interrupt will occur, how and when were you going to test for the result?

Quote:
I am declaring it in the Interupt, because when i declared it inthe main, i got the error in the interupt. *Sigh*
Make it a global variable.


Code:
 while (CharComp >=-1)
{
if (RfidReq[CharComp] == master[CharComp])// this is the line the error occures on
CharComp++;
else
CharComp = -10;
}
Bug 1: By default the variables in the CCS compiler are unsigned, if you want to use signed variables you have to declare them as such.

Bug 2: change the test '>=' to '>'

Dangerous: You are doing a call to putc() in the interrupt handler. For interrupts there is the requirement that they should be executed as quick as possible, at least faster than the time required to handle a single interrupt. Here you might be in error when the putc() is outputting data at the same or a slower baudrate than you are receiving with.
ebarnes



Joined: 16 Apr 2007
Posts: 11

View user's profile Send private message

PostPosted: Thu Apr 19, 2007 9:36 am     Reply with quote

Thank-you for your Critique, but You didn't address why I am getting the error.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Apr 19, 2007 9:59 am     Reply with quote

If the variable is updated in a isr it has to be declared in global scope.
so that it can be used outside your void RDA_isr(){} function.

Difference between static and global variables.
2nd paragraph of item 1.
http://c.ittoolbox.com/documents/popular-q-and-a/difference-between-static-global-variable-1596
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Apr 19, 2007 10:13 am     Reply with quote

Your static variable is defined inside of your ISR and can only be used inside the ISR. When you try to use a variable inside of main(), even though it has the same name, it is considered to be a completely different variable. You will need to declare a variable inside of main() to use it inside of main(). If you want to access the same variable inside of the ISR and inside of main() then you will need to declare it at the top of the code and make it a Global variable.

Also, It would not be a good idea to declare different variables with the same name inside of different functions. Even though the compiler will handle them properly, it may cause confusion for the programmer on which variable he is manipulating. Mistakes can creep into your code if you practice this type of programming.

Ronald
ebarnes



Joined: 16 Apr 2007
Posts: 11

View user's profile Send private message

PostPosted: Thu Apr 19, 2007 10:16 am     Reply with quote

what is the syntax for defining a global variable? I cannot find it in the help sections.
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Thu Apr 19, 2007 10:36 am     Reply with quote

Global variables have the same syntax as local variables, they are just declared outside of main or any other code block.

This is standard C syntax and is not specific to the CCS compiler, so it may not be discussed in the help files.
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Thu Apr 19, 2007 11:22 am     Reply with quote

ckielstra wrote:

Dangerous: You are doing a call to putc() in the interrupt handler. For interrupts there is the requirement that they should be executed as quick as possible, at least faster than the time required to handle a single interrupt. Here you might be in error when the putc() is outputting data at the same or a slower baudrate than you are receiving with.


When one need to receive & forward chars between two serials how should one do?

I know this:
http://www.ccsinfo.com/faq.php?page=interrupts_disabled

But I don't find a more efficient method than:

Code:
fputc(fgetc(STREAM1), STREAM2);


coded into the RDA isr.

Bye
Marco / iw2nzm
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group