To set the bit high and turn the LED on, use:
The InterruptPort class supports events that can
respond to a user action or other event at a port bit. On
the ChipworkX board, pin PA23 connects to a pushbutton.
This constructor defines an InterruptPort object on the
InterruptPort port = new InterruptPort(
Setting the second parameter as true enables a glitch
filter that ignores switch bounce on mechanical switches.
The ResistorMode parameter can set a chip’s internal
resistor — if available — to pull-up, pull-down, or no
resistor. The InterruptMode parameter specifies whether to
trigger an event on a rising edge, falling edge, both, or a
high or low level.
The OnInterrupt method assigns a routine to execute
when an interrupt event occurs:
The parameters passed to the port_OnInterrupt
routine identify the port and port state, and provide a
TimeSpan value that indicates when the event occurred.
The routine below responds to a switch press by toggling
Led1. The routine reads the state of Led1 and writes the
opposite state back to the bit. A Debug statement displays
the values of the passed parameters in Visual Studio’s
private static void port_OnInterrupt(
uint port, uint state, TimeSpan time)
Debug.Print(“Pin “ + port +
“““ State= “ + state +
“““ Time= “ + time);
On pressing button PA23, the LED’s state toggles and
the Output window displays the values of the parameters:
Pin 23 State= 1 Time= 00:00:42.2742919
48 April 2010
FIGURE 3. The provided software emulator includes a
“live” image of the development board as it runs your
.NET Micro Framework code.
Accessing Serial Ports
To access RS-232 ports, you can use a SerialPort
class that is similar to the SerialPort class in the full .NET
Framework. This example creates a SerialPort object, sets
parameters for communicating with a GPS unit, and
opens the port:
private SerialPort comPort = new SerialPort(
“““COM3”, 4800, Parity.None, 8, StopBits.One);
comPort.Handshake = Handshake.None;
The constructor sets the port name (COM3) to match
the name assigned to the RS-232 port on the ChipworkX
board. The port communicates at 4800 bits/sec with no
parity, eight data bits, and one Stop bit. The Handshake
property specifies the type of handshaking (flow control)
— if any — for the port.
After calling the Open method, the application can
read data from the port:
Byte receivedDataBuffer =
The receivedDataBuffer byte array holds the received
data. The Bytes ToRead method returns the number of
bytes waiting in the buffer. The Read method stores the
received data in the buffer. The second parameter (0) is
the beginning array index for storing the data, and
receivedDataBuffer.Length is the number of bytes to read.
The .NET Micro Framework also provides classes for
accessing components with SPI and I2C serial interfaces.
Adding a Flash Drive
To access USB peripherals, a device must have a USB
host port and support for USB host functions. GHI
Electronics provides hardware and software support for
accessing a variety of USB peripherals from .NET Micro
Probably the most popular USB device for small
systems is the Flash drive. Accessing a drive requires three
steps: detecting and initializing the drive; getting directory
and file information; and reading and writing to files.
Jan Axelson is the author of USB Complete and
Serial Port Complete. Her website is www.Lvr.com.