gpt4 book ai didi

通过 SMBus/I2C 更改带有 bcm2835 的 mlx90614 从机地址

转载 作者:行者123 更新时间:2023-12-03 02:10:46 24 4
gpt4 key购买 nike

如何使用 bcm2835 库更改 mlx90614 的从机地址?我尝试过以下代码...

int main()
{
// Buffer, where I store data which I'll send
unsigned char buf[6];

// bcm2835 i2c module intialisation code
bcm2835_init();
bcm2835_i2c_begin();
bcm2835_i2c_set_baudrate(25000);
bcm2835_i2c_setSlaveAddress(0x00);

// For debug purposes, I read what reason codes operations give.
bcm2835I2CReasonCodes why;
bcm2835_i2c_begin();

// function which reads and prints what value eeprom address 0x0e has.
// See below the main.

printf("Initial check\n");
check(); // this time it prints a factory default value 0x5a.

// To access eeprom, the command must start with 0x2X, where x determines the
// address, resulting 0x2e.
buf[0] = 0x2e;

// According to datasheet, I first have to clear the address before
// real write operation.
buf[1] = 0x00;
buf[2] = 0x00;
why = bcm2835_i2c_write(buf,3);
reason(why); // resolves and prints the reason code. This time it prints OK

// according to datasheet, eeprom needs 5ms to make a write operation,
// but I give it 2 seconds.
sleep(2);

// Then I check did the value in eeprom 0x0e change. IT DOESN'T!
printf("Check after clear\n");
check();

// Then I try to write a new address to the eeprom but since the clearing
// the register didn't work, this is very unlikely to work either.
buf[0] = 0x2e;
buf[1] = 0x4b;
buf[2] = 0x00;
why = bcm2835_i2c_write(buf,3);
reason(why);
sleep(2);

// The datasheet says that I have to reset the power supply and after that
// the device should respond to the new slave address.
// I do that by pluging off the jumper wires and reconnecting them
// after the program has finnished.
bcm2835_i2c_end();
return 0;
}

// The function I use to determine what the reason code was.
void reason(bcm2835I2CReasonCodes why)
{
printf("Reason is: ");
if(why == BCM2835_I2C_REASON_OK)
{
printf("OK");
}else if(why == BCM2835_I2C_REASON_ERROR_NACK){
printf("NACK");
}else if(why == BCM2835_I2C_REASON_ERROR_CLKT){
printf("Clock stretch");
}else if(why == BCM2835_I2C_REASON_ERROR_DATA ){
printf("Data error");
}else{
printf("Dunno lol");
}
printf("\n");
return;
}

// Here I read eeprom 0x2e.
void check()
{
unsigned char buf[6];
unsigned char reg = 0x2e;
bcm2835I2CReasonCodes why;
// better safe than sorry with the buffer :)
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
why = bcm2835_i2c_write (&reg, 1);
reason(why);
why = bcm2835_i2c_read_register_rs(&reg,&buf[0],3);
reason(why);
printf("Buffer values are: %x ; %x ; %x \n", buf[0], buf[1], buf[2]);
}

程序的输出如下:

Initial check
Reason is: OK
Reason is: OK
Buffer values are: 5a ; be ; dc
Reason is: OK
Check after clear
Reason is: OK
Reason is: OK
Buffer values are: 5a ; be ; dc
Reason is: OK

如果我在此之后运行 i2cdetect -y 1,该设备不会出现在表中,但它会响应从 0x00 或 0x5a 调用它的程序。我使用了这样的程序后,i2cDetect可以正常从地址0x5a检测到设备。

所以我想真正的问题是,为什么我无法清除并重写 eeprom 0x0e?

Mlx90614 SMBus 通信的说明如下。最相关的页面是 IMO 第 19 页,它实际上给出了我想要做的伪代码示例。 http://www.melexis.com/Assets/SMBus-communication-with-MLX90614-5207.aspx

这是 mlx90614 的数据表 http://www.melexis.com/Assets/IR-sensor-thermometer-MLX90614-Datasheet-5152.aspx

这是 bcm2835 的文档www.airspayce.com/mikem/bcm2835/group__i2c.html

最佳答案

您必须添加一个错误字节。看看这个网站的解释:https://sf264.wordpress.com/2011/03/10/howto-mlx90614-und-pwm/

计算 00002e4b00 的 CRC-8 得到 0xa3

我用这个网站来计算CRC-8:http://smbus.org/faq/crc8Applet.htm

我还没有测试过这个,但我认为这应该有效:

buf[0] = 0x2e;
buf[1] = 0x4b;
buf[2] = 0x00;
buf[3] = 0xa3;
why = bcm2835_i2c_write(buf,4);

关于通过 SMBus/I2C 更改带有 bcm2835 的 mlx90614 从机地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21690360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com