Skip to main content
WSpar.1
Associate III
February 2, 2024
Question

Trying to read I2C ADC device MCP3021 with HAL I2C library

  • February 2, 2024
  • 2 replies
  • 1396 views

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

 

This topic has been closed for replies.

2 replies

Pavel A.
Super User
February 2, 2024

> 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
Super User
February 2, 2024

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""."