Thursday, June 25, 2015

How fast can I turn a Nusbio gpio on and off in C#

How fast can I turn a Nusbio gpio on and off in C#?

The oscilloscope says 1 Milli second to turn it on, 1 Milli second to turn it off.
 



How fast can I turn a extra gpio pin on and off in C#?

The extension Ex8G32kE, add an extra 8 gpio pins and a 32k EEPROM to Nusbio.

For the extra gpio pins, I used the MCP23008 gpio expander. The MCP23008 is an I2C device.



To turn on the extra gpio pin, the method DigitalWrite() in the C# class MCP23008, makes 2 I2C calls (GetGPIOMask() and SetGPIOMask()) to turn on the gpio.


public override void DigitalWrite(byte gpioIndex, MadeInTheUSB.GPIO.PinState d)
{
    var mcpIndex = gpioIndex - this.GpioStartIndex;

    // only 8 bits!
    if (mcpIndex > 7) return;

    // read the current GPIO output latches
    int gpio = GetGPIOMask();

    // set the pin and direction
    if (d == GPIO.PinState.High)
    {
        gpio |= (byte)(1 << mcpIndex);
    }
    else
    {
        gpio &= (byte)(~(1 << mcpIndex));
    }
    // write the new GPIO
    SetGPIOMask((byte)gpio);
}

internal int GetGPIOMask()
{
    // read the current status of the GPIO pins
    return this._i2c.Ready1Byte8BitsCommand(MCP230XX_GPIO); 
}

internal bool SetGPIOMask(byte gpio)
{
    return this._i2c.Send2BytesCommand(MCP230XX_GPIO, gpio);
} 
 
How fast can I turn a extra gpio pin on and off in C#?
The oscilloscope says 7.4 milli second to turn it on, 7.4 Milli second to turn it off. 
We can estimate the cost of one I2C call to 3.7 Milli second.



Source Code

No comments:

Post a Comment