+ Reply to Thread
Results 1 to 3 of 3

Thread: XMC4500 USB float issue

  1. #1
    New Member New Member kirsch_dk is on a distinguished road
    Join Date
    Mar 2020
    Posts
    3
    Points
    45

    XMC4500 USB float issue

    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.

    Click image for larger version

Name:	i2c_temp.jpg
Views:	0
Size:	52.3 KB
ID:	4583

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

    Here is my code:

    Code:
    #include <DAVE.h> 
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdbool.h>
    
    #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

  2. #2
    Beginner Beginner Gertvb is on a distinguished road
    Join Date
    May 2019
    Location
    South Africa
    Posts
    6
    Points
    82.5
    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

  3. #3
    New Member New Member kirsch_dk is on a distinguished road
    Join Date
    Mar 2020
    Posts
    3
    Points
    45
    oh yes, I forgot to try 16.0. Thank you for your answer. it works

+ Reply to Thread
Disclaimer

All content and materials on this site are provided “as is“. Infineon makes no warranties or representations with regard to this content and these materials of any kind, whether express or implied, including without limitation, warranties or representations of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. No license, whether express or implied, is granted by Infineon. Use of the information on this site may require a license from a third party, or a license from Infineon.


Infineon accepts no liability for the content and materials on this site being accurate, complete or up- to-date or for the contents of external links. Infineon distances itself expressly from the contents of the linked pages, over the structure of which Infineon has no control.


Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Usage Terms of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Usage Terms of this site. Infineon reserves the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.