Strona 1 z 1

wyświetlanie liczb zmiennoprzecinkowych na LCD

: poniedziałek 06 mar 2017, 18:31
autor: robo1973
Cześć !!!
Chcę wyświetlić liczbę zmiennoprzecinkową na wyświetlaczu LCD /hd44780.h/
Napisałem taką funkcję ale mi to nie działa Wyświetla zamiast 12.345 >> 16357
proszę o pomoc i pozdrawiam !!

Kod: Zaznacz cały

void lcd_float(float val)
       {
          char bufor[17];     //bufor na dane
         
          sprintf(bufor,"%.1f",val);  //float na string - 4 miejsca po przecinku
       
         LcdClear();
           LcdDec(bufor);   //wyświetlasz string z liczbą np. "12,345"
         
       }



Działa Trzeba było LcdWrite(bufor);

Re: wyświetlanie liczb zmiennoprzecinkowych na LCD

: poniedziałek 06 mar 2017, 23:13
autor: Antystatyczny
Nie wiem, czy najszczęśliwszym rozwiązaniem jest użycie funkcji z rodziny printf. W module stdlib.h znajdziesz funkcję konwertującą double (oraz float) do postaci tekstowej. Pozwoliłem sobie wyłuskać z nagłówka tego modułu krótki opis oraz deklarację tejże funkcji:

Kod: Zaznacz cały

extern char *dtostre(double __val, char *__s, unsigned char __prec,
           unsigned char __flags);

/**
   \ingroup avr_stdlib
   The dtostrf() function converts the double value passed in \c val into
   an ASCII representationthat will be stored under \c s.  The caller
   is responsible for providing sufficient storage in \c s.

   Conversion is done in the format \c "[-]d.ddd".  The minimum field
   width of the output string (including the \c '.' and the possible
   sign for negative values) is given in \c width, and \c prec determines
   the number of digits after the decimal sign. \c width is signed value,
   negative for left adjustment.

   The dtostrf() function returns the pointer to the converted string \c s.
*/
extern char *dtostrf(double __val, signed char __width,
                     unsigned char __prec, char *__s);
                     
                     
                   


Przetestuj oraz porównaj rozmiary kodów wynikowych dla Twojej wersji funkcji z wykorzystaniem sprintf oraz dla funkcji z wykorzystaniem dtostrf.

Pozdrawiam.

Re: wyświetlanie liczb zmiennoprzecinkowych na LCD

: wtorek 07 mar 2017, 21:09
autor: robo1973
ok dzięki protestu