A Complete Program-Our First Flashlight


Listing 4-1 is version 1.0 of our computer-controlled flashlight.

Listing 4-1: A complete .NET Micro Framework flashlight

image from book
 using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; namespace MicroFrameworkFlashlight {    public class MicroFrameworkFlashlight    {        public static void Main()        {           Cpu.Pin lampPin = Cpu.Pin.GPIO_Pin1;           Cpu.Pin switchPin = Cpu.Pin.GPIO_Pin2;           OutputPort lampOutput = new OutputPort(lampPin, false);           InputPort switchInput = new InputPort(switchPin, false,                                       Port.ResistorMode.PullDown);           while (true)           {              lampOutput.Write(switchInput.Read());           }        }    } } 
image from book

This program implements a very simple push-button flashlight. When the input is set to high, the program switches the lamp on. When the Main method is called to start the program, it creates the input and output ports and then enters a loop that continuously copies the value of the switch onto the output port.

Figure 4-3 shows how the flashlight will be wired together. The switch will pull the input pin up to the positive voltage when it is closed. The output pin will produce a high level when the lamp is to be lit. This will cause a current flow through the lamp to ground, causing it to illuminate.

image from book
Figure 4-3: The .NET Micro Framework flashlight circuit.

The program includes the code that defines the pins to be used. The Cpu.Pin type is another enumeration that identifies the range of pins available for the selected hardware. This enumeration will provide a range of values that depends on the underlying hardware. The Pin type is found in the Microsoft.SPOT.Hardware namespace. This sets out the features available on the Microsoft SPOT device. If you use a different hardware platform, or wish to connect the lamp or switch to different pins, you will have to set lampPin and switchPin to the appropriate values. The good news is that this is all you will have to do, because the port instances will be connected to the correct devices when they are created.




Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net