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

PRINTF an int32 with decimal points (faking a float value)

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



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PRINTF an int32 with decimal points (faking a float value)
PostPosted: Sun Feb 07, 2010 11:07 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 08, 2010 12:49 am     Reply with quote

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:
Quote:

9999.99

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

View user's profile Send private message

PostPosted: Mon Feb 08, 2010 4:39 pm     Reply with quote

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 Smile

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

View user's profile Send private message

PostPosted: Mon Feb 08, 2010 5:06 pm     Reply with quote

Quote:

I can't test with anything else.

You can test other PICs in the MPLAB Simulator, using the "UART1"
feature (of MPLAB) to display text in the Output Window:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Feb 08, 2010 7:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 09, 2010 2:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 10, 2010 7:33 pm     Reply with quote

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 Smile

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!
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