0

Currently, I have two, 2-Channel 1 Amp SPDT Signal Relay Controllers connected to my Raspberry Pi 3 via I2C, and when I currently run the function to turn one relay on, the other one will shut off at the same time (one or the other is on). In addition, my button to shut off relay 1 and my button to shut off relay 2 will shut both relays off.

My program is written in a windows form (visual studio), and I am accessing a C Shared Library via Dll Import, but I know for a fact my problem is within my C library. I am very new to C and how shifting works, so the root of my problem lies within the logic and structure of my code. Frankly, I'm confused on how to properly code this.

This is currently the method to turn Relay 1 On. It turns the relay on properly, but this also turns Relay 2 Off at the same time.

void Relay1On() ***CURRENTLY TURNS OTHER OFF WHEN ACTIVATED***
{
    // Create I2C bus
    int file;
    char *bus = "/dev/i2c-1";
    if ((file = open(bus, O_RDWR)) < 0) 
    {
        printf("Failed to open the bus. \n");
        exit(1);
    }
    // Get I2C device, MCP23008 I2C address is 0x20(32)
    ioctl(file, I2C_SLAVE, 0x20);

    // Configure all pins of port as output (0x00)
    char config[2] = {0};
    config[0] = 0x00;
    config[1] = 0x00;
    write(file, config, 2);

    //Turn the first relay on
    char data = 0x01;
    config[0] = 0x09;
    config[1] = data;
    write(file, config, 2);

}

Here is the code for Relay 1 Off, I will not post Relay 2 On/Off because it is basically the same, Relay2On just has an added data += 1; after char data = 0x01;. Both 'Off' methods result in both relays shutting off.

void Relay1Off()
{
    // Create I2C bus
    int file;
    char *bus = "/dev/i2c-1";
    if ((file = open(bus, O_RDWR)) < 0) 
    {
        printf("Failed to open the bus. \n");
        exit(1);
    }
    // Get I2C device, MCP23008 I2C address is 0x20(32)
    ioctl(file, I2C_SLAVE, 0x20);

    // Configure all pins of port as output (0x00)
    char config[2] = {0};
    config[0] = 0x00;
    config[1] = 0x00;
    write(file, config, 2);

    //Turn the first relay off *****Turns all off at the moment******
    char data = 0xFE;
    data = (data << 1);
    config[0] = 0x09;
    config[1] = data;
    write(file, config, 2);
}

All I want is the methods to do as described, turn Relay 1 On when the method is called. When Relay1Off is called, shut only Relay 1 off. I'm sure it is simple, but as I've stated above C is quite new to me.

Thank you in advance for any contribution.

4

1 回答 1

0

我不知道花哨的ioctl东西是如何工作的,但我会尝试在这个函数之外进行所有初始化,包括将所有 GPIO 设置为输出。

您可能应该只调用一个函数来设置/清除继电器。我会做这样的事情开始:

void RelayOnOff(unsigned char relay, unsigned char enable)
{
    //Init to all off
    static unsigned char data = 0x00;
    ...  
    if (enable){
        data |= ( 1 << relay );
    }
    else{
        data &= ~( 1 << relay );
    }
    config[0] = 0x09;
    config[1] = data;
    write(file, config, 2);
}

您传入要控制的继电器,以及启用/禁用的布尔值。如果您将数据变量设为静态,它会从函数调用到函数调用“记住”该值。启用/禁用设置/清除您传入的任何继电器 (0-7) 的位。

于 2019-07-17T22:02:29.833 回答