Jump to content
Sign in to follow this  
mmckban

What Does This Mean?

Recommended Posts

Howdy,

 

I'm trying to port some code from HiTech and Boostc seems to choke on the following bit of code:

 

#define TXSTART (8192ul - (TX_BUFFER_SIZE + 8ul))

 

What do the "ul"s mean on the ends of the numbers? I've seen this several places now but no explanation in any reference I've found. I'm assuming it indicates unsigned long? Anyhow, does Boostc 6.70 support that and if not, what should I do to replace it?

 

Thanks,

Moses

Share this post


Link to post
Share on other sites

This tells the compiler to use unsigned long arithmetic when doing a calculation with these constants.

 

For example if you use PICC to do calculations like this:

 

#define FOSC1 (20000000)
#define TOSC (1000000000 / FOSC1)
#define PWM_MAXn (250)
#define TMR2_PRESCALEn (4)

#define PWM_PERIOD_usn ((TOSC * 4 * TMR2_PRESCALEn * PWM_MAXn) / 1000)

#define P_MIN ((850  * (PWM_MAXn * 4)) / PWM_PERIOD_usn)
#define P_MAX ((2000 * (PWM_MAXn * 4)) / PWM_PERIOD_usn)

volatile unsigned int ui_temp;
void test(void)
{

   ui_temp = P_MIN;
   ui_temp = P_MAX;
   ui_temp = ((P_MAX+P_MIN) >> 1);

}

P_MIN is incorrect, and ui_temp is assigned the value of : 44554

The correct value is : 4250

 

P_MAX is incorrect, and ui_temp is assigned the value of : 44406

The correct value is : 10000

 

As a result ((P_MAX+P_MIN) >> 1) is incorrect with the value : 44480'

The correct value is : 7125

 

Changing the literals to append "ul" corrects these errors.

 

#define FOSC1 (20000000ul)
#define TOSC (1000000000ul / FOSC1)
#define PWM_MAXn (250ul)
#define TMR2_PRESCALEn (4ul)

#define PWM_PERIOD_usn ((TOSC * 4ul * TMR2_PRESCALEn * PWM_MAXn) / 1000ul)

#define P_MIN ((850ul  * (PWM_MAXn * 4ul)) / PWM_PERIOD_usn)
#define P_MAX ((2000ul * (PWM_MAXn * 4ul)) / PWM_PERIOD_usn)

volatile unsigned int ui_temp;
void test(void)
{

   ui_temp = P_MIN;
   ui_temp = P_MAX;
   ui_temp = ((P_MAX+P_MIN) >> 1);

}

 

There are some kind of limits in how PICC does calculations with constants.

 

BoostC computes the correct values without the "ul" added to the constants. I do not think that you will have any issues by just deleting the "ul" from your constants.

Edited by cac001

Share this post


Link to post
Share on other sites

The other alternative is to use data type casting in its normal ANSI specified format.

This results in uglier and longer code, but is cross-compiler friendly.

Edited by emte

Share this post


Link to post
Share on other sites

I think you could just toss the ul and all would be well. All the math you are trying to do can be done with 16 bit math so there is no need to use 32-bit math hints. You can check the results by creating an unsigned int and assigning the value and looking at the code generated. It is not rocket science.

 

#define TX_BUFFER_SIZE  128
#define TXSTART (8192 - (TX_BUFFER_SIZE + 8))

main(){
   unsigned int What;  // 16-bit variable

   What = TXSTART;
}

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