View previous topic :: View next topic |
Author |
Message |
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
#define constant problem |
Posted: Tue Jun 22, 2004 12:55 am |
|
|
PCM version 3.188.
The following cutdown program illustrates the problem.
With
temp32 = temp32/RANGE;
In the list file it seems that the compiler is trying to have the #define calculation done in the code (and getting it wrong) rather than calculate the value and place it in the code.
If I make a change to
#define RANGE 102
the problem disappears.
Is there is something wrong with the way that I have done the #define?
Any suggestions would be welcomed.
Thanks
Ken
Code: |
#include <16f876.h>
#fuses HS,NOWDT,PUT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#define SF 10
#define RANGE (1023L + (SF/2))/SF // With SF=10, RANGE=102
int32 lword = 10220;
void main(void)
{
int32 temp32;
temp32 = lword;
temp32 = temp32/RANGE;
printf("%3lu ",temp32); // Incorrect (gives 0)
temp32 = lword;
temp32 /= RANGE;
printf("%3lu\n\r",temp32); // Correct (gives 100 as expected)
while(1);
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jun 22, 2004 1:04 am |
|
|
I have noticed this same behaviour. It looks like a missing feature for the pre-processor.
Hopefully someone can give some hints/tips? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 22, 2004 1:26 am |
|
|
CCS needs to be led by the hand when it comes to type promotions.
I suggest you do something like this: (Changes are in bold)
#define SF 10L
#define RANGE (int32)((1023L + (SF/2))/SF)
I'm not at a place where I can test this on hardware, but this
is the first thought that comes to mind. |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Tue Jun 22, 2004 1:38 am |
|
|
That fixed it. The list file shows that the same code is generated for both cases.
I had tried the 10L but not the (int32).
Thanks PCM.
Ken |
|
|
|