2014-07-10 01:34 AM
I'm trying to receive a float number through a VCP and writing it in the Flash memory of the STM32F4 discovery board.
The functions used to write in the Flash memory (FLASH_ProgramDoubleWord...FLASH_ProgramByte) accept an unsigned integer value as data input,but I've managed to write a signed integer in the Flash memory using this code:int dataflash1 = -1000;
int gain;
uint32_t Address= 0x08008000;
.......
FLASH_ProgramWord(Address,dataflash1);
.......
gain=*(int*)Address;
Can someone tell me how can I write a float data in the Flash memory?
Is it possible to send/receive a float data through VCP?
Thank you.
2014-07-10 04:40 AM
Hi
First understand how the computer/compiler defines a floating point number : http://en.wikipedia.org/wiki/Floating_point Look at the section ''Internal representation
'' and at the ''Single'' representation - this is usually (but not always) what C uses. ''Can someone tell me how can I write a float data in the Flash memory? '' If you can cast the float into the input parameter of your Flash write function and it writes 4 bytes then it should work. ''Is it possible to send/receive a float data through VCP?'' Have a look at : http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof Basically, the number will come in as a string, so it must be converted from the string back into a number (that the computer understands).2014-07-10 06:17 AM
Hi,
I already tried to cast the input data by doing:float dataflash1 = 456;
float gain;
uint32_t Address= 0x08008000;
.......
FLASH_ProgramWord(Address, (uint32)dataflash1);
.......
gain=*(float*)Address;
but I don't know how to interpretate the results:
dataflash1 d_gain
456 -> 1.72359711 e-043
123000.456 -> 1.72359711 e-040
9999456 -> 1.40129706 e-039
999999 -> 1.40129706 e-039
- 9999456 -> 0
2014-07-10 07:13 AM
FLASH_ProgramWord(Address, (uint32)dataflash1);
is actually
converting the float variable dataflash1 into equivalent integer value (i.e. 123U instead of 345).
You need to do something like:
FLASH_ProgramWord(Address, *(uint32 *)&dataflash1);
to preserve the original format of the variable and only mimic it as an integer container.
2014-07-10 07:21 AM
Hi Ivan,
It totally makes sense, that's why it works. Thanks.