| Author |
Message |
fatihbasaris
Joined: 01 Oct 2004 Posts: 6
|
29 Apr 2005 9:33 float to char |
|
|
|
|
How can I convert a float to char?
#define sil 1
#define home 2
#define solayaz 4
#define sagayaz 6
#define kursorgizle 12
#define kursoryanson 15
#define kursorgeri 16
#define kaydirsaga 24
#define kaydirsola 28
#define displaykapat 0
#define birincisatir 128
#define ikincisatir 192
#define karakuretadres 64
#define ciftsatir8bit 56
#define ciftsatir4bit 48
#define teksatir8bit 40
#define teksatir4bit 32
#define DATA P2
#define RS P3_0
#define RW P3_1
#define EN P3_2
#define clock P3_7
#define oe P3_5
void gecikme(int delay)
{
int i,j;
for (j=0;j<delay;j++)
for (i=0;i<10000;i++);
}
void lcdgecikme(int delay)
{
int i,j;
for (j=0;j<delay;j++)
for (i=0;i<2000;i++);
}
void DATAkomut(int komut)
{
RS=0;lcdgecikme(1);
RW=0;lcdgecikme(1);
EN=1;lcdgecikme(1);
clock=0;
DATA=komut;
clock=1;
EN=0;lcdgecikme(1);
}
void DATAveri(char veri[])
{
int i=0;
while(veri[i]!=0)
{
RS=1;lcdgecikme(1);
RW=0;lcdgecikme(1);
EN=1;lcdgecikme(1);
clock=0;
DATA=veri[i];
clock=1;
EN=0;lcdgecikme(1);
i++;
}
}
void LCDac(void)
{
int baslangic[]={kursorgizle,ciftsatir8bit,sagayaz},t;
for(t=0;t<3;t++)
DATAkomut(baslangic[t]);lcdgecikme(1);
}
void main()
{
char dizi[8];
int i;
float deger;
oe=0;
LCDac();
DATAkomut(sil);
DATAveri("fatih");
while(1)
{
oe=1;
i=P2;
deger=(i*5)/255.0;
oe=0;
DATAveri(!!!!!!!!!!);<----------Problem???
}[/b]
|
|
| Back to top |
|
 |
Belsugului
Joined: 10 Mar 2005 Posts: 49 Helped: 1
|
29 Apr 2005 10:37 ftoa ansi c |
|
|
|
|
What I can see you would like to write a floating number to an LCD. I have done such issue already using Borland C++ compiler and 80186 microcontroller. I do not know what type of C compiler you are using and what lkibraries you have, but Borland's compiler has the following two functions converting float to char:
fcvt in stdlib.h
and
gcvt in dos.h
Belsugului
|
|
| Back to top |
|
 |
svicent
Joined: 11 Jul 2001 Posts: 413 Helped: 23
|
29 Apr 2005 11:00 ansi c float to char |
|
|
|
|
const float ROUND[6]={0.49,0.05,0.005,0.0005,0.00005,0.000005};
void ftoa(float fnum, unsigned char decimals, unsigned char *str)
{
float scale;
unsigned char u1,u2;
if (fnum<0.0)
{
fnum=-fnum;
*str++='-';
}
if (decimals>5)
decimals=5;
fnum += ROUND[decimals];
u1=0;
scale=1.0;
while (fnum>=scale)
{
scale *= 10.0;
++u1;
}
if (u1==0)
*str++='0';
else
while (u1--)
{
scale=floor(0.5+scale/10.0);
u2=(unsigned char)(fnum/scale);
*str++=u2+'0';
fnum -= scale*u2;
}
if (decimals==0)
{
*str=0;
return;
}
*str++='.';
while (decimals--)
{
fnum *= 10.0;
u2=(unsigned char) fnum;
*str++ = u2+'0';
fnum -= u2;
}
*str=0;
}
|
|
| Back to top |
|
 |
fatihbasaris
Joined: 01 Oct 2004 Posts: 6
|
29 Apr 2005 21:59 float to char* |
|
|
|
|
ı use keil.like you said, i try to use gcvt and fcvt before. but ı couldn't run.
keil said:
missing function prototip.then ı wrote.
#include<stdiolib.h>
but ı couldn't run.
thanks alot for replies.
|
|
| Back to top |
|
 |
mgajo
Joined: 15 May 2001 Posts: 347
|
29 Apr 2005 23:33 float to char ansi c |
|
|
|
|
Hi
You can visit Keil forum
there was something you need
|
|
| Back to top |
|
 |
echo47
Joined: 07 Apr 2002 Posts: 4206 Helped: 566
|
30 Apr 2005 4:23 c floor float to unsigned char |
|
|
|
|
float to char? I think you mean float to string.
I don't know which standard libraries you have available, but here is an ANSI C example:
| Code: |
#include <stdio.h>
int main(void)
{
char buf[100];
float x = 123.456;
sprintf(buf, "%f", x);
puts(buf);
return 0;
} |
Please use the "code" button so we can see your indenting.
There is no such thing as gcvt or fcvt in ANSI C. They are probably just wrappers around an sprintf call.
|
|
| Back to top |
|
 |
Google AdSense

|
30 Apr 2005 4:23 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
mgajo
Joined: 15 May 2001 Posts: 347
|
30 Apr 2005 21:19 ansi c : rounding floats |
|
|
|
|
For Ke*il
flot2str(Lenght, buf)
for (i = 0; i < 10; i++)
{
sendbyte(buf[i]);//SBUF = buf[i];
}
|
|
| Back to top |
|
 |
Unomano
Joined: 31 Aug 2005 Posts: 43 Location: Minsk, Belarus
|
11 Nov 2005 20:18 float en char |
|
|
|
|
after you include stdio.h and call printf your program code will considerably increase (+2 kbytes for Keil),
so this is not the best solution:(
I use this:
| Code: |
void SendNumber(float x)
{
char d, i = 1;
if(x < 0)
{
x = fabs(x);
SendByte('-');
}
while(x >= 10)
{
x /= 10;
i++;
}
while(x || (i-- > 0))
{
d = x;
x = 10 * (x - d);
SendByte(d + 0x30);
if(x < 0.00001) x = 0;
if(!i && x) SendByte('.');
}
}
|
|
|
| Back to top |
|
 |
tekbirsoft
Joined: 28 Jun 2009 Posts: 1
|
28 Jun 2009 0:47 keil floating number round |
|
|
|
|
| Brother! --- very very SO SO Thanks you.
|
|
| Back to top |
|
 |