Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

Global Variables

Global Variables

2005-09-25 by lehighuboy

The following question involves an LPC2129 and Keil ARM IDE, AA and 
CARM compiler.

Are global variables defined in the startup.s file? And, if that's 
correct, then where should the code be placed? Finally, how would you 
define a global float, unsigned int, char, and const char variables? 
(please respond with examples)

Thank you for your help!

Re: [lpc2000] Global Variables

2005-09-25 by Anton Erasmus

On 25 Sep 2005 at 4:11, lehighuboy wrote:

> The following question involves an LPC2129 and Keil ARM IDE, AA and
> CARM compiler.
> 
> Are global variables defined in the startup.s file? And, if that's
> correct, then where should the code be placed? Finally, how would you
> define a global float, unsigned int, char, and const char variables?
> (please respond with examples)
> 
> Thank you for your help!
> 
Why would you want to define global variables in the startup.s file ?
Define them as you would when writing normal C code. The only thing
the startup.s file does, is to setup a suitable environment for the C
application to run in, as defined in Standard C. 

Regards
  Anton Erasmus
-- 
A J Erasmus

Re: Global Variables

2005-09-25 by lehighuboy

The code is segmented to several C files by function, and a few 
variables are shared across functions. I defined those variables in 
the first compiled C file, however the other functions in subsequent 
files doesn't see them during the compile. I rather not lump 
everything into one file therefore I wanted to define global 
variables for all C files.

Thanks, Garrett

--- In lpc2000@yahoogroups.com, "Anton Erasmus" <antone@s...> wrote:
> On 25 Sep 2005 at 4:11, lehighuboy wrote:
> 
> > The following question involves an LPC2129 and Keil ARM IDE, AA 
and
> > CARM compiler.
> > 
> > Are global variables defined in the startup.s file? And, if that's
> > correct, then where should the code be placed? Finally, how would 
you
> > define a global float, unsigned int, char, and const char 
variables?
> > (please respond with examples)
> > 
> > Thank you for your help!
> > 
> Why would you want to define global variables in the startup.s 
file ?
> Define them as you would when writing normal C code. The only thing
> the startup.s file does, is to setup a suitable environment for the 
C
Show quoted textHide quoted text
> application to run in, as defined in Standard C. 
> 
> Regards
>   Anton Erasmus
> -- 
> A J Erasmus

Re: Global Variables

2005-09-25 by lehighuboy

http://www.keilsoftware.com/support/man/docs/ca/ca_ap_startupcode.htm

--- In lpc2000@yahoogroups.com, "Anton Erasmus" <antone@s...> wrote:
> On 25 Sep 2005 at 4:11, lehighuboy wrote:
> 
> > The following question involves an LPC2129 and Keil ARM IDE, AA 
and
> > CARM compiler.
> > 
> > Are global variables defined in the startup.s file? And, if that's
> > correct, then where should the code be placed? Finally, how would 
you
> > define a global float, unsigned int, char, and const char 
variables?
> > (please respond with examples)
> > 
> > Thank you for your help!
> > 
> Why would you want to define global variables in the startup.s 
file ?
> Define them as you would when writing normal C code. The only thing
> the startup.s file does, is to setup a suitable environment for the 
C
Show quoted textHide quoted text
> application to run in, as defined in Standard C. 
> 
> Regards
>   Anton Erasmus
> -- 
> A J Erasmus

Re: [lpc2000] Re: Global Variables

2005-09-25 by balaji cr

Hi,
Have you tried using 'extern'?

For example in file1.c, you have a global variable

int global_var;

In all other files say file2.c and file3.c, you can
use it as:

extern int global_var;

Google for more help. You may find more info and some
good books too.

Cheers,
Balaji 

--- lehighuboy <garrett.j.young@...> wrote:

> 
> The code is segmented to several C files by
> function, and a few 
> variables are shared across functions. I defined
> those variables in 
> the first compiled C file, however the other
> functions in subsequent 
> files doesn't see them during the compile. I rather
> not lump 
> everything into one file therefore I wanted to
> define global 
> variables for all C files.
> 
> Thanks, Garrett
> 
> --- In lpc2000@yahoogroups.com, "Anton Erasmus"
> <antone@s...> wrote:
> > On 25 Sep 2005 at 4:11, lehighuboy wrote:
> > 
> > > The following question involves an LPC2129 and
> Keil ARM IDE, AA 
> and
> > > CARM compiler.
> > > 
> > > Are global variables defined in the startup.s
> file? And, if that's
> > > correct, then where should the code be placed?
> Finally, how would 
> you
> > > define a global float, unsigned int, char, and
> const char 
> variables?
> > > (please respond with examples)
> > > 
> > > Thank you for your help!
> > > 
> > Why would you want to define global variables in
> the startup.s 
> file ?
> > Define them as you would when writing normal C
> code. The only thing
> > the startup.s file does, is to setup a suitable
> environment for the 
> C
> > application to run in, as defined in Standard C. 
> > 
> > Regards
> >   Anton Erasmus
> > -- 
> > A J Erasmus
> 
> 
> 


Dream is just a dream.  A goal is a dream with a plan and a deadline.
- Harvey Mackay

Re: [lpc2000] Re: Global Variables

2005-09-25 by Anton Erasmus

On 25 Sep 2005 at 15:41, lehighuboy wrote:

> 
> The code is segmented to several C files by function, and a few 
> variables are shared across functions. I defined those variables in
> the first compiled C file, however the other functions in subsequent
> files doesn't see them during the compile. I rather not lump
> everything into one file therefore I wanted to define global variables
> for all C files.

This looks like a C problem, not a LPC2000 specific problem.


If you define a global variable

int foo; 

in one C module, and you want to access it from a different module,
then you need to define

extern int foo;

in the other module. This is standard C.

I suggest that you buy a book on C programming. If you are still struggling
with basic C concepts, then writing C for an embedded processor is going
to catch you out at some point. Just do not buy anything written by Herbert Schildt.
"Kernighan and Ritchie, The C Programming Language, 2nd Edition" is a good 
book to get.

Regards
  Anton


> Thanks, Garrett
> 
> --- In lpc2000@yahoogroups.com, "Anton Erasmus" <antone@s...> wrote: >
> On 25 Sep 2005 at 4:11, lehighuboy wrote: > > > The following question
> involves an LPC2129 and Keil ARM IDE, AA and > > CARM compiler. > > >
> > Are global variables defined in the startup.s file? And, if that's >
> > correct, then where should the code be placed? Finally, how would
> you > > define a global float, unsigned int, char, and const char
> variables? > > (please respond with examples) > > > > Thank you for
> your help! > > > Why would you want to define global variables in the
> startup.s file ? > Define them as you would when writing normal C
> code. The only thing > the startup.s file does, is to setup a suitable
> environment for the C > application to run in, as defined in Standard
> C. > > Regards >   Anton Erasmus > -- > A J Erasmus
> 
> 
> 
> 
> ------------------------ Yahoo! Groups Sponsor
> --------------------~--> Fair play? Video games influencing politics.
> Click and talk back!
> http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/dN_tlB/TM
> --------------------------------------------------------------------~-
> > 
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 
> 

-- 
A J Erasmus

Re: Global Variables

2005-09-25 by lehighuboy

Sorry for the simple question. Everything works, thanks.

--- In lpc2000@yahoogroups.com, balaji cr <balaji_c_r@y...> wrote:
> Hi,
> Have you tried using 'extern'?
> 
> For example in file1.c, you have a global variable
> 
> int global_var;
> 
> In all other files say file2.c and file3.c, you can
> use it as:
> 
> extern int global_var;
> 
> Google for more help. You may find more info and some
> good books too.
> 
> Cheers,
> Balaji 
> 
> --- lehighuboy <garrett.j.young@g...> wrote:
> 
> > 
> > The code is segmented to several C files by
> > function, and a few 
> > variables are shared across functions. I defined
> > those variables in 
> > the first compiled C file, however the other
> > functions in subsequent 
> > files doesn't see them during the compile. I rather
> > not lump 
> > everything into one file therefore I wanted to
> > define global 
> > variables for all C files.
> > 
> > Thanks, Garrett
> > 
> > --- In lpc2000@yahoogroups.com, "Anton Erasmus"
> > <antone@s...> wrote:
> > > On 25 Sep 2005 at 4:11, lehighuboy wrote:
> > > 
> > > > The following question involves an LPC2129 and
> > Keil ARM IDE, AA 
> > and
> > > > CARM compiler.
> > > > 
> > > > Are global variables defined in the startup.s
> > file? And, if that's
> > > > correct, then where should the code be placed?
> > Finally, how would 
> > you
> > > > define a global float, unsigned int, char, and
> > const char 
> > variables?
> > > > (please respond with examples)
> > > > 
> > > > Thank you for your help!
> > > > 
> > > Why would you want to define global variables in
> > the startup.s 
> > file ?
> > > Define them as you would when writing normal C
> > code. The only thing
> > > the startup.s file does, is to setup a suitable
> > environment for the 
> > C
> > > application to run in, as defined in Standard C. 
> > > 
> > > Regards
> > >   Anton Erasmus
> > > -- 
> > > A J Erasmus
> > 
> > 
> > 
> 
> 
> Dream is just a dream.  A goal is a dream with a plan and a 
deadline.
> - Harvey Mackay

RE: [lpc2000] Re: Global Variables

2005-09-26 by David Hawkins

> > 
> > The code is segmented to several C files by function, and a few 
> > variables are shared across functions. I defined those variables in
> > the first compiled C file, however the other functions in subsequent
> > files doesn't see them during the compile. I rather not lump
> > everything into one file therefore I wanted to define global variables
> > for all C files.

Hi,

It is generally good practice not to use global variables anywhere,
unless performance of the application is critical. Instead, where
multiple functions are required to access variables, put them in
a file by themselves, or in the case where another file needs
access to a variable, provide accessors; i.e., set and get functions.

Interrupt service routines are a good example of where a global
might need to be accessed.

eg.
  So you might write code separated into files like:

  /* event_flags.h */
  extern int event_flag;

  /* event_flags.c */
  int event_flag = 0;

  /* isrs.c */  
  void my_isr(void) 
  {
     event_flag = 1;
  }

however, a cleaner approach is:

  /* event_flags.h */
  void event_flag_set();
  void event_flag_clr();

  /* event_flags.c */
  static int event_flag = 0;
  void event_flag_set() {event_flag = 1;}
  void event_flag_clr() {event_flag = 0;}

  /* isrs.c */  
  void my_isr(void) 
  {
     event_flag_set();
  }

Think of this solution as writing a C++ class using C.
The static values needed between functions are like member
functions of a class, and the API presented to the user are
like C++ member functions.

Another good example is accessing peripherals, its better
to write an API for accessing the peripheral and then
use that in your code, rather than always accessing
the registers of the peripherals directly. It makes code
more easily portable. 

Dave

RE: [lpc2000] Re: Global Variables

2005-09-26 by David Hawkins

> Think of this solution as writing a C++ class using C.
> The static values needed between functions are like member
> functions of a class, 

sorry, that should read 'member variables'

> and the API presented to the user are
> like C++ member functions.
> 
> Another good example is accessing peripherals, its better
> to write an API for accessing the peripheral and then
> use that in your code, rather than always accessing
> the registers of the peripherals directly. It makes code
> more easily portable. 
> 
Dave

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.