INACKS
  • Welcome to the IS4310 Modbus Chip Wiki
  • Datasheet
    • Presentation
    • Description
      • Function Codes
      • Exception Codes
      • IS4310 Advantages
      • Modbus UART Port
    • Pin Description
    • Memory Description
      • Memory Map Table
      • HOLDx Registers
      • MBADR Register
      • MBBDR Register
      • MBPAR Register
      • MBSTP Register
    • I2C-compatible Bus Description
      • Single Word Read
      • Multiple Word Read
      • Single Word Write
      • Multiple Word Write
  • Hardware Examples
    • RS485 Design Example
    • Isolated RS485 Design Example
    • Bus Topology
    • Cable Wiring
  • Firmware Examples
    • Arduino Example
    • STM32 Reading Example
    • STM32 Writing Example
  • Appendix
    • Mechanical Drawings
    • Ordering
    • Product Selection Guide
    • Documentation Feedback
    • Customization
    • Legal
Powered by GitBook
On this page
  1. Firmware Examples

STM32 Reading Example

This source code is provided as-is, without any warranties, express or implied, including but not limited to fitness for a particular purpose or merchantability. The user assumes full responsibility for testing, validation, and ensuring compliance with all applicable regulations and standards.

uint8_t IS4310_I2C_Chip_Address; // This variable stores the I2C chip address of the IS4310.
IS4310_I2C_Chip_Address = 0x11; // The IS4310's I2C address is 0x11.
// The STM32 HAL I2C library requires the I2C address to be shifted left by one bit.
// Let's shift the IS4310 I2C address accordingly:
IS4310_I2C_Chip_Address = IS4310_I2C_Chip_Address << 1;

uint16_t Holding_Register_Address_To_Read; // This variable stores the holding register address we want to read from the IS4310.
Holding_Register_Address_To_Read = 500; // We will read address 0, which contains the Modbus Slave ID.

// The following array will store the read data.
// Since each holding register is 16 bits long, reading one register requires reading 2 bytes.
uint8_t readResultArray[2];


/*
 * This is the HAL function to read from an I2C memory device. The IS4310 is designed to operate as an I2C memory.
 *
 * Parameters:
 * 1. &hi2c1: This is the name of the I2C that you're using. You set this in the CubeMX. Don't forget the '&'.
 * 2. IS4310_I2C_Chip_Address: The I2C address of the IS4310 (must be left-shifted).
 * 3. Holding_Register_Address_To_Read: The holding register address to read from the IS4310.
 * 4. I2C_MEMADD_SIZE_16BIT: You must indicate the memory addressing size. The IS4310 memory addressing is 16-bits.
 *  This keyword is an internal constant of HAL libraries.
 * 5. readResultArray: An 8-bit array where the HAL stores the read data.
 * 6. 2: The number of bytes to read. Since one holding register is 16 bits, we need to read 2 bytes.
 * 7. 1000: Timeout in milliseconds. If the HAL fails to read within this time, it will skip the operation
 *    to prevent the code from getting stuck.
 */
HAL_I2C_Mem_Read(&hi2c1, IS4310_I2C_Chip_Address, Holding_Register_Address_To_Read, I2C_MEMADD_SIZE_16BIT, readResultArray, 2, 1000);

/*
 * At this point, we have the Modbus Slave ID stored in the readResultArray.
 * The upper 8 bits of the holding register are stored in readResultArray[0],
 * and the lower 8 bits are stored in readResultArray[1].
 *
 * By default, the Modbus Slave ID is 1, so:
 * - readResultArray[0] will contain 0.
 * - readResultArray[1] will contain 1.
 *
 * Storing the result across two variables isn't ideal, so let's combine them into a single 16-bit variable:
 */
uint16_t readResult;
readResult = readResultArray[0];
readResult = readResult << 8;
readResult = readResult | readResultArray[1];

PreviousArduino ExampleNextSTM32 Writing Example

Last updated 2 months ago