Reading a float number out UART

Thread Starter


Joined Dec 17, 2007
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];

fADCresult = getADCresult();
sprintf(cArray, "%3f", fADCresult);

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

// in header file:
#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);
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!


Joined Apr 20, 2004
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.

Thread Starter


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

Rich (BB code):
// header file:
do {                          \
ADCCON2 = 0x3E;               \
ADCCON1 = 0x73;               \
while(!(ADCCON1 & 0x80));     \
v = ADCL;                     \
v |= (((unsigned int)ADCH) << 8); \     // ADC result in two bytes, ADCH:ADCL
} while(0)

// in .c file:
void getTemp(void)
  unsigned int adcValue;
  float outputVoltage;

  // Note that the conversion result always resides in MSB section of ADCH:ADCL
  adcValue >>= 4; // Shift 4 due to 12 bits resolution

  outputVoltage = adcValue * CONST;       /* calibration for ADC temp sensor*/
  fADCresult = ((outputVoltage - OFFSET) / TEMP_COEFF);  /* put calibrated
 result in a floating point variable: fADCresult */