CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

#define constant problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

#define constant problem
PostPosted: Tue Jun 22, 2004 12:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 1:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 1:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 1:38 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group