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

printf and LCD

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



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

printf and LCD
PostPosted: Thu Apr 07, 2005 9:11 am     Reply with quote

I am not using LCD.C but I am doing something very similar...

The chip is 18F452.
Compiler is 3.217.

The problem is printf does not send a decimal point to lcd_putc so %03.1lu will only print 805� instead of 180.5�

%04.1lu will print 1805�

I know it must be something simple.....

Code:

void lcd_putc( char c) {
      switch (c) {
     case '\c'   : send_cmd(1);
                   delay_ms(2);
                   break;
     case '\t'   : send_cmd(0x80);  break; //home top line
     case '\b'   : send_cmd(0xc0);  break; //home second line
     default     : send_char(c);
      }
}


main(){
  position = 1805;
  printf(lcd_putc,"POSITION: %03.1lu%c",position,0xdf);
}

Ttelmah
Guest







PostPosted: Thu Apr 07, 2005 9:28 am     Reply with quote

'lu', signifies a long unsigned _integer_. Integers don't have a decimal point.....

Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Thu Apr 07, 2005 9:55 am     Reply with quote

The compiler is doing what you tell him:

position = 1805; // assign 1805 to the long int variable position

then convert it to printf

printf(lcd_putc,"POSITION: %03.1lu%c",position,0xdf);

To solve your problem and print 180.5 you must use a float
at expenses of memory use.

Change:

int16 position
by
float position;

position = 180.5;

printf(lcd_putc,"POSITION: %03.1f%c",position,0xdf);

Keep well,

Humberto
ljbeng



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

PostPosted: Thu Apr 07, 2005 10:22 am     Reply with quote

My mistake.

I assumed %03.1 would format any number just by inserting a decimal point where you ask. I will probably write a subroutine to print the way I want instead of using float.....
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Thu Apr 07, 2005 12:30 pm     Reply with quote

Quote:

I assumed %03.1 would format any number just by inserting a decimal point where you ask


Yes you can do that casting in this way:

printf(lcd_putc,"POSITION: %03.1f%c",(float)position,0xdf);

The number you are formating must have an arithmetic meaning, it is a
little more than accomodating a point.

If you are dealing with integers, the format 03.1 doesn�t have any sense.

Keep well,

Humberto
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 07, 2005 1:00 pm     Reply with quote

Just div by 10 and print the number
Print a "."
then print the modulus 10
Ttelmah
Guest







PostPosted: Thu Apr 07, 2005 3:04 pm     Reply with quote

ljbeng wrote:
My mistake.

I assumed %03.1 would format any number just by inserting a decimal point where you ask. I will probably write a subroutine to print the way I want instead of using float.....

You can use ldiv.
So:
Code:

int16 val;
ldiv_t result;
result=ldiv(val,10);
printf("%02lu.%1lu",result.quot,result.rem);



Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 07, 2005 5:09 pm     Reply with quote

Ttelmah wrote:
ljbeng wrote:
My mistake.

I assumed %03.1 would format any number just by inserting a decimal point where you ask. I will probably write a subroutine to print the way I want instead of using float.....

You can use ldiv.
So:
Code:

int16 val;
ldiv_t result;
result=ldiv(val,10);
printf("%02lu.%1lu",result.quot,result.rem);



Best Wishes


Today I learned somethin Very Happy
Kieran



Joined: 28 Nov 2003
Posts: 39
Location: Essex UK

View user's profile Send private message

PostPosted: Fri Apr 08, 2005 5:42 am     Reply with quote

Quote:

Code:


int16 val;
ldiv_t result;
result=ldiv(val,10);
printf("%02lu.%1lu",result.quot,result.rem);




Is ldiv a built in function? I can't find it (Vn 3.219).
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Apr 08, 2005 5:54 am     Reply with quote

Look in the current CCS manual page 93
ljbeng



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

PostPosted: Fri Apr 08, 2005 8:03 am     Reply with quote

Mark,

Version 3.217 gives me an error when I use your example.

Code:
int16 val;
ldiv_t result;
result=ldiv(val,10);
printf("%02lu.%1lu",result.quot,result.rem);


It stops at the "rem" on the printf line and says "printf format type is invalid". If I change "lu" to "u" it compiles but does not print the correct result if quot > 255. Otherwise, it works, 1805 was printed 180.5
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 08, 2005 8:11 am     Reply with quote

Not my example but after reading the manual:

Code:

signed int16 val;
ldiv_t result;
result=ldiv(val,10);
printf("%02ld.%1ld",result.quot,result.rem);
Ttelmah
Guest







PostPosted: Fri Apr 08, 2005 8:25 am     Reply with quote

Sorry about putting 'unsigned' in the printf.
It is in the manual under 'div', rather than 'ldiv' The first is for short integers only, while the latter handles 'longs'.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Apr 08, 2005 10:20 am     Reply with quote

I often have programs that require decimal points in the calculations but I like to use integers instead of floats. When it comes time to format the output for a display I will use a code like this:

Code:

int16 position = 1805;
char[10] text;// temporary text variable


sprintf(text, "%lu", position);// convert integer to a string
text[5] = text[4];// move string terminator
text[4] = text[3];// move characters to insert a decimal
text[3] = text[2];
text[2] = text[1];
text[1] = 0x2E;// decimal point

printf(lcd_putc,"POSITION: %S%c",position,0xdf);


This will convert the integer to a string that can then be shifted around to insert the decimal to wherever you need it. If the integer value will vary greatly then I will use an if() statement to determine just how many numbers will need to be shifted. You could use strlen() as well. Floats use up a lot of ram and decimals, quite often, only need to be used for formatting an output for somebody to read. The internal code can be written to account for the larger number (1805 vs. 180.5).

Ronald
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