Reading a float number out UART

Discussion in 'Embedded Systems and Microcontrollers' started by hazzman, Jan 28, 2008.

  1. hazzman

    Thread Starter Member

    Dec 17, 2007
    13
    0
    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:


    Code ( (Unknown Language)):
    1.  
    2. char cArray[4];
    3.  
    4. fADCresult = getADCresult();
    5. sprintf(cArray, "%3f", fADCresult);
    6.  
    7. for (i=0  ; i<sizeof(cArray); i++)
    8.   {
    9.     UART0_WAIT_AND_SEND( cArray[i] );
    10.   }
    11.  
    12. // in header file:
    13. #define UART0_WAIT_AND_SEND(x) \
    14. do                             \
    15. { while (!UTX0IF);             \
    16.   UTX0IF=0;                    \
    17.   U0DBUF=(x);                  \
    18. } while (0)
    19. [/i]


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

    The above doesn't get me anything
    However if i just do like :
    Code ( (Unknown Language)):
    1.  
    2. /*sprintf(cArray, "%3f", fADCresult);  // Get rid of this sprintf*/
    3. UART0_WAIT_AND_SEND((unsigned char)fADCresult);
    4.  
    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!
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    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.
     
  3. hazzman

    Thread Starter Member

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

    Code ( (Unknown Language)):
    1.  
    2. // header file:
    3. #define SAMPLE_TEMP_SENSOR(v) \
    4. do {                          \
    5. ADCCON2 = 0x3E;               \
    6. ADCCON1 = 0x73;               \
    7. while(!(ADCCON1 & 0x80));     \
    8. v = ADCL;                     \
    9. v |= (((unsigned int)ADCH) << 8); \     // ADC result in two bytes, ADCH:ADCL
    10. } while(0)
    11.  
    12. // in .c file:
    13. void getTemp(void)
    14. {
    15.   unsigned int adcValue;
    16.   float outputVoltage;
    17.  
    18.   SAMPLE_TEMP_SENSOR(adcValue);
    19.   // Note that the conversion result always resides in MSB section of ADCH:ADCL
    20.   adcValue >>= 4; // Shift 4 due to 12 bits resolution
    21.  
    22.   outputVoltage = adcValue * CONST;       /* calibration for ADC temp sensor*/
    23.   fADCresult = ((outputVoltage - OFFSET) / TEMP_COEFF);  /* put calibrated
    24.  result in a floating point variable: fADCresult */
    25.  
    26.  
     
Loading...