View Single Post
  #4   Spotlight this post!  
Unread 22-02-2004, 22:03
Kevin Karan's Avatar
Kevin Karan Kevin Karan is offline
Linux Guy
AKA: maxdamage
None #0174 (Arctic Warriors)
Team Role: Alumni
 
Join Date: Jan 2003
Location: Liverpool, NY
Posts: 141
Kevin Karan will become famous soon enoughKevin Karan will become famous soon enough
Send a message via AIM to Kevin Karan
Re: Need help with Variables ?

Ok, this is how you declare global variables and do definitions... When you do a constant definition, always do it in the header file (always!) do it sepreate from all other variables.
You declare the reg variables at the top of the source file and set them to their starting state if you want. If you want them to be an external you declare them in the H file the same as in the C file, but you add extern in front and DONT set the starting val
ex

Code:
//blah.c
#include "blah.h"
// our global variables
unsigned int var1 = 3;
signed int var2 = -2;
double var3 = 3.5;

// our types
OMGtype origion;
OMGtype point1; 
OMGtype point2[4];

/*also you can have static vars that are not seen publicly. If you want 
 *you can make a sub that will return them to other source file function
 *thingys. You would delcare the function globaly but only delcare the
 *var inside the source C file. I wouldnet recomend this method but it
 *is an option if you want to have vars that share a name in different
 *source files.
 *********************/
double Left_Speed; //the static (non-public) var

double Get_Left_Speed(void) { return Left_Speed; }// and then the function

void playwithtypes(void)
{
 point1.blah = 3 //this is how we read/write to a structure
 point2[0].blah = 4 // this is how we read/write to a structured array
}
Code:
//blah.h
#ifndef _blah_h
#define _blah_h

// defines
#define STATE1 0
#define STATE2 1
#define STATE3 2
#define LEFTMOTOR pwm01
#define RIGHMOTOR pwm02

//declare vars
extern unsigned int var1;
extern unsigned signed int var2;
extern unsinged double var3;

//declare prototype functions
double Get_Left_Speed(void);
void playwithtypes(void);

/* also you can do types which are like variables inside variables
 * this is usefull if you have many different variables, all related in
 * groupes. Ex coordinates
typedef struct defOMGtype
{
    double blah; // for my blah variable
    double x; // for my x variable
    double y; // for my y variable
    double z; // for my z variable
} OMGtype

// now define externals that use the structure
extern OMGtype origion;
extern OMGtype point1;
extern rom OMGtype point2[4] = { // a structured array!
/*if you want to you can define the variables here and set it as a constant
 *in the rom
 *********/
//blah, x, y, z
{2,3,4,6}, //array element 1 [0] //alway start at zero!
{3,2,5,6}, //array element 2 [1]
{3,5,3,2}, //array element 3 [2]
{3,2,1,0}  //array element 4 [3]
}; //remember to end it

#endif
hope this helps
__________________
It isnt ALWAYS the programmers falt!
2004: Buckeye team website award
2002: Rutgers semi-finalists
2001: National semi-finalists
2001: Rutgers semi-finalists
1998: Manchester Rookie All-Star Award

Last edited by Kevin Karan : 23-02-2004 at 08:13.