Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC > LCD > Displaying string and int value

Status
Not open for further replies.

kawauso

New Member
I would like to know how to write :
1) a message like « Bonjour ! Ça va ? »
2) the value of a variable after measuring,
3) the combination of both 1) & 2) like 3,47 volts.
I made some research on the web. I do not want to use only in MikroC the Conversion Library of Mikroelektronika and the void IntToString(void) without being able to watch the code inside and understanding the conversion process.
Some body has a idea or the link to an open source ?
Thanks at all !
Merci à tous.
Eric
 
Here's mine. No comments .. takes an array, a value and a decimal requirement then stores as fixed point number ie.. 900 with 1 decimal = 90.0
It does what I want it to... modify as you see fit..

C:
void printFloat(char * flt, long number, char digits)
    {
    if(number < 0)
        {
        number = abs(number);
        }
    if(digits == 2)
        {
        flt[0] = number / 1000 + 48;
        if(flt[0] == 48) flt[0] = 0x20;
        flt[1] = ((number % 1000) / 100)+ 48;
        flt[2] = 46;
        flt[3] = ((number % 100) / 10)+ 48;
        flt[4] = (number % 10) + 48;
        flt[5] = 0;
        }
    else
        {
        flt[0] = number / 100 + 48;
        if(flt[0] == 48) flt[0] = 0x20;
        flt[1] = ((number % 100) / 10)+ 48;
        flt[2] = 46;
        flt[3] = (number % 10) + 48;
        flt[4] = 0;
        }
    }
 
Just wondering .. does sprintf work ok micros? I could have sworn I used it before...

NameVariable = Jason
floatDegrees = 78.8
sprintf(buffer, "Hello, %s , it is %f degrees F.", NameVariable,FloatDegrees);

Something like that... Would print

Hello Jason, it is 78.8 degrees F.

Or am I forgetting something :)
 
Just wondering .. does sprintf work ok micros? I could have sworn I used it before...

NameVariable = Jason
floatDegrees = 78.8
sprintf(buffer, "Hello, %s , it is %f degrees F.", NameVariable,FloatDegrees);

Something like that... Would print

Hello Jason, it is 78.8 degrees F.

Or am I forgetting something :)

Of course it works, as long as you have a routine to actually do the printing - which you need to write yourself for whatever 'display' you're using.

However, what you're forgetting is the huge impact of sprintf which takes a LOT of memory space, and runs pretty slowly - it's far more efficient to write a specific routine to do just what you want.
 
I hate to admit it, but, it's so much easier in Arduino. I added an "LCD" class for my old 2-pin 8-bit LCD Backpack a couple years ago and it was relatively easy to include the Arduino "Print" class for all those neat formatting capabilities... I posted the demo' here on ETO...

Arduino LCD + Print Class demo
 
I hate to admit it, but, it's so much easier in Arduino. I added an "LCD" class for my old 2-pin 8-bit LCD Backpack a couple years ago and it was relatively easy to include the Arduino "Print" class for all those neat formatting capabilities... I posted the demo' here on ETO...

Arduino LCD + Print Class demo

Isn't it just a wrapper around sprintf anyway?, and just as resource hungry.

And of course you have the added advantage of C++ rather than plain C.
 
Just wondering .. does sprintf work ok micros? I could have sworn I used it before...

NameVariable = Jason
floatDegrees = 78.8
sprintf(buffer, "Hello, %s , it is %f degrees F.", NameVariable,FloatDegrees);

Something like that... Would print

Hello Jason, it is 78.8 degrees F.

Or am I forgetting something :)

I raid on Mikroelektronika Compiler > Help, that sprintf fit only with PIC 18F. For 16F I should use sprintl ...
 
Here's mine. No comments .. takes an array, a value and a decimal requirement then stores as fixed point number ie.. 900 with 1 decimal = 90.0
It does what I want it to... modify as you see fit..

C:
void printFloat(char * flt, long number, char digits)
    {
    if(number < 0)
        {
        number = abs(number);
        }
    if(digits == 2)
        {
        flt[0] = number / 1000 + 48;
        if(flt[0] == 48) flt[0] = 0x20;
        flt[1] = ((number % 1000) / 100)+ 48;
        flt[2] = 46;
        flt[3] = ((number % 100) / 10)+ 48;
        flt[4] = (number % 10) + 48;
        flt[5] = 0;
        }
    else
        {
        flt[0] = number / 100 + 48;
        if(flt[0] == 48) flt[0] = 0x20;
        flt[1] = ((number % 100) / 10)+ 48;
        flt[2] = 46;
        flt[3] = (number % 10) + 48;
        flt[4] = 0;
        }
    }
Dear Ian,
Sorry for my late reply. I tried ypur routine in my code. I probably missed some thing, because in line 2 of LCD, LCD doesn't displays the decounting of microsecond (just for testing the code, rapidely and in a wide situation).
Do you have an idea.
Compiled with mikroC from mikroelektronika
Eric
 

Attachments

  • LCD_v4.c
    16.9 KB · Views: 215
Probably because you have the parameters the wrong way round "print_float_v4(sec, txt, 8);"

Should be txt first AND the txt is a pointer so you need to include the ampersand..
Also the last parameter is either 1 or 2... ( decimal places )

The other issue is the negative... You'll need to increase the txt parameter to include a space or a - .. for negative numbers

print_float_v4(&txt[0] sec, 8); //<< the ampersand passes the LOCATION of the first char of the array..
 
Probably because you have the parameters the wrong way round "print_float_v4(sec, txt, 8);"

Should be txt first AND the txt is a pointer so you need to include the ampersand..
Also the last parameter is either 1 or 2... ( decimal places )

The other issue is the negative... You'll need to increase the txt parameter to include a space or a - .. for negative numbers

print_float_v4(&txt[0] sec, 8); //<< the ampersand passes the LOCATION of the first char of the array..
Bonsoir Ian,

Thank you to enlighten me. I m trying to switch with your instruction ... it’s almost ok ... I will try some changes by my own, maybe will come back.
About the line with abs ... I would not use any fonction of the library, how could I change this line ? number = - number doesn’t work ...

Merci !

Éric
 
Bonjour Ian,
In order to count for(sec = -8388607; sec <= 8388607; sec = sec + 0.5), I fitted your routine to my needs as follow here under ...
But ... ‘-‘ minus never displays ...
And what is the aim of lines :
if (*(flt + 2) == 48) *(flt + 2) = 32; // 32 = space

Code :

void print_float_v7(char *flt, long number, char digits)
{
if (number < 0)
{
number = - number;
*(flt) = 45; // 45 = '-'
*(flt + 1) = 32; // 32 = space
}

if (number >= 0)
{
*(flt) = 32; // 32 = space
*(flt + 1) = 32; // 32 = space
}

if (digits == 2)
{
*(flt + 2) = number / 1000000 + 48;
//if (*(flt + 2) == 48) *(flt + 2) = 32; // 32 = space
*(flt + 3) = ((number % 1000000) / 100000) + 48;
*(flt + 4) = ((number % 100000) / 10000) + 48;
*(flt + 5) = ((number % 10000) / 1000) + 48;
*(flt + 6) = ((number % 1000) / 100) + 48;
*(flt + 7) = ((number % 100) / 10) + 48;
*(flt + 8) = ((number % 10) / 1) + 48;
*(flt + 9) = 44; // 44 = ','
*(flt + 10) = ((number % 100) / 10) + 48;
*(flt + 11) = (number % 10) + 48;
*(flt + 12) = 0;
}
else
{
*(flt + 2) = number / 100000 + 48;
//if (*(flt + 2) == 48) *(flt + 2) = 32; // 32 = space
*(flt + 3) = ((number % 100000) / 10000) + 48;
*(flt + 4) = ((number % 10000) / 1000) + 48;
*(flt + 5) = ((number % 1000) / 100) + 48;
*(flt + 6) = ((number % 100) / 10) + 48;
*(flt + 7) = 44; // 44 = ','
*(flt + 8) = (number % 10) + 48;
*(flt + 9) = 0;
}
}

Thanks for your help.

Merci.

Eric
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top