14 Bit conversion in HEX - MCP2515 and MAX31855

Hello everyone!

Referring to the MAX31855 board, the signed value of the temperature is given by 14bits bits [31:18].
Those 14 bits are sent out MSB first, and the first bit is the sign bit as shown in the attached memory map.

Those are the example values:

Example values

I would like to place the value in the first two slot of a 8 slot array, that will be sent out via CAN with a MCP2515 module.

Actually i’m studying this code, that is the send example of the MCP_CAN_lib: Library

// CAN Send Example
//

#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN0(10);     // Set CS to pin 10

void setup()
{
  Serial.begin(115200);

  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  else Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);   // Change to normal mode to allow messages to be transmitted
}

byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

void loop()
{
  // send data:  ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
  byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
  if(sndStat == CAN_OK){
    Serial.println("Message Sent Successfully!");
  } else {
    Serial.println("Error Sending Message...");
  }
  delay(100);   // send data per 100ms
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

Any help, and suggestion about best libraries to use, is super appreciated!

can you extract the temperature from the MAX31855 record OK

horace:
can you extract the temperature from the MAX31855 record OK

Sorry but i haven't understood...
Yes, for sure i can extract the value from the record.

But once i have the temperature extracted in signed decimal floating point value, how i can convert it in hex?

Banfotech:

I would like to place the value in the first two slot of a 8 slot array, that will be sent out via CAN with a MCP2515 module.

try something like this in your code…

float x = 32.5;
uint8_t *ptr = (uint8_t*)&x;

for(uint8_t i=0; i<sizeof(float);++i){
   data[i] = ptr[i];
}

hope that helps

you can use memcpy(), e.g. copying a 16 bit int and a 32 bit float

int main(void)
{
    byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
    short int i=2049, j;
    memcpy(data, &i, 2);
    printf("data %x %x\n", data[0], data[1]);
    memcpy(&j, data, 2);
    printf("j %d\n", j);
    float pi=3.14159, x;
    memcpy(data, &pi, 4);
    printf("data %x %x %x %x\n", data[0], data[1], data[2], data[3]);
    memcpy(&x, data, 4);
    printf("x %f", x);
}

gives

data 1 8
j 2049
data d0 f 49 40
x 3.141590

I assume you are sending this data to another processor
you may run into problem with diffent data type lengths and the endianness being different

this may help with the canbus coding

I’ll try to be more clear about my need. I have to get the value that I’m already getting from the sensor, already converted in decimal format and “put” the value in the first two bytes of the can massage.
This will allow me to put up to 4 different sensor on one message. This on the sender side, on the receiver side, I have to “decode” the message and split the 4 results.
Something like this:
0000 | 0000 | 0000 | 0000
value 1 | value 2 | value 3 | value 4.

I’m also using the MAX31855 library from Rob Tillaard to manage communication with the module and get the result.

in post #2 yoi stated that the data was a signed decimal floating point value which would be typically 4 bytes
however if you are happy with one decimal place for the fractional component you can convert it to an int, e.g.

  short itemp=(short) (temp*10.0f);

and transmit it as two bytes as shown in post #2
at the receiver convert it back

 float x=(float)itemp/10.0f;

for a more limited temperature range you could multiply by 100 to give two decimal places for the fractional component

alternativly transmit the original data from the MAX31855 and convert the 14bit value at the receiver