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

这十分完美。但现在我想使用 WiringPi lib 将其转换为 C++。

#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 读取工作正常,但写入不起作用..任何建议!

4

0 回答 0