Beside the mentioned input and output ports are two more special types: TristatePort and InterruptPort. A TristatePort is a combined input and output port. An interrupt port is an input port that allows you to run code when the signal on the line changes, on most system the buttons are connected to an interrupt port.
How many GPIO ports are available depends on the device used. Some, but not all, devices allow you to use every type of port on every GPIO port. Also the voltages allowed depend on the used hardware.
To prevent different parts of code read or write to a single GPIO port each port can be declared only once. If you don't use the GPIO port anymore you should Dispose it or use the using keyword.
Contents |
An InputPort allows you to read the signal level from a line. The constructor looks like this:
new InputPort(Cpu.Pin portId, bool glitchFilter, Port.ResistorMode resistor);
The first parameter is the port number. How many ports there are and which to use depends on the device you use. Look in the hardware manual for this. The second parameter allows you to enable a glitch filter. The glitch filter prevents short signal switches to be registered. Hardware switches can "bounce" when they make or break contact which can cause extra signals to be generated. If you pass the value true in the glitchFilter parameter when creating an InputPort, it can "debounce" the attached switch. The exact time that determines whether or not a pair of signals is considered a bounce is controllable with Cpu.GlitchFilterTime value. A pair of signals that appears within the specified interval will be ignored by the InputPort. The third parameter to the InputPort constructor enables or disables a pull-up or pull-down resistor. Which you can use also depends on the device you use.
Example usage:
In the example a new InputPort is created. GPIO Port 10 is used. The glitch filter is disabled and a Pull-up resistor is used.
// Create new InputPortusing (InputPort GPIO_In = new InputPort((Cpu.Pin)10, false, Port.ResistorMode.PullUp))
{ // Read InputPort signal a thousand timesfor (int c = 0; c < 1000; c++)
{if (GPIO_In.Read() == true)
Debug.Print("InputPort is high");
elseDebug.Print("InputPort is low");
// Wait 1/10 secondThread.Sleep(100);
}}// Because we used the using statement the port is now automatically disposed.An OutputPort allows you to write a high or low signal to a line. You could use this to switch a relay, blink an LED, etc. The constructor for an OutputPort looks like this:
new OutputPort(Cpu.Pin portId, bool initialState);
The first parameter is the port number. How many ports there are and which to use depends on the device you use. The second parameter is the initial signal level for the output port.
Example usage:
In the example a new OutputPort is created. It's toggled a thousand times and then frees the GPIO port by calling Dispose.
// Create a new OutputPortOutputPort GPIO_Out = new OutputPort((Cpu.Pin)9, false);
// Switch OutputPort a thousand times (eg. blink an LED)for (int c = 0; c < 1000; c++)
{ // Switch Ouputport valueGPIO_Out.Write(!GPIO_Out.Read());
// Wait 1/10 secondThread.Sleep(100);
}// Free OutputPort GPIO pinGPIO_Out.Dispose();
A TristatePort is a combined input and output port. It allows you to easily switch direction. It has an Active property that controls the output driver. When Active is false the tristate port behaves like an input port, when the Active property is true the tristate port behaves like an output port. The constructor for the tristate port looks like this:
new TristatePort(Cpu.Pin portId, bool initialState, bool glitchFilter, Port.ResistorMode resistor);
The first parameter is the port number. How many ports there are and which to use depends on the device you use. Look in the hardware manual for this. The second parameter sets the output driver initial value. The third parameter allows you to enable a glitch filter. The glitch filter prevents short signal switches to be registered. The exact time is controllable with Cpu.GlitchFilterTime. You can use this filter for debouncing switches. The fourth parameter enables or disables a pull-up or pull-down resistor. Which you can use also depends on the device you use. The TristatePort initially has the Active property set to true and behaves like an output port.
Example usage:
In the example a new TristatePort is created and made an input immediately. Next the program waits until the line goes low and then it becomes an output forcing the line low.
// Create a new TristatePort and make it an inputTristatePort GPIO_InOut = new TristatePort((Cpu.Pin)10, true, false, Port.ResistorMode.PullUp);
GPIO_InOut.Active = false;
// Wait until port signal goes lowwhile (GPIO_InOut.Read() == true) { Thread.Sleep(100); }
// Keep forcing the signal line lowGPIO_InOut.Write(false);
GPIO_InOut.Active = true;
// Infinite sleepThread.Sleep(-1);
The InterruptPort is basically an InputPort with the ability to run user defined code when the signal on the attached switch moves to a specific level or edge. Edges are explained in more detail below. The signal that starts the code is called a trigger. When you use an InputPort as an InterruptPort instead, it's typically to reduce power consumption on the embedded device you're working with. If you were to write a while loop in your code that polls an InputPort constantly looking for changes, it would be very difficult to put the device into a low power state because the while loop would keep the processor busy all the time. The InterruptPort registers your interest in those state changes so you don't have to constantly poll for them. This allows the device to potentially use less power until the trigger occurs. It doesn't mean that it will use less power. It just enables low-power modes to be implemented in the hardware.
Below are the logic levels and edges shown:

When you want to run code when the user presses the button in the example circuit you can trigger on the falling edge.
Below the constructor of the InterruptPort is displayed:
new InterruptPort(Cpu.Pin portId, bool glitchFilter, Port.ResistorMode resistor, Port.InterruptMode interrupt);
The first parameter is the port number. How many ports there are and which to use depends on the device you use. Look in the hardware manual for this. The second parameter allows you to enable a glitch filter. The glitch filter prevents short signal switches to be registered. Hardware switches can "bounce" when they make or break contact which can cause extra signals to be generated. If you pass the value true in the glitchFilter parameter when creating an InputPort, it can "debounce" the attached switch. The exact time that determines whether or not a pair of signals is considered a bounce is controllable with Cpu.GlitchFilterTime value. A pair of signals that appears within the specified interval will be ignored by the InterruptPort. The third parameter enables or disables a pull-up or pull-down resistor. Which you can use depends on the device you use. The fourth parameter defines the trigger for the source code, Which you can use also depends on the device you use.
When the InterruptPort is triggered the OnInterrupt event is fired. In the example you can see how to define a event handler.
Example usage:
In this example a new InterruptPort is created that is triggered on a falling edge. In the event handler a debug message is written.
// Create new InterruptPortInterruptPort GPIO_In2 = new InterruptPort((Cpu.Pin)10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
// When the interrupt happens, call the GPIO_In2_OnInterrupt functionGPIO_In2.OnInterrupt += GPIO_In2_OnInterrupt;
// Wait infinitly for interruptsThread.Sleep(-1);
void GPIO_In2_OnInterrupt(uint data1, uint data2, TimeSpan time)
{Debug.Print("GPIO Interrupt");
}