树莓派I2C总线连接M24512 EEPROM:Python代码转C++
Alright, let's convert that Python smbus code to C++ for your Raspberry Pi and M24512 EEPROM. Here's a straightforward implementation using Linux's native I2C interface (which is what the Python smbus module uses under the hood):
Convert Python SMBus EEPROM Code to C++
First, let's recap what your original Python code does:
- Initializes I2C bus 1
- Writes 5 bytes (50-54) starting at EEPROM address 0x0000
- Resets the read pointer to address 0x0000
- Reads and prints 4 consecutive bytes from the EEPROM
Here's the equivalent C++ implementation:
#include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> #include <iostream> #include <cstdint> int main() { // Open I2C bus 1 (/dev/i2c-1) int i2c_fd = open("/dev/i2c-1", O_RDWR); if (i2c_fd < 0) { std::cerr << "Error: Could not open I2C bus" << std::endl; return 1; } // Set target I2C device address (0x50 for your M24512) if (ioctl(i2c_fd, I2C_SLAVE, 0x50) < 0) { std::cerr << "Error: Failed to set I2C slave address" << std::endl; close(i2c_fd); return 1; } // Prepare data to write: low address byte + 5 data bytes (50-54) // The first parameter to i2c_smbus_write_i2c_block_data is the high address byte (0x00) uint8_t write_buffer[] = {0x00, 50, 51, 52, 53, 54}; if (i2c_smbus_write_i2c_block_data(i2c_fd, 0x00, sizeof(write_buffer)/sizeof(write_buffer[0]), write_buffer) < 0) { std::cerr << "Error: Failed to write block data to EEPROM" << std::endl; close(i2c_fd); return 1; } // Wait for EEPROM write cycle to complete (M24512 max write time is 5ms; 0.5s is very conservative) usleep(500000); // Set read pointer to address 0x0000 (send high address 0x00 + low address 0x00) uint8_t read_addr_buffer[] = {0x00}; if (i2c_smbus_write_i2c_block_data(i2c_fd, 0x00, sizeof(read_addr_buffer)/sizeof(read_addr_buffer[0]), read_addr_buffer) < 0) { std::cerr << "Error: Failed to set read address" << std::endl; close(i2c_fd); return 1; } usleep(500000); // Read and print 4 consecutive bytes (address auto-increments after each read) for (int i = 0; i < 4; ++i) { int read_byte = i2c_smbus_read_byte(i2c_fd); if (read_byte < 0) { std::cerr << "Error: Failed to read byte from EEPROM" << std::endl; close(i2c_fd); return 1; } std::cout << read_byte << std::endl; usleep(500000); } // Cleanup close(i2c_fd); return 0; }
How to Compile & Run:
- Install required I2C development headers first:
sudo apt-get update && sudo apt-get install libi2c-dev - Compile the code:
g++ -o eeprom_test eeprom_test.cpp -li2c - Run the program (needs root privileges or membership in the
i2cgroup):sudo ./eeprom_test- To avoid using
sudoevery time, add your user to thei2cgroup:
Log out and back in for this change to take effect.sudo usermod -aG i2c $USER
- To avoid using
Key Notes:
- The M24512 uses a 16-bit address, so we send the high address byte (0x00) as the "register" parameter to
i2c_smbus_write_i2c_block_data, followed by the low address byte and data. - The 0.5s delays match your original Python code—they're way longer than the EEPROM's actual 5ms max write cycle, but ensure reliable operation.
i2c_smbus_read_byte()automatically increments the EEPROM's internal address pointer after each read, so each subsequent call gets the next byte in sequence.
内容的提问来源于stack exchange,提问作者Superman
相关产品推荐
相关产品推荐

