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?

0