您的位置:首页 > 其它

在.Net Micro Framework中访问硬件 - part1

2008-07-09 23:05 429 查看
在.Net Micro Framework中访问硬件 - part1


摘要:本文介绍了.Net Micro Framework中对硬件的简单而独特的访问方式。涉及GPIO,RS232串口等。通过简明的例程说明了如何创建IO口,如何发送接受数据的过程。

Keywords:

.Net Micro framework, GPIO, RS232, Embedded, C#

[b]

1.GPIO
[/b]

通常来说,一块MCU要与周边环境进行交流,使用GPIO(General Purpose Input/Output)无疑是最常用的方式。一个GPIO口在被初始化之后可以被用于输入或输出的通道。一个GPIO口可以由两种状态来描述 - 低(约为0伏)和高(通常认为是3.3伏的正向电压)。

在.Net Micro Framework中,GPIO的状态被定义为布尔型,false->低, true->高。

Tips这里说的低(0伏)和高(3.3伏)是指你在设置GPIO的时候,实际加在GPIO口的电压。而在考虑输入的时候一般1v以下会被认为是逻辑低,1.7~5.5伏通常认为是逻辑高。超过5.5的电压是如果不加保护电路通常是会损坏你的硬件的。

1.1输出

在Microsoft.SPOT.Hardware命名空间下,你可以找到OutputPort类,它继承自Microsoft.SPOT.Hardware.Port---一个用于描述GPIO的基础类。

定义OutputPort一般都会初始化一个默认值(true代表高,false表示低)。

OutputPort outputPort = new OutputPort(MyPins.StatusLED, true);

/*第一个参数是枚举类型Microsoft.SPOT.Hardware.Cpu.Pin,不过为了使你的代码更灵活,非常建议你使用自己封装的类来绑定CPU的管脚名称和GPIO口的编号。*/

接着,outputPort的Write和Read方法就可以使用了,Write方法控制了管脚的电平状态,Read方法用于返回当前状态,也即上一次设置的状态。

下面的例子用于让自定义pin的led按1hz的频率闪烁(实际上是亮暗各0.5s左右)。

OutputPort outputPort = new OutputPort(MyPins.StatusLED, true);

while (true)

using System;

using System.Threading;

using Microsoft.SPOT;

using Microsoft.SPOT.Hardware;



InputPort inputPort = new InputPort(Cpu.Pin.GPIO_Pin2,//管脚号

false,//是否过滤毛刺信号

Port.ResistorMode.PullDown);//电阻模式,这里设为下拉即合上开关(按下按钮)时GPIO状态为高

while (true)

using System;

using System.Threading;

using Microsoft.SPOT;

using Microsoft.SPOT.Hardware;



InterruptPort port =

new InterruptPort(Cpu.Pin.GPIO_Pin3,

false, //不过滤毛刺

Port.ResistorMode.PullDown,

Port.InterruptMode.InterruptEdgeBoth);//中断模式设为双边沿触发

port.OnInterrupt += new GPIOInterruptEventHandler(port_OnInterrupt);//指定中断服务程序

Thread.Sleep(Timeout.Infinite);



private static void port_OnInterrupt(Cpu.Pin port,

bool state,

TimeSpan time)



interruptPort =

new InterruptPort(Cpu.Pin.GPIO_Pin2,false, Port.ResistorMode.PullUp,

Port.InterruptMode.InterruptEdgeLevelLow);//中断模式设置为低电平触发

interruptPort.OnInterrupt +=

new GPIOInterruptEventHandler(port_OnInterrupt);

Thread.Sleep(Timeout.Infinite);//除了等待中断以外,该线程休眠



private static void port_OnInterrupt(Cpu.Pin port,

bool state,

TimeSpan time)

using System;

using System.Text;

using System.Threading;

using Microsoft.SPOT;

using Microsoft.SPOT.Hardware;



SerialPort.Configuration config =

new SerialPort.Configuration(SerialPort.Serial.COM1,

SerialPort.BaudRate.Baud9600,

false);//不做任何流控制

SerialPort serialPort = new SerialPort(config);

byte[] outBuffer = Encoding.UTF8.GetBytes("Hello World!"r"n");

serialPort.Write(outBuffer, 0, outBuffer.Length);//MF中无需先Open再写入

serialPort.Dispose();

//keeps the emulator running to see results

Thread.Sleep(Timeout.Infinite);

读数据的方法也和PC上差不多:

SerialPort.Configuration config =

new SerialPort.Configuration(SerialPort.Serial.COM1,

SerialPort.BaudRate.Baud115200,

false);

SerialPort serialPort = new SerialPort(config);

byte[] inBuffer = new byte[32];

while (true)

internal sealed class MyHardwareProvider : HardwareProvider

//注册HWP

HardwareProvider.Register(new MyHardwareProvider());

SerialPort.Configuration config =

new SerialPort.Configuration(SerialPort.Serial.COM1,

SerialPort.BaudRate.Baud9600,

false);

//如果此时serialPortB从 config创建时,serialPortA尚未释放的话,会导致运行时错误。

SerialPort serialPortA = new SerialPort(config);

//SerialPort serialPortB = new SerialPort(config); //出错,因为这时COM1被A占用



推荐资源:

MS .Net Micro Framework team

Windows Embedded blog From china

MVP 刘洪峰的blog

.Net MF国内的资源站点Winbile.Net(正在改版)

[书籍]Embedded Programming with the .Net Micro Framework

[书籍]Expert .Net Micro Framework

My first .net mf app on Freescale iMX!! 图中是我的第一个mf程序,在iMX上显示了一张它的照片(240*320):



enjoy it!

黄季冬
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: