简体   繁体   English

如何在mbed上的同一引脚上从I2C切换到OneWire?

[英]How can I switch from I2C to OneWire on the same pin on mbed?

I have a project which involves talking to OneWire chips (DS2431) through a I2C switch (PCA9548). 我有一个项目涉及通过I2C开关(PCA9548)与OneWire芯片(DS2431)通信。 What I want to do is configure the switch properly (just write a byte to its state register) then use the mbed SDA pin for OneWire communication. 我想要做的是正确配置开关(只需将一个字节写入其状态寄存器),然后使用mbed SDA引脚进行OneWire通信。 The switch doesn't care about the SDA line (it can even pass DC in both directions) and I've tested it with OneWire successfully. 交换机不关心SDA线路(它甚至可以在两个方向上通过DC),我已经成功地使用OneWire进行了测试。 The problem is switching in software from I2C to OneWire on the same pin. 问题是在同一引脚上将软件从I2C切换到OneWire。
I've tried it the easy way : making a global I2C instance, then a OneWire instance, but the last one always busts the previous one so that I can either have I2C working or OneWire. 我尝试了一种简单的方法:先创建一个全局I2C实例,然后创建一个OneWire实例,但是最后一个实例总是破坏前一个实例,以便我可以使用I2C或OneWire。 Is there a way to destruct one instance and create it again? 有没有办法破坏一个实例并再次创建它?

You can go about it in a few ways, but one of the simpliest ways is to declare the I2C and OneWire instance inside of your main function. 您可以通过几种方法进行处理,但是最简单的方法之一是在主函数中声明I2C和OneWire实例。

If you need to access the OneWire instance outside of main, you can assign it to a pointer. 如果需要访问main之外的OneWire实例,可以将其分配给指针。

Here's some pseudocode: 这是一些伪代码:

OneWire *oneWireGlobal;

void func1() {
    oneWireGlobal->writeBit(0xFF);
}

void main() {
    I2C i2c(I2C_SDA, I2C_SCL);

    [I2C operations here...]

    OneWire oneWire(I2C_SDA);
    oneWireGlobal = &oneWire; // Be sure to do this before accessing "oneWireGlobal"

    [oneWire operations here...]

    func1();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM