Search code examples
cstm32i2cstm32cubeide

How to convert data from LM73 temperature register to print out the answer?


I would like to get data(temperature) out of LM73 and print it over UART. However i can't get temperature with commas. I don't know what i should do in C. I have received data from LM73 and written into buffer. LM73 temperature register size is 16 bits.

My buffer is this:

uint8_t rx_buf[2];

Now i would like to get temperature into double or some form to print it out over UART. I can only make it work with integers. But i would like to use commas as well and show positive or negative value. How to achieve it? My C knowledge is limited. I think i need to use float or double. But it should be signed number.

Im thinking if i could just shift bits like this:

int16_t temperature = (rx_buf[0] << 8) | rx_buf[1];

But i understund it can't work because int16_t is integer.

Picture of LM73 temperature register

I dont know how to move forward. My current code prints out only integer value without sign:

void lm73_read_temp(void)
{
    uint8_t rx_buf[2];
    uint8_t rx_len = 2;

    uint8_t temp_buf[1];

    I2C_MasterReceiveData(&I2C1Handle, rx_buf, rx_len, SLAVE_ADDR);

    temp_buf[0] = rx_buf[0] << 1; //shifting out LM73 SIGN bit(D15)

    temp_buf[0] = temp_buf[0] | (rx_buf[1] >> 7); // adding LM73 bit D7 to get integer value

    //creating array to store char values after conversion with sprintf
    char uart_buffer[10];

    //initializing array with 0
    memset(uart_buffer, 0, sizeof(uart_buffer)/sizeof(uart_buffer[0]));


    sprintf(uart_buffer, "%d", temp_buf[0]);

    //printing values out with UART
    for(int i = 0; i < 5; i++)
    {
        USART_write(uart_buffer[i]);
    }
}

Solution

  • According to https://github.com/ZakKemble/LM73/blob/master/arduino/LM73/LM73.cpp

    double temperature = ((data[0]<<8) | data[1]) * 0.0078125;