This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Float to UART

i need to display a floating point number ranging from 0.1 to 0.001 over uart
here is what i managed to search from forums and what not

unsigned long *chptr;
float f=0.1;
void main()
{
PINSEL0=0x5;
uart0_open();

 chptr= (unsigned long*)&f;

 uart0_printf("\n%x \n",f);
 sendchar(*chptr++);
 sendchar(*chptr++);
 sendchar(*chptr++);
 sendchar(*chptr++);


i am getting weird results
any help masters?

another patch i worked with was

void main()
{
PINSEL0=0x5;
uart0_open();

uart0_printf("Enter Data");
while(1)
{
for(i=0;i<6;i++)
{
ch[i]=getkey();
}

data=chars2float(ch);
uart0_printf("%x\n",data);
float2chars(data,ch2);
uart0_printf("\n%c%c%c%c%c",ch2[0],ch2[1],ch2[2],ch2[3],ch2[4]);

}
}

float chars2float(char* parts) {

                union {
                unsigned long bits;
                float number;
        } floatbits;

        floatbits.bits = parts[0] + ((long)parts[1]<<7) + ((long)parts[2]<<14) +  ((long)parts[3]<<21) + ((long)parts[4]<<28);

        return floatbits.number;
}


  void float2chars(float input, char* parts) {

        union {
                unsigned long bits;
                float number;
        } floatbits;

        floatbits.number = input;

        parts[0] = floatbits.bits & ~0x80; // clear top bit
        parts[1] = (floatbits.bits >> 7) & ~0x80;
        parts[2] = (floatbits.bits >> 14) & ~0x80;
        parts[3] = (floatbits.bits >> 21) & ~0x80;
        parts[4] = (floatbits.bits >> 28);

}



i am getting results here but the problem is i get a [] for the last digit.

any rectifications?

Parents
  • Thanks sir,
    figured my retarded mistake :) many thanks

    #include<lpc214x.h>
    #include"uart0.h"
    #include <stdio.h>
    #include <stdlib.h>
      unsigned i;
      unsigned char ch[6];
      char buffer[16];
      float flt;
    void sendstring(char *s)
    {
    while(*s)
        sendchar(*s++);
    }
    
    void main()
    {
    PINSEL0=0x5;
    uart0_open();
    while(1)
            {
             uart0_printf("\nEnter Text\n");
                    for(i=0;i<6;i++)
                            ch[i]=getkey();
    
         flt=atof(ch);
            sprintf(buffer,"%f",flt);
            sendstring(buffer);
                                                            }
       }
    
    
    

Reply
  • Thanks sir,
    figured my retarded mistake :) many thanks

    #include<lpc214x.h>
    #include"uart0.h"
    #include <stdio.h>
    #include <stdlib.h>
      unsigned i;
      unsigned char ch[6];
      char buffer[16];
      float flt;
    void sendstring(char *s)
    {
    while(*s)
        sendchar(*s++);
    }
    
    void main()
    {
    PINSEL0=0x5;
    uart0_open();
    while(1)
            {
             uart0_printf("\nEnter Text\n");
                    for(i=0;i<6;i++)
                            ch[i]=getkey();
    
         flt=atof(ch);
            sprintf(buffer,"%f",flt);
            sendstring(buffer);
                                                            }
       }
    
    
    

Children
  • Grab an entry-level book on C programming. That is not how to play with C strings. Don't you believe in zero-termination?

    Another thing - do visit Wikipedia or some other source and read up on floating-point numbers. Don't expect that you can take the individual storage bytes of a floating point number and send over the serial port and have the other side manage to show it as a printable number. If it was binary on the transmit side, it will be binary on the other side too...

    When you have issues, the #1 step is to read. Not to directly ask questions.

  • Remember that you are not the only programmer in the world. And you are not breaking a new ground also. Hence whatever problems you are facing, people around the world have face all those problems years back and have also come up with solutions that can be readily used.

    Hence always search for a possible available solution. It is already existing in some corner of the world and Google can search it!!

    Have you ever heard of Standard Libraries?
    Read sprintf, sscanf, atoi, ftoa etc.