Jump to content
hollie

Volatile Declaration Syntax

Recommended Posts

Hello,

 

I have a question concerning the declaration of a volatile.

 

I'm writing an LCD lib that would be very flexible in terms of pin location on the PIC (i.e. the data pins do not have to be connected to the upper or lower nibble of a port).

 

For this, the volatiles are very handy.

 

What I would like to do is to let the user of the code declare the port and the pin number of a certain connectio pin, e.g.

 

#define LCD_DATA4_PORT  PORTB
#define LCD_DATA4_PIN  0

 

The 'internal' part of the code then works like:

 

volatile bit lcd_d4 	 @ LCD_DATA4_PORT.LCD_DATA4_PIN;

Now what I would like to be able to write also is:

 

volatile bit lcd_tris_d4  @ LCD_DATA4_PORT + 0x80 . LCD_DATA4_PIN;

So that the user doens't need to bother about declaring the TRIS register separately.

 

However, it seems like doing

 

<something> @ number + number . pin;

is not supported. I think the compiler gets stuck on the addition in the address.

 

Is this intended behaviour or should I post a bug report for this?

 

Kind regards,

Hollie.

Share this post


Link to post
Share on other sites
<something> @ number + number . pin;

is not supported. I think the compiler gets stuck on the addition in the address.

 

Is this intended behaviour or should I post a bug report for this?

 

I have seen this and it appears to be a minor bug.

 

However, on a different note I have a some suggestions for you:

 

The idea of the offset to automatically map between the port register and tris looks appealing at first but the problem is you are then writing processor specific code. The offset between the port registers and the tris registers on a 16F8xx are different to those on an 18Fxxx. There is no real saving as a result.

 

Secondly, you may want to have a look at the use of "templates". Support for these was added in BoostC1.7. Take a look at the i2c_driver.h, serial_driver.h and the lcd_driver.h files in the include directory of the BoostC1.8 installation.

 

You might want to consider modifying the lcd_driver.h file to meet your needs. The sample driver assumes that the three control lines are on a common port so you would need to change this if your control lines are scattered across different ports. I know this driver works because I ported ot to my hardware yesterday without too much of a problem.

Share this post


Link to post
Share on other sites

Hollie,

 

Hello,

 

I have a question concerning the declaration of a volatile.

 

I'm writing an LCD lib that would be very flexible in terms of pin location on the PIC (i.e. the data pins do not have to be connected to the upper or lower nibble of a port).

 

For this, the volatiles are very handy.

 

From what you say here it looks like you don't understand the volatile keyword, actually I think only a few people do.

 

When a variable is declared as volatile it tells the compiler that something external to the current code can change the variables value, or is reading the variables value. This means the compiler may need to generate different code for these variables compared to variables that are declared as non volatile.

 

When using the PIC all the hardware peripherals that have special function registers and change them or read them. General Purpose registers are only changed by your program execution, and hence are normally not declared as volatile.

 

Without volatile the compiler could cache the value of a register and never read the actual register again. With a volatile declaration the compiler know that it must read or write the register every time it is reference.

 

The compiler also is more careful what it does with voltaile declared variables:

Example 1 non volatile declared bit:
   bsf  PORTB, 0
   btfss turnItOn, 0
   bcf  PORTB, 0

Example 2 volatile declared bit:
   btfss turnItOn, 0
   goto lab1
   bsf PORTB, 0
   goto lab2
lab1:
   bcf PORTB, 0
lab2

 

Both example 1 and 2 turn on RB0 when turnItOn bit 0 is set.

 

Q) Whats the difference then ?

A) The first example momentarily turns off RB0 before turning it on, creating a blip on the actual port. The second example either turn RB0 zero on or off, it never does both in a single pass.

 

Volatile is quite tricky to understand.

 

Hope this help.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hello,

 

@asmallri: thank you for your suggestion. I know that I am writing processor specific code. However, considering the we have quite some LCD-related things going on and that even the data pins are not always connected to one single port, I thought it would be handy to have the tris-registers auto-calculated. It reduces the risk of introducing errors for our specific implementation.

 

@dave: thanks for the clarification. I understand the concept of the volatile keyword, and I indeed incorrectly used it for the definition of the TRIS bits. However, even without the volatile keyword, the language construct

 #define PORTB 0x06
bit tris_bit @ PORTB + 0x80 . 0

fails. Could this be a handy feature to include in the compiler?

 

Kind regards,

Hollie.

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...

×
×
  • Create New...