This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: Code Composer Studio
Hi
I am trying to have single i2c function to read single or multiple bytes. Below is a part of the code
unsigned char * i2c_data;
unsigned char RX_Data; unsigned char * i2c_read(unsigned char slv_addr, unsigned char reg_addr, unsigned int len){ unsigned char data[36]; unsigned int i = 0; while(UCB1STAT & UCBBUSY); UCB1I2CSA = slv_addr; UCB1CTLW0 |= UCTR | UCTXSTT; while(UCB1CTLW0 & UCTXSTT); UCB1TXBUF = reg_addr; while(!(UCB1IFG & UCTXIFG0)); UCB1CTLW0 &= ~UCTR; UCB1CTLW0 |= UCTXSTT; while(UCB1CTLW0 & UCTXSTT); for(i = 0; i < len; i++) { if(i == len - 1) { UCB1CTLW0 |= UCTXSTP; } while(!(UCB1IFG & UCRXIFG0)); data[i] = UCB1RXBUF; } while(UCB1CTLW0 & UCTXSTP); return(data); }
When I check for the read value against the desired value, it is true but when I try to print the value, I am getting junk.
i2c_data = i2c_read_bytes2(BME280_I2C_ADDRESS, BME280_CHIP_ID_ADDR, 1); if(i2c_data[0] == 0x60) { uart_puts("Chip Address is: "); sprintf(RX_Data, "%x", i2c_data); uart_puts(RX_Data); uart_puts("\r\n"); }
It is printing 2Ðð instead of 60
Regards
Vijay
Hello,
Vijay Kolaventy said:When I check for the read value against the desired value, it is true
That's a good start.
Vijay Kolaventy said:when I try to print the value, I am getting junk.
This must be related to how the characters are converted to ASCII or something similar. Perhaps it's because "i2c_data" array is declared as a "char" when you're using the "%x" format flag for an unsigned hexadecimal integer. You could try declaring the array as an unsigned integer.
Regards,
James
Thank you. This resolved the issue. However, I also had to write some code to convert the integer to text to send it through uart with which it was send ascii characters.
**Attention** This is a public forum