View previous topic :: View next topic |
Author |
Message |
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
Problems with int32 |
Posted: Mon Oct 21, 2013 10:32 am |
|
|
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
|
|
Posted: Mon Oct 21, 2013 10:37 am |
|
|
What PIC and what compiler version ? |
|
|
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
|
Posted: Mon Oct 21, 2013 10:38 am |
|
|
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
|
|
Posted: Mon Oct 21, 2013 10:49 am |
|
|
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:
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
|
[SOLVED!] |
Posted: Mon Oct 21, 2013 10:58 am |
|
|
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. |
|
|
|