ADGAN
Full Member level 5
- Joined
- Oct 9, 2013
- Messages
- 295
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 18
- Activity points
- 1,837
I dont know much about mikroc (i tried it briefly a few years back), but i dont think the availability of sprintf() is limited by which PIC you are using.The sprintf function is also not available since I'm using PIC16F887.
int float_to_char(char *buff, int start_pos, float fnum, short dec)
{
long i;
int pos, k;
if(fnum < 0) {
buff[start_pos++] = '-';
fnum *= -1;
}
i = fnum;
pos = long_to_char(buff, start_pos, i);
buff[pos++] = '.';
fnum -= i;
fnum *= pow10l(dec);
i = fnum;
pos += dec;
for(k = dec; k > 0; k--){
buff[--pos] = 48 + (i % 10);
i /= 10;
}
return pos + dec;
}
int long_to_char(char *buff, int start_pos, long inum)
{
char n, k;
long div = 1000000000;
if(inum < 0) {
buff[start_pos++] = '-';
inum *= -1;
}
for(k = 0; k < 10; k++)
{
n = (inum / div) % 10;
if(n > 0) break;
div /= 10;
}
buff[start_pos++] = 48 + n;
k++;
div /= 10;
for(; k < 10; k++)
{
n = (inum / div) % 10;
buff[start_pos++] = 48 + n;
div /= 10;
}
return start_pos;
}
long pow10l(short p)
{
switch(p)
{
case 1: return 10l;
case 2: return 100l;
case 3: return 1000l;
case 4: return 10000l;
case 5: return 100000l;
case 6: return 1000000l;
case 7: return 10000000l;
case 8: return 100000000l;
case 9: return 1000000000l;
default: return 1;
}
}
Isn't there a function that I can use for this task. It would be much easy. I found this in the internet.
Code:int float_to_char(char *buff, int start_pos, float fnum, short dec) { long i; int pos, k; if(fnum < 0) { buff[start_pos++] = '-'; fnum *= -1; } i = fnum; pos = long_to_char(buff, start_pos, i); buff[pos++] = '.'; fnum -= i; fnum *= pow10l(dec); i = fnum; pos += dec; for(k = dec; k > 0; k--){ buff[--pos] = 48 + (i % 10); i /= 10; } return pos + dec; } int long_to_char(char *buff, int start_pos, long inum) { char n, k; long div = 1000000000; if(inum < 0) { buff[start_pos++] = '-'; inum *= -1; } for(k = 0; k < 10; k++) { n = (inum / div) % 10; if(n > 0) break; div /= 10; } buff[start_pos++] = 48 + n; k++; div /= 10; for(; k < 10; k++) { n = (inum / div) % 10; buff[start_pos++] = 48 + n; div /= 10; } return start_pos; } long pow10l(short p) { switch(p) { case 1: return 10l; case 2: return 100l; case 3: return 1000l; case 4: return 10000l; case 5: return 100000l; case 6: return 1000000l; case 7: return 10000000l; case 8: return 100000000l; case 9: return 1000000000l; default: return 1; } }
https://www.mikroe.com/forum/viewtopic.php?f=88&t=24842
But the problem is it this values goes all around the display.
#ifndef Byte
#define Byte unsigned char
#endif
#ifndef Word
#define Word unsigned int
#endif
#ifndef DWord
#define DWord unsigned long
#endif
float f1,f2,f3;
Byte CRam1[32];
Word EAx;
unsigned char *fltToa (float x, unsigned char *str,char precision)
{
/* converts a floating point number to an ascii string */
/* x is stored into str, which should be at least 30 chars long */
unsigned char *adpt;
int ie, i, k, ndig;
double y;
adpt=str;
ndig = ( precision<=0) ? 7 : (precision > 22 ? 23 : precision+1);
ie = 0;
/* if x negative, write minus and reverse */
if ( x < 0)
{
*str++ = '-';
x = -x;
}
else *str++ = ' '; // rajoute espace pour le signe +
/* put x in range 1 <= x < 10 */
if (x > 0.0) while (x < 1.0)
{
x *= 10.0; // a la place de =*
ie--;
}
while (x >= 10.0)
{
x = x/10.0;
ie++;
}
// in f format, number of digits is related to size
ndig += ie; // a la place de =+
//round. x is between 1 and 10 and ndig will be printed to
// right of decimal point so rounding is ...
for (y = i = 1; i < ndig; i++)
y = y/10.;
x += y/2.;
if (x >= 10.0) {x = 1.0; ie++;}
if (ie<0)
{
*str++ = '0'; *str++ = '.';
if (ndig < 0) ie = ie-ndig;
for (i = -1; i > ie; i--) *str++ = '0';
}
for (i=0; i < ndig; i++)
{
k = x;
*str++ = k + '0';
if (i == ie ) *str++ = '.';
x -= (y=k);
x *= 10.0;
}
*str = '\0';
return (adpt);
}
void main()
{
.........
//T°Ext LM335 2730mV at 0°C:
EAx=Mesure_ADC(0);
f1=(float) (EAx)*0.40000-273.0; // Vref = 4.096V
// *fltToa (float x, unsigned char *str,char precision)
k=fprintf(_H_USART,"T°Ext:%4d %s°C ;",EAx,fltToa(f1,CRam1,2));
.......
//Display float value with 3 decimal places to LCD.
void FloatToLCD(float fval)
{
unsigned int value_x1000;
char digit;
//Ex. fval = 34.576
value_x1000 = (unsigned int)(fval * 1000); // value_x1000 = 34576
digit = (value_x1000/10000)%10;
lcd_putch('0'+digit); //Display '3'
digit = (value_x1000/1000)%10;
lcd_putch('0'+digit); //Display '4'
lcd_putch('.'); //Display '.'
digit = (value_x1000/100)%10;
lcd_putch('0'+digit); //Display '5'
digit = (value_x1000/10)%10;
lcd_putch('0'+digit); //Display '7'
digit = (value_x1000)%10;
lcd_putch('0'+digit); //Display '6'
return;
}
@paulfjujo
How do I use your function in the main?
Yes, obviously.I want to display a variable not a constant therefore I would not know the value.
______________Thanks for the reply. Not the whole display. after printing 0.0000 in the next column again it prints 0.0000 then goes to next line. I'm clearing the display at the very beginning.
void FloatToLCD(float fval)
{
int value;
char digit;
//Ex. fval = 34.5769
value= fval * 10000; // value= 345769
digit = (value/100000)%10;
lcd_putch('0'+digit); //Display '3'
digit = (value/10000)%10;
lcd_putch('0'+digit); //Display '4'
lcd_putch('.'); //Display '.'
digit = (value/1000)%10;
lcd_putch('0'+digit); //Display '5'
digit = (value/100)%10;
lcd_putch('0'+digit); //Display '7'
digit = (value/10)%10;
lcd_putch('0'+digit); //Display '6'
digit = (value)%10;
lcd_putch('0'+digit); //Display '9'
return;
}
void FloatToLCD(float fval)
{
int value;
char digit;
//Ex. fval = 34.5769
value= fval * 10000; // value= 345769
.. etc ....
}
...
unsigned long int value;
value= (unsigned long int) (fval * 10000.0); // value= 345769
...
Its only shows the '.'
float y = 0.3244;
void FloatToLCD(float fval)
{
unsigned long int value_x1000;
char digit;
//Ex. fval = 34.576
value_x1000 = (unsigned long int)(fval * 1000); // value_x1000 = 34576
digit = (value_x1000/10000)%10;
Lcd_chr(4,3,0+digit); //Display '3'
digit = (value_x1000/1000)%10;
Lcd_chr(4,4,0+digit); //Display '4'
Lcd_chr_cp('.'); //Display '.'
digit = (value_x1000/100)%10;
Lcd_chr(4,6,0+digit); //Display '5'
digit = (value_x1000/10)%10;
Lcd_chr(4,7,0+digit); //Display '7'
digit = (value_x1000)%10;
Lcd_chr(4,8,0+digit); //Display '6'
return;
}
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?