cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to read I2C ADC device MCP3021 with HAL I2C library

WSpar.1
Associate III

Hi all,

Can anyone help me out how I should read out the two bytes 10 bits ADC result from the MCP3021
https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/20001805C.pdf

My address is 0x4C 1001100(RD/WR) 

I'm using the HAL generated library:

HAL_I2C_Mem_Read(&hi2c1, (uint16_t)ZTP_I2C_ADDRESS, 0, 8, (uint8_t*)aRxBuffer, 16, 1000);

It is not very clear to me what the units should be, bits or bytes?

TEK00002.PNG

 

2 REPLIES 2
Pavel A.
Evangelist III

> what the units should be, bits or bytes?

Bytes. By tradition these devices send data in big endian order. Convert the 16-bit data to little endian.

 

TDK
Guru

To read two bytes from the device:

uint8_t data[2];
HAL_I2C_Master_Receive(&hi2c1, 0x4C << 1, data, 2, HAL_MAX_DELAY);
uint16_t value = data[0] << 8 + data[1];
If you feel a post has answered your question, please click "Accept as Solution".