Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] mikroC, split a floating point number into individual digits

Status
Not open for further replies.

crazy-igzp

Member level 1
Joined
Feb 3, 2014
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Settat, Morocco
Activity points
255
I am helping a friend with a small electronics project using a PIC microcontroller 16F877 (A) which I am programming with mikroC.

I have run into a problem which is that I have a floating point number in a variable lets say for example 1234.123456 and need to split it out into variables holding each individual number so I get Char1 = 1, Char2=2 etc etc. for display on LCD. The number will always be rounded to 3 or 4 decimal places so there should be a need to track the location of the decimal point.

Any advice on how to get this split would be greatly appreciated.
 

probably the fastest way is to convert it to a string
then process char[acter] by char[acter]

not sure what you mean by the 'need to track the location of the decimal point'
 

Use mikroC FloatToStr() function. Remember the array has to be atleast 23 bytes for using with FloatToStr() function. Alternately you can use paulfjujo's float2ascii.h file. For this you have to declare an array with minimum 30 bytes. PM paulfjujo and request float2ascii.h file.
 
I agree with milan.rajik, you can use FloatToString() function, then you can refer to the single digit by a pointer to the character that compose the string.
 

hello


With converting float to string with a defined format.. you can know, how many digit used after the Point separator..
fltToa(float value, Texte table to store the result, Nb of digit after the decimal point);
You have all digit in the teste table
with strlen you Now how many characteres stored inthe texte table.
Strlen(txt)-Nb of digit after the point, gives the position of the point




Code:
float f1,f2,f3;
float Oldf1,Oldf2;
static unsigned char CRam1[32];


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;

x=x+0.0000012; // <-  le petit plus qui fait la difference de comportement!
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';
}



// init Exponential filtering
    EA2=Mesure_ADC(1);
    f2=(float)EA2*0.40;   // (4096 /1024 Points)
    if (f2>60.0) f2=60.0;
    if(f2<0.001) f2=0.0;
    Oldf2=f2;
 
 .... init nokia LCD ...
 
while(1)
{
  //T° Int LM35DZ 0mV at 0°C
    EA2=Mesure_ADC(1);
    txt=Texte;
   OUT_RS232
    f2=(float)EA2* 0.4;  // (4096 /1024 Points)
    if (f2>60.0) f2=60.0;
    if (f2<0.0) f2=0.0;
    f3=0.2*f2+0.8*Oldf2;  // K filter =0,2 et complement 1.0-0,20=0.80    K=1.0=NO filter   K=0.0=) never refresh
    Oldf2=f3;
    k=sprintf(txt,"TInt=%s C",fltToa(f2,CRam1,2));   // 2 digits after the point
    gotoxy(0,3);
    Nokia_PutRamString(txt);
........
........
   // or to keep allways the same number of digits, add spaces 
    fltToa(f3,CRam1,2);
    k=strlen(CRam1);
    if (k<6)   
    {
    for (i=6;i>0;i--) CRam1[i]=CRam1[i-1];
    CRam1[0]=' ';  	
    }  
    k=sprintf(txt,"TInt=  %s",CRam1);   //  xxx.xx
    Nokia_PutRamString(txt);
// to find out where is the decimal point
 // 
   fltToa(f3,CRam1,2);
    k=strlen(CRam1)-2;
   // decimal point is at k  position !
 // so at Cram1[k]
 }
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top