Jump to content
Sign in to follow this  
Maister

What Is The Difference?

Recommended Posts

Hello!

 

Could anybody explain me what is the diffrence between this:

volatile bit bPin0 @ PORTA.0

or

bit bPin0 @ PORTA.0

 

Now I have got some questions ;)

 

In wheech file I have to save some function, if I want to use that function in new program code.

How I call that function in my new program?

 

I hope you understand my questions?

 

Thanks for the answers!

Share this post


Link to post
Share on other sites

Maister,

 

volatile mean that the data is linked to something external.

The compiler needs to know this to prevent side affects that can occur when the shortest possible code is used.

 

conisder:

 

bit a = 1;

bit b;

b = a;

 

// solution 1 - shortest assembly

BCF b

BTFSC a

BSF b

 

// solution 2 - no blip on b assembly

BTFSS a

BCF b

BTFSC a

BSF b

 

Solution 1 for a moment clears b, before either setting if require.

This is fine unless this bit happens to be controlling the reset on a counter (for example). In which case causes the counter to reset when you don't expect it.

 

Also when reading volatile data, ensures that it is actually read. A compiler could potentially cache a value read, say in the w register then while( done_bit ); could potentially never complete.

 

Hope that helps - volatile is a slightly tricky subject.

 

 

Regards

Dave

Share this post


Link to post
Share on other sites

Maister,

 

In wheech file I have to save some function, if I want to use that function in new program code.

How I call that function in my new program?

 

The simplest thing to do is create a header file (.h) and source file (.c) for the code you want to be common - say myfuncs.h and myfunc.c

 

Save these files in a common place (not in the same folder as any project), say "myfuncs"

 

Assuming the myfunc folder is at the same directory level as a project you are working on, when you want add this code to a project:

1) Add the myfuncs.c to the project (right click on the project and use add)

2) Use #include "..\myfunc\myfunc.h" in each source file that refers to any of thses functions.

 

Hope this helps.

 

BTW: Alternatively you could make a library out of the code, its not hard to do this either, but I won't describe all these details here or things will get just too confusing.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hello!

 

Thank You Dave for the answers.

My problem is, that I don't know what I must write in header file.

Please, give me one simple example ;)

Could I write just .c function and than call this function in my source code?

Why do we need header file?

I'm sorry for this school questions,but I'm new at that :rolleyes:

 

Thanks for the answers!

Share this post


Link to post
Share on other sites

Maister,

 

The .c file contains the function definitions:

 

int add( int a, int b )

{

return a + b;

}

 

The .h file contains function prototypes, that tells the compiler the function arguments and its return type:

 

int add( int a, int b );

 

 

Hope that brief guide helps.

 

Regards

Dave

Share this post


Link to post
Share on other sites
Guest Joe

Also note that global control variables that are used in an interrupt routine need to be volitile....

 

Basicly, volatile variables will not be optimised, and will remain in memory un-aultered.

 

"Ahh", you say, "but of course they will always be un-aultered".

 

..Erm, no. Read on..

 

Compilers commonly optimise variables that do not have scope in the current execution path, for example, if a global variable is not used in a function, then when that function is executing, the global variable may be optimised.

 

By optimised, i mean that the compiler may move the variable and re-use the memory location that it occupied, then when it has finished its current opperation, the variable will be restored.

 

So, if we have a global variable that is NOT volatile, and we execute a function that does not use our global var, the compiler may decide to use the memory that our global was using for somthing else, then put it back when its finished. But if an interrupt occurs when this has happened, and the interrupt service routine tries to read that global variable, it may not read the (correct) variable value. ;)

 

Computing with volatile variables will (most likely) require more RAM and more ROM, so DO NOT make every global variable volatile!!!

 

 

- I hate trying to explain what volatile means/does, and i think the more people explain it in different ways, the better the overall understanding will be!

 

 

 

Joe

Share this post


Link to post
Share on other sites

Joe,

 

By optimised, i mean that the compiler may move the variable and re-use the memory location that it occupied, then when it has finished its current opperation, the variable will be restored.

 

The reuse of global var memory is very unlikely, where is the variables value retained in the mean time ???

 

The much more likely senario is that the global variable is cached (in a register for example), if this variable is changed by an interrupt service routine, the the cached value will not be changed. This caching is disabled when the variable is declared are volatile, you are advising the compiler that the variable is changed by something external.

 

Using the volatile type modifier means that the compiler won't take the shortest path to get a result, so it will result in more ROM and maybe RAM usage.

 

Regards

Dave

Share this post


Link to post
Share on other sites
Guest Joe
The reuse of global var memory is very unlikely, where is the variables value retained in the mean time ???

 

On a PIC device, this optimisation would be pointless as there is no-where the global memory can be copied to, but i was explaining the general principle of volatile where this is also possible.

 

Win32 compiler use a process where the global memory (in ram) could be saved to virtual memory (hard drive) to free the ram for another process, as is done quite frequently by CAD packages and other memory-hungry software!

 

 

 

 

 

 

Joe

Share this post


Link to post
Share on other sites

Joe,

 

In the old days you would have to worry about memory in windows, these days in windows that memory management is transparent, you neither know or care when swapping out to disk occurs (thank god!).

 

Regards

Dave

Share this post


Link to post
Share on other sites
Guest Joe

Indeed - VM really was a pain! :rolleyes:

 

 

Maybe a better example would have been allocating a large block of continously alligned ram, hence causing some global vars to be shifted around in ram so it would fit?

 

Hey, would SB do this if a large block was dynamicly allocated in c++ ?

 

 

In any case, i think the "what is a volatile" has been explained, hopefully! ;)

Share this post


Link to post
Share on other sites

Joe,

 

BoostC will have (now has - in the next release for you) dynamic memory allocation ;)

 

But as you can imagine for a tiny target the algorithms have to be kept simple and efficient.

 

So it allocates first block found big enough and compact and try again if that fails.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...