示例很简单,主要是按钮和LED灯的测试,红牛和EM-STM3210E的按钮和LED有所不同,所以有针对性的对二者pin都做了定义(这些信息,你可以直接看开发板相对应的原理图,很容易就可以获得相关Pin脚的定义)。
功能就是 LED每隔1s就亮灭一次,按相关按键,则从调试口输出按键信息。
核心代码如下:
#define Redox //红牛开发板
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;
namespace GPIOTest
{
public class Program
{
enum GPIO_NAMES
{
PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15,
PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15,
PC0, PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10, PC11, PC12, PC13, PC14, PC15,
PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15,
PE0, PE1, PE2, PE3, PE4, PE5, PE6, PE7, PE8, PE9, PE10, PE11, PE12, PE13, PE14, PE15,
PF0, PF1, PF2, PF3, PF4, PF5, PF6, PF7, PF8, PF9, PF10, PF11, PF12, PF13, PF14, PF15,
PG0, PG1, PG2, PG3, PG4, PG5, PG6, PG7, PG8, PG9, PG10, PG11, PG12, PG13, PG14, PG15
};
public static void Main()
{
#if Redox
//wakeup - PA0 tamper- PC13 user1-PA8 user2- PD3
Cpu.Pin[] Button_Pins = new Cpu.Pin[] { (Cpu.Pin)GPIO_NAMES.PA0, (Cpu.Pin)GPIO_NAMES.PC13, (Cpu.Pin)GPIO_NAMES.PA8, (Cpu.Pin)GPIO_NAMES.PD3};
//LED
Cpu.Pin[] LED_Pins = new Cpu.Pin[] { (Cpu.Pin)GPIO_NAMES.PF6, (Cpu.Pin)GPIO_NAMES.PF7, (Cpu.Pin)GPIO_NAMES.PF8, (Cpu.Pin)GPIO_NAMES.PF9, (Cpu.Pin)GPIO_NAMES.PF10 };
#else
//user - PG8 up - PG13 left - PG14 right - PG15 down - PD3 sel - PG7
Cpu.Pin[] Button_Pins = new Cpu.Pin[] { (Cpu.Pin)GPIO_NAMES.PG8, (Cpu.Pin)GPIO_NAMES.PG13, (Cpu.Pin)GPIO_NAMES.PG14, (Cpu.Pin)GPIO_NAMES.PG15, (Cpu.Pin)GPIO_NAMES.PD3, (Cpu.Pin)GPIO_NAMES.PG7 };
//LED
Cpu.Pin[] LED_Pins = new Cpu.Pin[] { (Cpu.Pin)GPIO_NAMES.PF6, (Cpu.Pin)GPIO_NAMES.PF7, (Cpu.Pin)GPIO_NAMES.PF8, (Cpu.Pin)GPIO_NAMES.PF9 };
#endif
InterruptPort[] button = new InterruptPort[Button_Pins.Length];
for (int i = 0; i < button.Length; i++)
{
button[i] = new InterruptPort(Button_Pins[i], false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeBoth);
button[i].OnInterrupt += new NativeEventHandler(Program_OnInterrupt);
}
OutputPort[] led = new OutputPort[LED_Pins.Length];
for (int i = 0; i < led.Length; i++)
{
led[i] = new OutputPort(LED_Pins[i], false);
}
while (true)
{
for (int i = 0; i < led.Length; i++)
{
led[i].Write(!led[i].Read());
}
Thread.Sleep(1000);
}
}
static void Program_OnInterrupt(uint data1, uint data2, DateTime time)
{
Debug.Print(data1.ToString() + ":" + data2.ToString());
}
}
}