M24512 EEPROM与Raspberry Pi在I2C上的接口:将Python转换为C++。

问题描述 投票:1回答:1

我必须在I2C总线上将M24512 EEPROM IC与Raspberry pi连接。在地址0x50处显示i2cdetect -y 1。

我让它在python-smbus上工作。

import smbus
import time
bus=smbus.SMBus(1)
bus.write_i2c_block_data(0x50, 0x00, [0x00, 50, 51, 52, 53, 54])
time.sleep(0.5)
bus.write_i2c_block_data(0x50, 0x00, [0x00])
time.sleep(0.5)
print bus.read_byte(0x50)
time.sleep(0.5)
print bus.read_byte(0x50)
time.sleep(0.5)
print bus.read_byte(0x50)
time.sleep(0.5)
print bus.read_byte(0x50)
time.sleep(0.5)
print bus.read_byte(0x50)
time.sleep(0.5)
print bus.read_byte(0x50)
time.sleep(0.5)
print bus.read_byte(0x50)
time.sleep(0.5)

显示输出

50
51
52
53
54
255
255

它是完美的.但现在我想把它转换为C++使用WiringPi库。

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int temp=0;
int a=0;
wiringPiSetup();
int fd = wiringPiI2CSetup(0x50);
cout << "file descriptor " << fd << endl ;
wiringPiI2CWriteReg8(fd, 0x00, 0x00);
delayMicroseconds(500000);
cout << wiringPiI2CWrite(fd, 100);
delayMicroseconds(500000);
wiringPiI2CWriteReg8(fd, 0x00, 0x00);
delayMicroseconds(500000);
a=wiringPiI2CRead(fd);
delayMicroseconds(500000);
cout<<a<<endl;
a=wiringPiI2CRead(fd);
delayMicroseconds(500000);
cout<<a<<endl;
a=wiringPiI2CRead(fd);
delayMicroseconds(500000);
cout<<a<<endl;
a=wiringPiI2CRead(fd);
delayMicroseconds(500000);
cout<<a<<endl;
a=wiringPiI2CRead(fd);
delayMicroseconds(500000);
cout<<a<<endl;
a=wiringPiI2CRead(fd);
delayMicroseconds(500000);
cout<<a<<endl;
a=wiringPiI2CRead(fd);
delayMicroseconds(500000);
cout<<a<<endl;

EEPROM读取工作正常,使用上述代码,但写是不工作的...任何建议!!!。

c++ raspberry-pi3 i2c eeprom wiringpi
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.