This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Transmitting 32 bit float in 8 bit UART ...

Other Parts Discussed in Thread: MSP430F1611

I have just started to use the TI DSP development environment and I
have got a question that is very simple (I would assume) but I am
wasting way too much time trying to figure out.

I am doing plane wave beamforming on the DSP ... I want to transmit
the resulting data out via UART.  The board I am using has a UART
output and I can transmit out characters without any problems.  My
problem is that all my data is in floating point form (32 bit).  But
in UART I transmit one char at a time 8 bits.  Does anyone know an
easy way to take the array of floating point (32 bit) numbers and
selectively transmit them in 8 bit increments?

Thanks for any help.

S

  • Are you viewing the UART output through Tera Term or some other console?  If so, you will need to convert your floating point numbers into an ascii representation.  The easiest way to do that would be a C runtime function such as sprintf.

  • One way to break a float into 8-bit numbers is to declare a union of float and char[4]. The following should work once you get the syntax right:

    union
    {
       float f;
       char c[4];
    } floatNchars;

    floatNchars.f = floatVar;
    WriteByteToUart( floatNchars.c[0] ); ... etc.

    You could also cast a pointer to your float to a pointer to char's, like

    char *p_c = (char *)&floatVar;
    WriteByteToUart( *p_c++ );

    There are probably some other ways, like casting to an int and shifting and masking then casting to a char, but these two seem the simplest.

    In any method, you will need to take care to observe endianess on both ends of the Uart (DSP and destination).

  • Well, I could definately do it that way ... except I am going to be transmitting a lot of data.  I am already concerned about whether I can get the data out at the rate I am wanting to.  If I transmit it out by converting it to ascii ... it will be transmitting a 8bit ascii character for each digit in the float number ... say I am transmitting the float value ... 562.90345 ... this number is stored in memory as a 32 bit value ... but to transmit it as ascii characters ... it would take 9 ... 8 bit characters ... or 72 bits to transmit one 32 bit number.

    Thanks for the thought though.

    S

  • Thanks RandyP ... I tried it out and it was exactly what I was trying to accomplish.  Thanks for your help.

    S

  • Does this meet your data transmission requirement? If not, let us know which DSP you are using and what device you are trying to get the data to.

  • Hi all,

    i am just trying to do the same program, but I am using the MSP430F1611, not a DSP. I tried to program some codes but I had no success. My last code was this. I think it is easy to understand. I want to separate the float number in different digits like this: 345.678 ==> 3 4 5 . 6 7 8 and send the diferent numbers via UART. I programed this code with DevC software and it worked perfectly, quite the opposite with IAR. It doesn't work with IAR. I know that this code has some restrictions like that i can use only three integers and three decimals but i don't get any other idea about how to do this. It should be a simple problem but i think that it is defeating me...

    Anybody can help me, maybe telling me what is wrong with my code, or with other ideas, or with some code to send a float via UART with a MSP430. I don't understand the code in the previous posts and i don't know if it is possible to use it with a MSP (not a DSP)...

    static void SendValue(float data)    
    {
      
        static int number;
        static char digits[7]={0,0,0,0,0,0,0};
        static int i=0;
       
       
        number = abs(data*1000);
        
        digits[6]= (number % 10) + 48; //add 48 for the corresponding ASCII number
          number = number/10;
        digits[5]= numero % 10 + 48;
         number = number/10;
        digits[4]= nnumber % 10 + 48;
          number = number/10;
          digits[3]= 46;  //"."
        digits[2]= number % 10 + 48;
         number = number/10;
        digits[1]= number % 10 + 48;
          number = number/10;
        digits[0]= number % 10 + 48;
          //number = number/10;
       
        if (data <0) 
        { TXBUF0 = 45; //"-"
            while (!(IFG1 & UTXIFG0));
        }
       
      for(i=0; i<7 ; i++)
      {
         TXBUF0 = (digits[i]);
          while (!(IFG1 & UTXIFG0));
      }

    Thank you very much for your help in advance.

    Greetings

    Alejos

  • Please post this to the MSP430 Forum so you can get someone who knows how it handles floats and ints. They will be able to tell you more quickly what you are doing wrong.

  • Alejos,

    This is exactly the sort of thing that sprintf does.  Does the MSP430 not have a library of standard C functions?

    Brad

  • Sorry RandyP, i didn't realise the forum i was posting. I saw the title and just asked my question. Now I am moving my post to the MSP430 Forum... Thank you.

    Brad:  yes, IAR has the sprintf() function in C. I tried to use this function but didn't work. I found in the documentation that this function does not support float numbers for the MSP430.

    I think i need more suggestions.

    Thank you very much...

     

    Alejos