Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC C18 sprintf for floating point number

Status
Not open for further replies.

Rokas

Newbie level 4
Joined
May 28, 2011
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Coventry, UK
Activity points
1,341
Hello,
while looking for an easy way to display floating point number on a LCD I've came across lots of threads asking for this.
But none of them gave a good enough solution. So I've decided to combine them.
Yes yes, maybe this is already known for most of you, but maybe beginners will find this useful.

This is for PIC MCUs, c18 compiler and XLCD library.

#include <stdio.h>
#include <XLCD.h>
... ... ...
float displ;
char buff[n];

sprintf(buff, (const far rom char*) "%02d.%02d", (int)displ, (int)((displ-(int)(displ))*1000));
putsXLCD(buff);


displ - is a floating point number which you want to display.

buff[n] - array of n elements for storing converted strings.

(const far rom char*) - this is the declaration for formating taken from stdio.h library. Not including this part causes a warning in the compiling process.

"%02d.%03d" - formating your output. %02d - format for decimal part (2 numbers, d stands for decimal). "Dot" ( . ) between %02d and %03d is a real symbol which you will see on your LCD. And finally %03d - format for decimals. (3 numbers, d - decimal). Refer to https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output/

(int)displ - for displaying an integer part. (int) takes integer part of variable displ.

(int)((displ-(int)(displ))*1000) - this is a function which converts displ's decimals part into integer type (which later will be displayed as decimals on your LCD). Multiplier "1000" defines how many decimal places you want to use, in this case it will be 3 decimal places.

Don't forget to change array's size (number of elements - n) according to your number length (including "Dot").

And finally putsXLCD(buff) displays your number on your LCD.

Cheers,
Rocky
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top