I am trying to break apart a float but none of the bit instructions seem to work on a float.
I want to take the float and break it in to 2 words HiWord and LoWord. #define makeLong(hi, low) ((hi) << 16 & (low))
//This defines a command that will shift the high value 16 bits to the left and add it to the low value:
int byte1;
int byte2;
int byte3;
int byte4;
float val;
} myUnion;
In this union "byte1", 2, 3 and 4 all overlap the same memory location.
It's also misleading to name these "byteX" since they are int sized.
You would want something like:
union {
int as_int[2];
long as_long;
byte as_byte[4];
float as_float;
} myUnion;
Then you could say:
union myUnion val;
val.as_float = 3.1415927;
Serial.println(val.as_int[0]);
Serial.println(val.as_int[1]);
// etc.
I am trying to move a float to a device that only reads words. Not sure of the bit order on the other side once moved as a 3rd party will read and display on the screen.
Consulting the ATMega data sheet would show how the float is represented in memory. That seems like a more reasonable thing to do than to try to learn something about how the bits are set by turning LEDs on or off.
Consulting the ATMega data sheet would show how the float is represented in memory. That seems like a more reasonable thing to do than to try to learn something about how the bits are set by turning LEDs on or off.
Sorry to seem to be picking on you this morning Paul, but I would be very surprised if the ATmega datasheet could cover float uses and storage as there is no floating point hardware in a mega device. Surely it's gcc or some avr math library that would define how floats are used and stored in atmega program?
Sorry to seem to be picking on you this morning Paul, but I would be very surprised if the ATmega datasheet could cover float uses and storage as there is no floating point hardware in a mega device.
No problem. The compiler supports floats, so I thought that how a float was stored in memory would have to be documented in the ATMega data sheet. I'll look, first, next time.
moorsb:
I am trying to move a float to a device that only reads words. Not sure of the bit order on the other side once moved as a 3rd party will read and display on the screen.
Do you have a link to the datasheet of the device?
I am reading a DHT11 humidity and temperature in float on a nano with a enj2860 ethernet board. I want to send this data via Modbus TCP
here is an article
I then use a tablet running Telsa Modbus Scada app over wifi and can display modbus floating point.
The usual recommendation for transferring float data is to not worry about float length, format or byte order, and instead convert the floats to a text format. dtostre() will convert floats to a compact scientific notation that can be easily parsed by another system with no loss of range or resolution, and no risk of format or byte order problems.
I then use a tablet running Telsa Modbus Scada app over wifi and can display modbus floating point.
The DHT11 gives only integer values for both T and H (I wrote a lib for DHT series) so using float is not needed - at least not from the DHT11 point of view.