.NET Micro Framework Wiki :: GPIO

From .NET Micro Framework Wiki
Jump to: navigation, search
Example circuit to showcase the GPIO ports
Example circuit to showcase the GPIO ports
Many applications require interaction with the outside world. One of the ways to do this is trough GPIO ports. GPIO stands for General Purpose Input/Output. As the name states there are two kinds of GPIO ports, Input ports and Output ports. There ports allow you to read or write a high or low signal on a line. A typical usage case for an output port is controlling a relay. A typical usage for an input port is reading the state of a switch.

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


InputPort

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.

  1. // Create new InputPort
  2. using (InputPort GPIO_In = new InputPort((Cpu.Pin)10, false, Port.ResistorMode.PullUp))
  3. {
  4.   // Read InputPort signal a thousand times
  5.   for (int c = 0; c < 1000; c++)
  6.   {
  7.     if (GPIO_In.Read() == true)
  8.       Debug.Print("InputPort is high");
  9.     else
  10.       Debug.Print("InputPort is low");
  11.     // Wait 1/10 second
  12.     Thread.Sleep(100);
  13.   }
  14. }
  15. // Because we used the using statement the port is now automatically disposed.

OutputPort

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.

  1. // Create a new OutputPort
  2. OutputPort GPIO_Out = new OutputPort((Cpu.Pin)9, false);
  3.  
  4. // Switch OutputPort a thousand times (eg. blink an LED)
  5. for (int c = 0; c < 1000; c++)
  6. {
  7.   // Switch Ouputport value
  8.   GPIO_Out.Write(!GPIO_Out.Read());
  9.  
  10.   // Wait 1/10 second
  11.   Thread.Sleep(100);
  12. }
  13.  
  14. // Free OutputPort GPIO pin
  15. GPIO_Out.Dispose();


TristatePort

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.

  1. // Create a new TristatePort and make it an input
  2. TristatePort GPIO_InOut = new TristatePort((Cpu.Pin)10, true, false, Port.ResistorMode.PullUp);
  3. GPIO_InOut.Active = false;
  4.  
  5. // Wait until port signal goes low
  6. while (GPIO_InOut.Read() == true) { Thread.Sleep(100); }
  7.  
  8. // Keep forcing the signal line low
  9. GPIO_InOut.Write(false);
  10. GPIO_InOut.Active = true;
  11.  
  12. // Infinite sleep
  13. Thread.Sleep(-1);


InterruptPort

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:
Image:EdgesAndLevels.png
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.

  1. // Create new InterruptPort
  2. InterruptPort GPIO_In2 = new InterruptPort((Cpu.Pin)10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
  3.  
  4. // When the interrupt happens, call the GPIO_In2_OnInterrupt function
  5. GPIO_In2.OnInterrupt += GPIO_In2_OnInterrupt;
  6.  
  7. // Wait infinitly for interrupts
  8. Thread.Sleep(-1);
  9.  
  10. void GPIO_In2_OnInterrupt(uint data1, uint data2, TimeSpan time)
  11. {
  12.   Debug.Print("GPIO Interrupt");
  13. }
Personal tools