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 CCS Technical Support

Problems with int32

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



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

Problems with int32
PostPosted: Mon Oct 21, 2013 10:32 am     Reply with quote

Code:

   calc = (int32)((int32)250ul * (int32)millis);
   #ifdef DEBUG
      fprintf(pc, "calc: %Lu", calc);
   #endif   


I'm trying to use a 32 bit value during calculations. It seems to be giving me odd results so I put some debugging output in there. The above code returns 5200 as the value of calc if I pass in a value of 2000 for millis. This is obviously wrong, the proper answer is 500,000. I consulted the documentation and it seems like the format string I used is the correct one but it still shows 52000. That value makes no sense at all. What am I doing wrong?

EDIT: And, yes, calc is defined unsigned int32.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 21, 2013 10:37 am     Reply with quote

What PIC and what compiler version ?
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Mon Oct 21, 2013 10:38 am     Reply with quote

Oh, sorry, yes I suppose that would have been useful info.

PIC18F25J11 is the processor and 4.129 as the compiler version.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 21, 2013 10:49 am     Reply with quote

It works for me. I installed vs. 4.129 and compiled it in MPLAB vs. 8.91
and ran it in MPLAB simulator and I got this result in the Output Window:
Quote:
calc: 500000

You didn't provide a test program so I made this one:
Code:
#include <18F25J11.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS, stream=pc)

#define DEBUG TRUE

//==============================================
void main()
{
int16 millis;
int32 calc;

millis = 2000;

calc = (int32)((int32)250ul * (int32)millis);

#ifdef DEBUG
      fprintf(pc, "calc: %Lu", calc);
#endif   

while(1);
}


Code:

CCS PCH C Compiler, Version 4.129, xxxxx        21-Oct-13 09:47

               Filename: C:\Program Files\PICC\Projects\PCH_Test\PCH_Test.lst
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

[SOLVED!]
PostPosted: Mon Oct 21, 2013 10:58 am     Reply with quote

Man, my transition back to PIC programming has been off to a rocky restart. Two dumb errors in two days... This one was caused by accidentally defining millis as an int. I'm use to 32bit MCU programming where int is suitably large. On this architecture it is 8 bits... Yeah, that might explain things. So, thanks for testing and creating an example. It jogged my memory.
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