syenidogan
Member level 1
- Joined
- Mar 6, 2014
- Messages
- 41
- Helped
- 2
- Reputation
- 4
- Reaction score
- 3
- Trophy points
- 8
- Activity points
- 297
That's ok. Never use this function again.
void FloatToString (float Value, char * String, char Accuracy)
{
signed long k = 1;
signed long tmp;
char Len;
while (Accuracy--) k = k * 10;
if (Value<0) tmp = ((signed long)Value)*k - (float)(Value*k);
else tmp = (float)(Value*k) - ((signed long)Value)*k;
sLongToStr((signed long)Value, String);
Len = StringLen(String);
String[Len++]='.';
uLongToStr(tmp, String+Len);
}
signed long uLongTosLong (unsigned long Value)
{
signed long Result;
if ((Value)&(1<<30)) Result = - (0xFFFFFFFF - Value);
else Result = Value;
return Result;
}
void sLongToStr (signed long Value, char * String)
{
unsigned long tmp;
if (Value<0)
{
Value=-Value;
tmp=Value;
*String++='-';
uLongToStr(tmp, String+1);
}
else
{
tmp=Value;
uLongToStr(tmp, String);
}
}
what is the memory size you have allocated to ' lcddata ' ?
it is often better to use your own routines which can be optimized for only the features you use
Another possibility would be test different compiler optimization levels to check if can reach some reduction of memory usage. In order to allow the program keep some level of portability to other uC cores, it is somewhat advisable to use the standard C libraries instead of direct access to special function registers.
firstly thankx for helping.Code:void FloatToString (float Value, char * String, char Accuracy) { signed long k = 1; signed long tmp; char Len; while (Accuracy--) k = k * 10; if (Value<0) tmp = ((signed long)Value)*k - (float)(Value*k); else tmp = (float)(Value*k) - ((signed long)Value)*k; sLongToStr((signed long)Value, String); Len = StringLen(String); String[Len++]='.'; uLongToStr(tmp, String+Len); }
Code:signed long uLongTosLong (unsigned long Value) { signed long Result; if ((Value)&(1<<30)) Result = - (0xFFFFFFFF - Value); else Result = Value; return Result; }
Code:void sLongToStr (signed long Value, char * String) { unsigned long tmp; if (Value<0) { Value=-Value; tmp=Value; *String++='-'; uLongToStr(tmp, String+1); } else { tmp=Value; uLongToStr(tmp, String); } }
void uLongToStr (unsigned long Value, char * String)
{
char zero=0;
char tmp=0;
unsigned long cnt=1000000000;
while (cnt!=1)
{
while (Value>=cnt)
{
Value-=cnt;
tmp++;
}
if (tmp) zero=1;
if (zero) * String++ = tmp+48;
tmp=0;
cnt/=10;
}
* String = Value+48;
}
it is working ty.2.03 accuracy 2
1.1 accuracy 1
3.1415 accuracy 4
just amount of digits after the dot
Code:void uLongToStr (unsigned long Value, char * String) { char zero=0; char tmp=0; unsigned long cnt=1000000000; while (cnt!=1) { while (Value>=cnt) { Value-=cnt; tmp++; } if (tmp) zero=1; if (zero) * String++ = tmp+48; tmp=0; cnt/=10; } * String = Value+48; }
Thanks
Damn, I will check this. Possible, some math optimization problems.
void FloatToString (float Value, char * String, char Accuracy)
{
signed long k = 1;
signed long tmp;
char Len, Len2, i=0;
char AftDot[6];
for (tmp = Accuracy; tmp; tmp--) k *= 10;
tmp = MOD(((signed long)Value)*k - Value*k);
sLongToStr((int)Value, String);
Len = StringLen(String);
String[Len++]='.';
sLongToStr(tmp, AftDot);
Len2 = StringLen(AftDot);
while (Accuracy--)
{
if (Accuracy>=Len2) String[Len++] = '0';
else String[Len++] = AftDot[i++];
}
}
unsigned char StringLen (char * String)
{
char cnt=0;
while (* String++) cnt++;
return cnt;
}
void sLongToStr (signed long Value, char * String)
{
unsigned long tmp;
if (Value<0)
{
Value=-Value;
tmp=Value;
*String='-';
uLongToStr(tmp, String+1);
}
else
{
tmp=Value;
uLongToStr(tmp, String);
}
}
void uLongToStr (unsigned long Value, char * String)
{
char zero=0;
char tmp=0;
unsigned long cnt=1000000000;
while (cnt!=1)
{
while (Value>=cnt)
{
Value-=cnt;
tmp++;
}
if (tmp) zero=1;
if (zero) * String++ = tmp+48;
tmp=0;
cnt/=10;
}
* String = Value+48;
}
Try this:
Code:void FloatToString (float Value, char * String, char Accuracy) { signed long k = 1; signed long tmp; char Len, Len2, i=0; char AftDot[6]; for (tmp = Accuracy; tmp; tmp--) k *= 10; tmp = MOD(((signed long)Value)*k - Value*k); sLongToStr((int)Value, String); Len = StringLen(String); String[Len++]='.'; sLongToStr(tmp, AftDot); Len2 = StringLen(AftDot); while (Accuracy--) { if (Accuracy>=Len2) String[Len++] = '0'; else String[Len++] = AftDot[i++]; } }
Code:unsigned char StringLen (char * String) { char cnt=0; while (* String++) cnt++; return cnt; }
Code:void sLongToStr (signed long Value, char * String) { unsigned long tmp; if (Value<0) { Value=-Value; tmp=Value; *String='-'; uLongToStr(tmp, String+1); } else { tmp=Value; uLongToStr(tmp, String); } }
Code:void uLongToStr (unsigned long Value, char * String) { char zero=0; char tmp=0; unsigned long cnt=1000000000; while (cnt!=1) { while (Value>=cnt) { Value-=cnt; tmp++; } if (tmp) zero=1; if (zero) * String++ = tmp+48; tmp=0; cnt/=10; } * String = Value+48; }
unsigned long MOD (signed long Value)
{
if (Value>0) return Value;
else return -Value;
}
Sorry, it just a module of value
Code:unsigned long MOD (signed long Value) { if (Value>0) return Value; else return -Value; }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?