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.

IEEE 754 Compliant Floating Point to decimal

Status
Not open for further replies.

roykyn

Full Member level 5
Joined
Sep 15, 2006
Messages
253
Helped
12
Reputation
24
Reaction score
5
Trophy points
1,298
Activity points
2,704
i want to convert 32-bit floating point number in to decimal so tat i can display it on 16X2 lcd ..... i am using pic16f72.....i need the procedure or how the software works....
 

You may find this code useful if you don't want the code size of sprintf. Once you have got ASCII characters in s[], you could have output that to no matter graphical display or 2*20 LCD display.

Full project to display temperature and RH values in 1 decimal place using this function shown below (for 65k color LCD display)

If more decimal places wanted, consider sprintf.

Code:
/*
*********************************************************************************************************
*                        CONVERT A FLOATING POINT NUMBER TO STRING WITH 1 DECIMAL PLACE
*
* Description :	This function converts a floating point to a null terminated string 
*				with 1 decimal place.
*			
*				Examples:
*					
*				float f = 9.567;
*				ftoa(&s[0], f);		//s[]={'9','.','5', 0}
*				float f = -0.189;
*				ftoa(&s[0], f);  	//s[]={'-', '0', '.', '1', 0}
* Arguments   : 'unsigned char* buf' 	is the pointer to the string holding the conversion result
*				'float f'				is the input floating point
* Returns     : None
* Notes		  : This routine modified from itoa10() in ..\sample\misc folder of ht-picc
*				If more decimal places required, modify the last section of this code
*				Range of f in (-3,276.7, 3,276.7)
*				This function doesn't print result like 0.0. Instead, a single '0' got printed.
*********************************************************************************************************
*/
void ftoa(unsigned char *buf, float f) {
	unsigned int rem;
	unsigned char *s,length=0;
	int i;

	i = (int)((float)f*10);

	s = buf;
	if (i == 0){ 
		*s++ = '0'; 
		*s=0; //null terminate the string
	} else {	
		if (i < 0) {
			*buf++ = '-';
			s = buf;
			i = -i;
		}
		//while-loop to "decode" the long integer to ASCII by append '0', string in reverse manner
		//If it is an integer of 124 -> string = {'4', '2', '1'}
		while (i) {
			++length;
			rem = i % 10;
			*s++ = rem + '0';
			i /= 10;
		}
		//reverse the string in this for-loop, string became {'1', '2', '4'} after this for-loop
		for(rem=0; ((unsigned char)rem)<length/2; rem++) {
			*(buf+length) = *(buf+((unsigned char)rem));
			*(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1));
			*(buf+(length-((unsigned char)rem)-1)) = *(buf+length);
		}

		/* Take care of the special case of 0.x if length ==1*/	
		if(length==1) {
			*(buf+2) = *buf;
			*buf = '0';
			*(buf+1) = '.';
			*(s+2)=0; //null terminate
		} else {
			*(buf+length) = *(buf+length-1);
			*(buf+length-1)='.';
			*(s+1)=0; //null terminate
		}
	}
}

John Leung
www.TechToys.com.hk
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top