convert number to IEEE754 simple presicion (32bit) and split in two integer 16b

hello people

Nice to meet you I would like said to hope to do well and please be patient whit me, I'm new in Arduino world,

I work with my Arduino one, in this project:

1º I'm reading values from analog inputs, from pressure gauges. (works ok)

2º this values are converted to float reg. (works ok)

3º need send to pc over USB but thought Modbus RTU protocol, it's because going to encapsulated software.

with the Modbus library works when using only coils register, but my problems its because I need send to holding registers, and for that I need write two values with 16bits from ieee 754 simple precision.

for example, register 55 (03 holding registers)

au16Data[54] = word2; //low word var int from iee754,
au16Data[55] = word1; //high word

when my value from sensor it's = 12.4

this values in binary is: 01000001 01000110 01100110 01100110

and this two word must be

word1 = 16710;
word2 = 26214;

those dates from excel attached on the bottom, try to write all code but doesn't work because of discrepacy between excel and Arduino code.

in resume, i need calculate for float to word1 and word2

this is part to mi code:
#include <ModbusRtu.h>


  • Modbus object declaration
  • u8id : node id = 0 for master, = 1..247 for slave
  • u8serno : serial port (use 0 for Serial)
  • u8txenpin : 0 for RS-232 and USB-FTDI
  • or any pin number > 1 for RS-485
  • Version 05/10/2017 probando funcion para calcular iee754

#define ID 9

Modbus slave(ID, 0, 0);
boolean led;
int8_t state = 0;
unsigned long tempus;
// data array for modbus network sharing
uint16_t au16Data[32]={3}; // first value must be set=3 for holding registers

//variables para conversion
int signo=0; //primer bit
int signoM=-1;
float reg=0;
int exponente=0;
int expoSuma=0; //para los siguientes bits
float num1Abs; //uso valor absoluto
float byte1,byte2,byte3, byte4, aux0,aux1,aux3,aux4,aux6;

int word1,word2, aux2,aux5;

float mantissa;

void setup() {
// define i/o

// start communication
slave.begin( 19200 );
tempus = millis() + 100;
digitalWrite(13, HIGH );

void calcular(float num1){

//Convert Float to two words IEEE 754
if (num1>=0)

mantissa = (num1*signoM)/pow(2,exponente);

byte1 = (exponente+127)>>1; //para este caso convierte 130 en 65
aux0 = round((mantissa -1)*8388608);
aux1 = aux0/256;
aux2 = aux1;
aux3 = (aux1 - aux2)*256;

aux4 = aux2 /256;
aux5 = aux4;
aux6 = (aux4 - aux5)*256;

byte2 = aux1/256;

byte3 = aux3;
byte4 = aux6;

word1 = word(byte1,byte2);
word2 = word (byte3,byte4);

//End Convert


void loop() {

// poll messages
// blink led pin on each valid message
state = slave.poll( au16Data, 64 ); //( , +-cantidad de registros)
if (state > 4) {
tempus = millis() + 50;
digitalWrite(13, HIGH);
if (millis() > tempus) digitalWrite(13, LOW );

// get data


//grabo los valores calculados

au16Data[54] = word2; //low word var int from iee754,
au16Data[55] = word1; //high word


// diagnose communication
au16Data[6] = slave.getInCnt();
au16Data[7] = slave.getOutCnt();
au16Data[8] = slave.getErrCnt();

//for check number from 0 to 100

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Hi, have you found a solution?