Go to Post Every team in FIRST can make this choice. Be positive, help others, and work like a maniac behind the scenes. This approach will bring you joy and satisfaction, whether you win or not. - OZ_341 [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 08-02-2004, 15:15
mightywombat's Avatar
mightywombat mightywombat is offline
bins? stacking?? NOOOOO!
AKA: Bill Rosemurgy
#0857 (Superior Roboworks)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Houghton, MI
Posts: 93
mightywombat is on a distinguished road
volatile keyword

What does the volatile declaration mean?
ie (from kevin.org/frc timer demo)

volatile unsigned long msClock =0;
__________________
Bill Rosemurgy - Aspiring Programmer and Human Player (stack of 8 in <10 seconds...oh yeah... but I can't shoot for crap.)
www.team857.com
  #2   Spotlight this post!  
Unread 08-02-2004, 15:21
mightywombat's Avatar
mightywombat mightywombat is offline
bins? stacking?? NOOOOO!
AKA: Bill Rosemurgy
#0857 (Superior Roboworks)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Houghton, MI
Posts: 93
mightywombat is on a distinguished road
Re: volatile keyword

as an addition to my last post, what does the keyword static mean as well? as in:
static unsigned int divisor =0;
__________________
Bill Rosemurgy - Aspiring Programmer and Human Player (stack of 8 in <10 seconds...oh yeah... but I can't shoot for crap.)
www.team857.com
  #3   Spotlight this post!  
Unread 08-02-2004, 15:34
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: volatile keyword

Quote:
Originally Posted by mightywombat
What does the volatile declaration mean?
Volatile is a hint to the compiler to tell it that the memory location where the variable which was declared "volatile" is stored can change without the compiler knowing about it. This is significant, because a common optimization that a compiler will make with variables is that it will try to minimize the number of times a variable is copied out of RAM and into a register inside the CPU. Meaning if you use a variable named "blah" several times in a function and it's stored in the memory address 1234, the compiler will copy the value from memory location 1234 to a register at the beginning of the function, then use the register each time your code accesses that variable, and then just save the contents of the register back to memory address 1234 at the end of the function.

When you tell the compiler that a variable is "volatile" it means that it can be changed by something other than the code which the compiler is generating. So, when the compiler sees this, it does not cache the variable in a register and instead reads it directly out of RAM each time it is needed. Where this becomes important is on the digital I/O for example. The digital inputs are memory mapped (meaning to read the digital inputs, you just read a memory location). If these are not declared volatile, each time you access the digital inputs you may not be receiving the newest available data. So, if you go and look at ifi_picdefs.h, you'll see that all the CPU registers are declared volatile for this reason.

Hope that made sense, it's pretty tricky to explain in text without a whiteboard . Let me know if you want me to clarify any part of it...
  #4   Spotlight this post!  
Unread 08-02-2004, 15:37
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: volatile keyword

Quote:
Originally Posted by mightywombat
What does the volatile declaration mean?
ie (from kevin.org/frc timer demo)

volatile unsigned long msClock =0;
Volatile is an ANSI C storage qualifier that tells the compiler that a value in that storage location can not be trusted.

From the MPLAB® C18 C Compiler User’s Guide:

2.9.2.2 INTERRUPT SERVICE ROUTINES
...
Global variables that are accessed by both an ISR and mainline functions should be declared volatile.


What does this mean? Let's assume you are doing a simple math operation. One operand is in a register and the other is a memory location. The compiler will not use the memory location for a volatile variable. This becomes very important for a complex mathematical line such as:

x = x * m + b; // where x is a variable modifiable by an ISR.

In this example, if x is stored with x*m and the ISR modifies x before x+b occurs, you get the wrong answer... The volatile keyword prevents this from happening.

Regards,
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #5   Spotlight this post!  
Unread 08-02-2004, 15:38
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,856
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: volatile keyword

Quote:
Originally Posted by mightywombat
as an addition to my last post, what does the keyword static mean as well? as in:
static unsigned int divisor =0;
volitile tells the compiler not to assume the memory location of the variable retains it's value. It will check that the variable has the correct value whenever your code uses it.
static tells the compiler to always retain the variable's value.

You'll want to use static quite often.
In the following example, divisor will increase in value every time My_stuff() is called, but x will always start fresh as 0 each time and increase to 1, before being lost when you exit the routine:
Code:
void My_stuff()
{
  static unsigned int divisor =0;
  unsigned int x=0;

  x++;
  divisor++;
}
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
  #6   Spotlight this post!  
Unread 08-02-2004, 15:39
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
Re: volatile keyword

Quote:
Originally Posted by mightywombat
as an addition to my last post, what does the keyword static mean as well? as in:
static unsigned int divisor =0;

Static means that only one copy is used of the variable in all instances of the function. For example,

void foo(){
static int count = 0;
count++;
printf("%d", count);
}

int main(int argc, char *argv[]){
while(true)
foo();
}

would print out 123456789101112, etc whereas

void foo(){
int count = 0;
count++;
printf("%d", count);
}

would just print out 1 every time since the function can't keep track of things between calls.


As for volatile, it basically tells the compiler that it's possible that something other than the program itself could change the value of this variable, so don't try to do any crazy optimization stuff. Chances are that you won't really need it much (if ever), but it's good to know its there just in case.

-Rob
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php
  #7   Spotlight this post!  
Unread 09-02-2004, 10:54
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: volatile keyword

static can also be used outside a function. When used on a global variable, it is then only accessable to the functions in that file. It is impossible for a function out side the file to use it.
__________________

  #8   Spotlight this post!  
Unread 09-02-2004, 13:32
Random Dude Random Dude is offline
Oregon State Head FTA
AKA: Chris
no team (Oregon Robotics Tournament & Outreach Program)
 
Join Date: Aug 2002
Rookie Year: 1998
Location: Oregon
Posts: 142
Random Dude will become famous soon enoughRandom Dude will become famous soon enough
Re: volatile keyword

Quote:
Originally Posted by Texan
static can also be used outside a function. When used on a global variable, it is then only accessable to the functions in that file. It is impossible for a function out side the file to use it.

Actually, that is how normal "global" variables work. they have file scope. To increase the scope you need to use extern (see above).
  #9   Spotlight this post!  
Unread 09-02-2004, 15:46
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: volatile keyword

Quote:
Originally Posted by Random Dude
Actually, that is how normal "global" variables work. they have file scope. To increase the scope you need to use extern (see above).
static prevents the use of extern.
__________________

  #10   Spotlight this post!  
Unread 09-02-2004, 16:12
Random Dude Random Dude is offline
Oregon State Head FTA
AKA: Chris
no team (Oregon Robotics Tournament & Outreach Program)
 
Join Date: Aug 2002
Rookie Year: 1998
Location: Oregon
Posts: 142
Random Dude will become famous soon enoughRandom Dude will become famous soon enough
Re: volatile keyword

Quote:
Originally Posted by Texan
static prevents the use of extern.

Ahh, I did not know that. Thanks.
  #11   Spotlight this post!  
Unread 11-02-2004, 06:56
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: volatile keyword

Actually, here's what really happens. A normal golbal variable is accessible to any file, but in order to use it, the file has to be told it exists. Hence the extern. Static limits the scope to the file. If you know C++, an unnamed namespace does the same thing as static, when used on a global variable.
__________________

Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the extern keyword. D.Viddy Programming 7 20-01-2004 21:22


All times are GMT -5. The time now is 00:04.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi