# Reading a float number out UART

#### hazzman

Joined Dec 17, 2007
13
hey people,

Im really close to achieving my objective but I seem to get a type conversion error/problem.

I'm basically sampling from an temperature sensor in an ADC on a 8051 MCU and every second firing a timer to take a sample and send out on UART (RS-232) in which i have connected a serial cable to my PC with hyperterminal running. I have the ADC result saved as a float, but the UART has a 1 byte tx/rx buffer so tried using sprintf:

Rich (BB code):
char cArray[4];

for (i=0  ; i<sizeof(cArray); i++)
{
UART0_WAIT_AND_SEND( cArray );
}

#define UART0_WAIT_AND_SEND(x) \
do                             \
{ while (!UTX0IF);             \
UTX0IF=0;                    \
U0DBUF=(x);                  \
} while (0)


Hyperterminal shows:
Tâ"þTâ"þTâ"þTâ"þ // repetitively on and on....

The above doesn't get me anything
However if i just do like :
Rich (BB code):
/*sprintf(cArray, "%3f", fADCresult);  // Get rid of this sprintf*/
UART0_WAIT_AND_SEND((unsigned char)fADCresult);
Hyperterminal:
TÁTÁTÁTÁTÁTðTðTðTðTðTðTðTðTðTƒTðTðTÁTÁ /* Ignore the 'T's i add them in as i need to send out a byte first. A changing output when i touch the MCU, built in temp-sensor*/

So i know ADC readings are occuring by the output(random character/symbols) changing when i warm up the MCU (touch it with my finger! - v. technical!) and they change back when i let go.

So my question is how can i get the ADCresult float value into a char array(string) to output onto hyperterminal as the char array equivalent of the fADCresult float vals ?????? I know i'm close!

#### beenthere

Joined Apr 20, 2004
15,819
Might you have a problem with the data typing? The output of an ADC is an integer, not floating point. If it's a bipolar conversion, it's a signed integer.

#### hazzman

Joined Dec 17, 2007
13
ok to clarrify why i use float here is my temp sensor sampling code:

Rich (BB code):
// header file:
#define SAMPLE_TEMP_SENSOR(v) \
do {                          \
} while(0)

// in .c file:
void getTemp(void)
{
result in a floating point variable: fADCresult */