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(;;); }



LinkBack URL
About LinkBacks


