Using Float

This is a discussion on Using Float within the C Programming forums, part of the General Programming Boards category; Hello I am new to C programming and I have a question on how to output a float using "sprintf". ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Using Float

    Hello I am new to C programming and I have a question on how to output a float using "sprintf". I have the following code, but when I run the code on my microchip PIC16F874a (Hi-Tech PICC compiler), all I get is "f" to print on my LCD, but I would like to be getting a decimal number. My code defines adcvalue as a float, then passes that result into an equation called temperature which is also a float. Should I be adding a library? Or is this the complete wrong way of displaying a decimal number on my LCD? Thanks.

    Code:
    #include <pic.h>
    #include <stdio.h>
    #include "LCD.h"
    #include "delay.h"
    
    
    __CONFIG(PWRTDIS & WDTDIS & BORDIS & LVPDIS & XT & UNPROTECT);
    
    
    /* Sample code to set up the A2D module */
    void init_a2d(void)
    
    {
    	ADCON0=129;	// select Fosc/8, AN0, A/D on
    	ADCON1=130;	// select left justify result. A/D port configuration 0
    	ADON=1;		// turn on the A2D conversion module
    	DelayMs(50);
    }
    
    
    main()
    {
    	float adcvalue, temperature;
    	int i;
    	char outString[20];
    	i=0;
    
    	TRISA=0x05;
    
    
    		lcd_init();
    		init_a2d();
    
    			
    		while(i<151)
    
    		{
    			DelayMs(200);  
    			
    			i = i +1;
    		
    		}	
    
    			LCDclear();
    			ADGO=1;
    			while(ADGO)continue;
    			ADIF=0;
    			adcvalue=(ADRESH << 8) + ADRESL;
    			
    			temperature = ((1/((-2.828570/(((100/20400)*adcvalue)+1.65))+.857143))+102.371);
    			sprintf(outString,"%f",temperature);
    			LCDwritestring(outString);
    			ADON=0;
    for(;;);
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Well it would be the right way on your desktop, but embedded systems (especially small embedded systems like yours) restrict the scope of certain library functions.

    One typical thing you see is that unknown conversions are simply stripped of the % character (so %f simply becomes a 'f', as you observe).

    Maybe there is some other float to string function, or you can convert the float into two integers which you can then print?

    Code:
    int intPart = temperature;
    int fracPart = ((int)( temperature * 1000 )) % 1000;
    sprintf("%d.%03d", intPart, fracPart );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21