XMC4500 USB float issue

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
User19171
Level 1
Level 1
Hello everyone,

I have an issue with the USB. I read via I2C a temperature sensor and send the value to the pc. The problem is the float value is wrong. I checked it with the calculator but the decimal places are zero every time.
I send both values MSB and LSB, temperature (float) and a test value (float) to my pc. The test value is correct but the temp value is wrong.

4143.attach

Why I receive 21.0000 and not 21.875 (calc: 1*16+(94/16))?

Here is my code:


#include
#include
#include
#include
#include

#define TEST_SUCCESS 1
#define TEST_FAILED 0

typedef enum MPU6050
{
//ADR = 0x68 << 1, //GY-521 or 0b11010000
//T_C = 0x42, // << 1,
//T_C = 0b1000001,
//GY_x = 0b0100010
GY_x = 0x44

} MPU6050_REGADDR_t;

typedef enum MPC9809
{
ADR = 0x18 << 1, //MPC9809
REG_CONF = 0x01 << 1,
SHUT_UP_DOWN = 0x0100 << 1
} TEMP_SENS;

char mb[200]; // MessageBuffer for sprintf()

void delay(uint32_t);

volatile uint8_t tx_completion_0 = 0, rx_completion_0 = 0;

int main(void)
{
uint32_t i2c_master_fifo_xfer_status = 0;

DAVE_STATUS_t status;

status = DAVE_Init();

USBD_VCOM_Connect();

while(!USBD_VCOM_IsEnumDone());
while(!cdc_event_flags.line_encoding_event_flag);

delay(0xffff);

if(status != DAVE_STATUS_SUCCESS)
{
while(true){}
}

while (true)
{
uint8_t i2c_data_x[1];
uint8_t i2c_temp = 0x00;

I2C_MASTER_Init(&I2C_MASTER_0);

I2C_MASTER_Transmit(&I2C_MASTER_0, true, ADR, &i2c_temp, 1, true);
while (tx_completion_0 == 0);
tx_completion_0 = 0;

i2c_temp = 0x05;

I2C_MASTER_Transmit(&I2C_MASTER_0, true, ADR, &i2c_temp, 1, false);
while (tx_completion_0 == 0);
tx_completion_0 = 0;

I2C_MASTER_Receive(&I2C_MASTER_0,true, ADR, i2c_data_x, 2, true, true);
while(rx_completion_0 == 0);
rx_completion_0 = 0;

if (!i2c_master_fifo_xfer_status)
{
i2c_master_fifo_xfer_status = TEST_SUCCESS;
}
else
{
i2c_master_fifo_xfer_status = TEST_FAILED;
}

float temp = 0;

i2c_data_x[0] = i2c_data_x[0] & 0x1f;

if ((i2c_data_x[0] & 0x10) == 0x10)
{
i2c_data_x[0] = i2c_data_x[1] & 0x0f;
temp = 256 - ((i2c_data_x[0]*16) + (i2c_data_x[1]/16));
}
else
{
temp = (i2c_data_x[0]*16) + (i2c_data_x[1]/16);
}

float test_val = 16.492;

sprintf(mb, "T1 = %d || T2 = %d || value: %f\n", i2c_data_x[0], i2c_data_x[1], test_val);
USBD_VCOM_SendString((int8_t*)mb);
CDC_Device_USBTask(&USBD_VCOM_cdc_interface);

sprintf(mb, "%f \n", temp);
USBD_VCOM_SendString((int8_t*)mb);
CDC_Device_USBTask(&USBD_VCOM_cdc_interface);

delay(0xffffff);
}
return 1;
}

void delay(uint32_t cnt)
{
volatile uint32_t count = cnt;
while (--count)
{

}
}

void EndOfTransmit(void)
{
tx_completion_0 = 1;
}

void EndOfReceive(void)
{
rx_completion_0 = 1;
}



BR David
0 Likes
2 Replies
User17085
Level 1
Level 1
Hallo there!

Not sitting in front of my IDE, so just replying from the top of my head,

change the lines : temp = (i2c_data_x[0]*16) + (i2c_data_x[1]/16);

to : temp = (i2c_data_x[0]*16.0) + (i2c_data_x[1]/16.0);

Your i2c_data_x is an integer and dividing by 16 will do integer division whereas dividing by 16.0 will force it to do floating point division
0 Likes
User19171
Level 1
Level 1
oh yes, I forgot to try 16.0. Thank you for your answer. it works 🙂
0 Likes