sdcc-user

 [Sdcc-user] float print routine From: Olgierd Eysymontt - 2006-07-05 19:31:59 ```Hi, as I got no answer to the problem of printing floats in pic16 port, I made a small routine to print numbers in the form DDDDD.MMMMMMM to 8 decimal places, it's very simple, but can be useful to somebody else. The usart_send_2 routine is used to send chars to the serial port, so can be implemented by the user in different ways. Olgierd ---------------------- // code /** Converts a float number into a string considering 'left_min_zeros' numbers before the decimal place and 'decimals' decimals */ void send_float(float val, unsigned char decimals , unsigned char left_min_zeros){ unsigned char i,j; union float_long fl1; unsigned char sign,et; char exp; char int_part[10],dec_part[10]; unsigned long mantissa,mint,mflot,sum_dec,mask,comp; //Lookup Table of 1e8/2^i for calculation of decimal valued unsigned long dec_vals[23]={ 50000000 , 25000000 , 12500000 , 6250000 , 3125000 , 1562500 , 781250 , 390625 , 195312 , 97656 , 48828 , 24414 , 12207 , 6103 , 3051 , 1525 , 762 , 381 , 190 , 95 , 47 , 23 , 12 }; fl1.f = val; sign=SIGN(fl1.l); exp = EXP (fl1.l) - 127; mantissa=MANT(fl1.l); mint=mantissa; for(i=0;i<23-exp;i++){ mint=mint >> 1; } et=9+exp; mflot = ((mantissa << et) >> 9); // now do an arithemtic sum of decimals values sum_dec=0; for(i=0;i<23;i++){ mask=1ul< 0) for(i=0;i 0) for(i=0;i
 Re: [Sdcc-user] float print routine From: Olgierd Eysymontt - 2006-07-06 15:07:39 ```BTW, the code is posted to be freely used, so use GPL, GPL2, BSD or the licence you want to use, not a problem. Olgierd El mi=E9, 05-07-2006 a las 15:30 -0400, Olgierd Eysymontt escribi=F3: > Hi, as I got no answer to the problem of printing floats in pic16 port, > I made a small routine to print numbers in the form DDDDD.MMMMMMM to 8 > decimal places, it's very simple, but can be useful to somebody else. >=20 > The usart_send_2 routine is used to send chars to the serial port, so > can be implemented by the user in different ways. >=20 > Olgierd >=20 > ---------------------- > // code >=20 >=20 > /** > Converts a float number into a string considering 'left_min_zeros'=20 > numbers before the decimal place and 'decimals' decimals >=20 > */ > void send_float(float val, unsigned char decimals , unsigned char > left_min_zeros){ > unsigned char i,j; > union float_long fl1; > unsigned char sign,et; > char exp; > char int_part[10],dec_part[10]; > =09 > unsigned long mantissa,mint,mflot,sum_dec,mask,comp; > =09 > //Lookup Table of 1e8/2^i for calculation of decimal valued > unsigned long dec_vals[23]=3D{ > 50000000 , > 25000000 , > 12500000 , > 6250000 , > 3125000 , > 1562500 , > 781250 , > 390625 , > 195312 , > 97656 , > 48828 , > 24414 , > 12207 , > 6103 , > 3051 , > 1525 , > 762 , > 381 , > 190 , > 95 , > 47 , > 23 , > 12 }; > =09 >=20 > fl1.f =3D val; >=20 > sign=3DSIGN(fl1.l); > exp =3D EXP (fl1.l) - 127; > mantissa=3DMANT(fl1.l); > =09 >=20 > mint=3Dmantissa; > for(i=3D0;i<23-exp;i++){ > mint=3Dmint >> 1; > } > et=3D9+exp; > mflot =3D ((mantissa << et) >> 9); > =09 > // now do an arithemtic sum of decimals values > sum_dec=3D0; > for(i=3D0;i<23;i++){ > mask=3D1ul< comp=3Dmask & mflot; > =09 > if (comp !=3D 0ul ){ > sum_dec=3Dsum_dec+dec_vals[22-i]; > } > } > // now I print the number > ltoa(mint, int_part, 10); > ltoa(sum_dec, dec_part, 10); > =09 > if (sign=3D=3D1) > usart_send_2('-'); > else > usart_send_2(' '); > =09 > // check the sizes, reuse the exp variable > exp =3D left_min_zeros-strlen(int_part); > // do i have to add zeros a beginning > if(exp > 0) > for(i=3D0;i usart_send_2('0'); > // send the integer part > send_uint(mint); > usart_send_2('.'); > =09 > // check the zeros of decimal part, reuse the exp variable > exp =3D 8-strlen(dec_part); > if(exp > 0) > for(i=3D0;i usart_send_2('0'); > // send the decimal part > for(i=3D0;i usart_send_2(dec_part[i]); > =09 > =09 > } >=20 >=20 >=20 > Using Tomcat but need to do more? Need to support web services, securit= y? > Get stuff done quickly with pre-integrated technology to make your job = easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geron= imo > http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D120709&bid=3D263057&dat= =3D121642 > _______________________________________________ > Sdcc-user mailing list > Sdcc-user@... > https://lists.sourceforge.net/lists/listinfo/sdcc-user ```