HI-Tech C Compiler problém s sprintf() a reálným číslem

Ahoj,
opět jsem tu já a můj další problém. Při zobrazování měřeného napětí na lcd mi nejde zobrazit hodnotu v realném tvaru a proto jsem napsal vcelku jednoduchý program na zobrazení reálného čísla na lcd.

[code]#include <htc.h>
#include “lcd.h”
#include <stdio.h>
#define _XTAL_FREQ 8000000

void main(void)
{
TRISB = 0; // PORTB all outputs
TRISC = 0;
TRISA = 0b11111111;

float cislo;
char text[16];

lcd_init();

while(1)
{
	lcd_clear();
	lcd_goto(0);
	lcd_puts("It's working!!!");
	
	cislo = 12.7;
	sprintf(text, "Cislo: %2.5f", cislo);
	lcd_goto(64);
	lcd_puts(text);
	 __delay_ms(100);

}       

}
[/code]

Ovšem mi to vyhodí X chyb, viz výpis :

Build D:\BMAM\LCD-zkusebni\LCD-Zkusebni for device 16F690
Using driver C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe

Make: The target “D:\BMAM\LCD-zkusebni\lcd.p1” is up to date.
Make: The target “D:\BMAM\LCD-zkusebni\main.p1” is out of date.
Executing: “C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe” --pass1 D:\BMAM\LCD-zkusebni\main.c -q --chip=16F690 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist “–errformat=Error %n] %f; %l.%c %s” “–msgformat=Advisory%n] %s” “–warnformat=Warning %n] %f; %l.%c %s”
Executing: “C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe” -oLCD-Zkusebni.cof -mLCD-Zkusebni.map --summary=default --output=default lcd.p1 main.p1 --chip=16F690 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist “–errformat=Error %n] %f; %l.%c %s” “–msgformat=Advisory%n] %s” “–warnformat=Warning %n] %f; %l.%c %s”
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.83
Copyright © 2011 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Error [1347] ; 0. can’t find 0x76 words (0x76 withtotal) for psect “text536” in class “CODE” (largest unused contiguous range 0x44)
Error [1347] ; 0. can’t find 0x6E words (0x6e withtotal) for psect “text532” in class “CODE” (largest unused contiguous range 0x44)
Error [1347] ; 0. can’t find 0x6C words (0x6c withtotal) for psect “text533” in class “CODE” (largest unused contiguous range 0x44)
Error [1347] ; 0. can’t find 0x66 words (0x66 withtotal) for psect “text531” in class “CODE” (largest unused contiguous range 0x44)
Error [1347] ; 0. can’t find 0x5E words (0x5e withtotal) for psect “text535” in class “CODE” (largest unused contiguous range 0x44)
Error [1347] ; 0. can’t find 0x5A words (0x5a withtotal) for psect “maintext” in class “CODE” (largest unused contiguous range 0x44)
Error [1347] ; 0. can’t find 0x48 words (0x48 withtotal) for psect “text520” in class “CODE” (largest unused contiguous range 0x44)
Error [1347] ; 0. can’t find 0x25 words (0x25 withtotal) for psect “text525” in class “CODE” (largest unused contiguous range 0x14)
Error [1347] ; 0. can’t find 0x1D words (0x1d withtotal) for psect “text521” in class “CODE” (largest unused contiguous range 0x14)
Error [1347] ; 0. can’t find 0x1A words (0x1a withtotal) for psect “text530” in class “CODE” (largest unused contiguous range 0x14)
Error [1347] ; 0. can’t find 0x1A words (0x1a withtotal) for psect “text540” in class “CODE” (largest unused contiguous range 0x14)
Error [1347] ; 0. can’t find 0xC words (0xc withtotal) for psect “text534” in class “CODE” (largest unused contiguous range 0x3)
Error [1347] ; 0. can’t find 0xA words (0xa withtotal) for psect “text524” in class “CODE” (largest unused contiguous range 0x3)

********** Build failed! **********

Když tam dál celé číslo int a do sprintf dám %d, tak to funguje bez problému, ale zobrazit reálné čislo ne a ne.

Za připomínky děkuji.

Zdravím, podle mých zkušeností nejdou zobrazit real čísla u této funkce i v překladačích jinych firem. Používám vždy formát intiger a desetinou část převedu jednoduchou rutinou na další číslo. Více bych to neřešil.

Ano to jsem zkoušel

[code]int aaa;
int napeti_j, napeti_d;
double napeti;
char text1[16];
char text2[16];

void main(void)
{
TRISB = 0; // PORTB all outputs
TRISC = 0;
TRISA = 0b11111111;

lcd_init();
ADint();

while(1)
{
	aaa = prevod();
	napeti = (aaa * 4.79)/256;
	napeti_j = napeti;
	napeti_d = (napeti - napeti_j)*100;
	sprintf(text1,"Prevod: %d/256",aaa);
	sprintf(text2,"Napeti: %d.%d V",napeti_j,napeti_d);
	lcd_clear();
	lcd_goto(0);
	lcd_puts(text1);
	lcd_goto(64);
	lcd_puts(text2);
	__delay_ms(100);


}       

}[/code]

ovšem jsem narazil na problém, že když je hodnota dejme tomu 2,03V tak se zobrazí 2,3V a to je rozdíl celkem. Dá se to udělat

sprintf(text2,"Napeti: %d.%2d V",napeti_j,napeti_d);

ale zde dojde jenom k tomu, že se to posune o jedno doprava a je zde volné místo, kde by měla být nula.

Tak jsem to vyřešil tak, že když je desetiná část menší jak 10, tak to zapíše do toho volnýho místa 0. :slight_smile:

Staci z vaseho radku: “sprintf(text2,“Napeti: %d.%2d V”,napeti_j,napeti_d);”
Udelat: “sprintf(text2,“Napeti: %d.%02d V”,napeti_j,napeti_d);” Pridanim “0” dosahnete, spravneho vysledku. Vzdy budou zobrazeny 2 nuly. Doporucuji precist funkci “vfprintf()” - zde je vse popsano.

S pozdravem

:arrow_right: administrator: příspěvek byl upraven
Citace byla pozměněna.