|
|
View previous topic :: View next topic |
Author |
Message |
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
PRINTF an int32 with decimal points (faking a float value) |
Posted: Sun Feb 07, 2010 11:07 pm |
|
|
Hi,
Is it possible to have %w formatting but with an int32 integer?
Example value of 999999 to be displayed like %4.2w (9999.99).
Passing an int32 with %w give me garbage prints on my lcd.
Have a nice day! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 08, 2010 12:49 am |
|
|
You didn't specify your PIC or your version. So, I ran the following test
program with vs. 4.104 and a 14F452. MPLAB simulator gives me the
following result:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main(void)
{
int32 value;
value = 999999;
printf("%4.2w", value);
while(1);
}
|
|
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Mon Feb 08, 2010 4:39 pm |
|
|
PCWHD 1.104 with dspic30f4013.
Your code return me a lot of garbage chars.
In fact I've tested with an uint8 same thing! (I don't know if it's a problem with PCD, i can't test with a anything else i'm out of PIC except dspics)
What could be the other alternative for inserting a dot when displaying value.
It's just a matter of putting the decimal at the right place.
I think if it's possible to read the int32 and on the last field insert a dot in between (from 99999 example value) this would result in 9999.9 being displayed
Thank you very much for you help
EDIT: i thought dividing 10 the value but this would require to have a float variable which i would like to avoid if possible ? _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Mon Feb 08, 2010 7:04 pm |
|
|
Hi,
Your code run well in MPLAB SIM
but this doesn't:
Code: |
#include <30f4013.h>
#FUSES FRC_PLL16,PR_PLL,NOWDT,NOPUT,NOMCLR,NOBROWNOUT
#use delay(clock=117920000)
#use rs232(baud=9600, UART1, ERRORS)
void main(void)
{
int32 value;
value = 999999;
printf("%4.2w", value);
while(1);
}
|
it crash during execution (but compiling is successful) and SIM UART1 Tab is empty no output from serial
hmmm i don't have any clue why it doesn't work even with simulation
thanks! _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 09, 2010 2:24 am |
|
|
You could do a work-around for the %w format, by using using division
and the modulo operator to calculate the quotient and the remainder.
Then just display the quotient and the remainder in one printf statement,
with a '.' between the format specifiers for each one.
But first, you need to see how the PCD compiler handles the sign of
the result of the modulo (%) operator. In PCH, the result is always
positive. But CCS may follow a different standard (C99 ?) in PCD.
Change the program below for the 30f4013 and see what you get
for the remainder. Is it a negative or a positive number ?
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main(void)
{
signed int32 dividend;
signed int32 remainder;
signed int32 quotient;
//dividend = 999999;
dividend = -123456;
quotient = dividend / 100;
remainder = dividend % 100;
printf("quotient = %ld\r", quotient);
printf("remainder = %ld\r", remainder);
while(1);
}
|
|
|
|
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
|
Posted: Wed Feb 10, 2010 7:33 pm |
|
|
Dear PCM programmer,
Sorry for the late reply, here's the output of your code from the dspic.
Quote: |
quotient = -1234
remainder = 56
|
Nice quick fix
I know it's not the best way (I've never use so much dirty workaround in programming ) to do it but it works !
If only PCD was less buggy and more mature like his brothers PCB,PCH and PCM
Best wishes _________________ Regards,
Laurent
-----------
Here's my first visual theme for the CCS C Compiler. Enjoy! |
|
|
|
|
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
|