|
Writing Applications
Now that you have the tools installed, you can being your first application.
We are going to write a simple application that flashes the LED every time the button is pressed.
First we are going to create a new project:
1. Open Visual Studio
2. From the main menu, select File | New | Project...
3. From the New Project Dialog, select Visual C# | Micro Framework | Console Application
4. Choose a name for the application and press OK
Now we can write some code:
5.To talk to the GPIO pins on the Meridian/P we need to add some references. In the Solution Explorer, right-click on References
and select Add Reference...
6. From the list, select DeviceSolutions.SPOT.Hardware.MeridianP, and while holding down the Ctrl key, select Microsoft.SPOT.Hardware, then press OK.
These two items will be added to the References list.
7. We need to included these in the code. From the Solution Explorer window, double-click Program.cs to open the main program file
8. At the top of the file, add:
using DeviceSolutions.SPOT.Hardware;
using Microsoft.SPOT.Hardware;
using System.Threading;
We need System.Threading for the Sleep() function we will use later.
9. Delete the Debug.Print( Resources... statement as this is not required in our application.
10. Now we can define the objects we need to access the LED and Button. Add the following lines at the start of the Main() function:
OutputPort led = new OutputPort( MeridianP.Pins.LED, true);
InputPort button = new InputPort(MeridianP.Pins.SW1, true, Port.ResistorMode.PullUp);
These statement create an OutputPort attached to the LED pin and an InputPort attached to SW1 which is the user button on Meridian/P.
The default state of the led is true, which corresponds to a high signal level with the LED off.
The internal pullup is required for the button to operate. This means a low level on the input will indicate the button is pressed.
11. We will now add the main code loop, and a statement to check the button:
while (true)
{
if(button.Read() == false)
{
}
}
As we mentioned earlier, the GPIO will be low when pressed, so we check to see when it is false.
12. Now to add the code to flash the LED:
led.Write(false);
Thread.Sleep(200);
led.Write(true);
Thread.Sleep(200);
This code turns the LED on for 200ms, then off again.
13. To try it out, connet your Meridian/P to you PC with the USB cable.
14. Right-click on the project in Solution Explorer and select Properties.
15. Select the .NET Micro Framework tab, and then select USB as the Deployment Transport.
You should see "Meridian_a7e70ea2" appear as the device.
16. Press F5 to deploy the application. Press SW1 on the Meridian/P to see the LED flash!
Congratulations, you have written your first application for the Meridian/P!
|