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

Help with float variable and UART transmission

Hi everyone

I'm having trouble reading the right values from the uart using a serial port logger.

I'm using the cygnal C8051F124 uc and I send in four bytes a float variable by the UART0

To convert the flot variable in 4 bytes I have the following code:

union {

float floater;
unsigned long int longinter;
unsigned char string[4];
unsigned int interger[2];
}float_char;

To convert I use:

float_char.floater=0.43567;
sendbuff[0]=float_char.string[0];
sendbuff[1]=float_char.string[1];
sendbuff[2]=float_char.string[2];
sendbuff[3]=float_char.string[3];



Then If I sent the same value stored in sendbuff[x] everytime I send the data by the UART0 I get the exact value without errors.

That means maybe the conversion from float to 4 bytes is incorrect or maybe cause some problems.

I think the UART tranmission is correct because if I dont change the value in the sendbuff[i] variables I obtained in the pc the values that I want.

Please give me suggestions

Thanks

Parents
  • Jean Paul,

    I had a little trouble with the English on your post, but I think I have a possible idea as to what's happening with your code (although it would be useful to actually see the portion of the code that does the transmission). You've created a buffer of bytes you want to send out of the port, and you likely have the interrupt service routine for the UART send them one at a time and keep track of which byte to send with a counter. What MIGHT be happening if you see problems when modifying the value is that you're modifying it before it has been sent. That is, if you do another assignment to float_char.floater before your routine has finished transmitting all four bytes of the last value, then you will have errors. Try setting/clearing a flag that indicates when the ISR is using the buffer to prevent overwriting it.

Reply
  • Jean Paul,

    I had a little trouble with the English on your post, but I think I have a possible idea as to what's happening with your code (although it would be useful to actually see the portion of the code that does the transmission). You've created a buffer of bytes you want to send out of the port, and you likely have the interrupt service routine for the UART send them one at a time and keep track of which byte to send with a counter. What MIGHT be happening if you see problems when modifying the value is that you're modifying it before it has been sent. That is, if you do another assignment to float_char.floater before your routine has finished transmitting all four bytes of the last value, then you will have errors. Try setting/clearing a flag that indicates when the ISR is using the buffer to prevent overwriting it.

Children
No data