http://www.cnblogs.com/yefanqiu/archive/2008/07/16/1243978.html
目前针对.Net Micro Framework无论是开发GPIO、SPI、I2C,还是串口通信都离不开硬件,前段时间我扩展了官方的MF模拟器,可以不用硬件,零成本的体验上述技术开发。
要体验MF开发,必须具备如下条件:
Ø PC机上已安装VS2005;
Ø 从http://msdn.microsoft.com/zh-cn/embedded/bb267253(en-us).aspx下载并安装MF 2.5 SDK;
Ø 从http://www.sky-walker.com.cn/MFEmulator_SDK.rar下载模拟器及PPT文档;
Ø 根据说明,在本机上注册该模拟器;
一、模拟器的使用
1、模拟器下载
从http://www.sky-walker.com.cn/MFEmulator_SDK.rar下载模拟器。
2、模拟器注册
压缩包“YFMF模拟器”目录中有一个YFEmulatorReg.exe文件,运行该文件,并选择打开模拟器文件,完成注册。
3、模拟器配置
新建一个MF工程,打开该工程的属性页,在Micro Framework选项中,设定我们扩展的模拟器。
4、启动后的模拟器
二、应用开发示例
1、GPIO测试
模拟器中相关GPIO的PIN值如下:
I0~I7 :Pin=10~17
Q9~Q7 :Pin=20~27
完整的测试代码如下:
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;
namespace GPIOTest
{
public class Program
{
static OutputPort[] output = new OutputPort[ 8 ];
static InputPort[] input = new InputPort[ 8 ];
public static void Main()
{
// 叶帆模拟器GPIO的pin定义
Cpu.Pin[] pin_I = new Cpu.Pin[ 8 ] { (Cpu.Pin) 10 , (Cpu.Pin) 11 , (Cpu.Pin) 12 , (Cpu.Pin) 13 , (Cpu.Pin) 14 , (Cpu.Pin) 15 , (Cpu.Pin) 16 , (Cpu.Pin) 17 };
Cpu.Pin[] pin_Q = new Cpu.Pin[ 8 ] { (Cpu.Pin) 20 , (Cpu.Pin) 21 , (Cpu.Pin) 22 , (Cpu.Pin) 23 , (Cpu.Pin) 24 , (Cpu.Pin) 25 , (Cpu.Pin) 26 , (Cpu.Pin) 27 };
// GPIO
for ( int i = 0 ; i < 8 ; i ++ )
{
input[i] = new InputPort(pin_I[i], false , Port.ResistorMode.PullDown);
output[i] = new OutputPort(pin_Q[i], false );
}
int Index = 0 ;
while ( true )
{
GPIOTest( ref Index);
Thread.Sleep( 200 );
}
}
// GPIO测试
public static void GPIOTest( ref int Index)
{
output[Index].Write( ! output[Index].Read());
if ( ++ Index > 7 ) Index = 0 ;
string strPace = " " ;
Debug.Print( " Input : I0 I1 I2 I3 I4 I5 I6 I7 " );
Debug.Print( " " + (input[ 0 ].Read() ? " 1 " : " 0 " ) + strPace + (input[ 1 ].Read() ? " 1 " : " 0 " ) + strPace + (input[ 2 ].Read() ? " 1 " : " 0 " ) + strPace + (input[ 3 ].Read() ? " 1 " : " 0 " ) + strPace + (input[ 4 ].Read() ? " 1 " : " 0 " ) + strPace + (input[ 5 ].Read() ? " 1 " : " 0 " ) + strPace + (input[ 6 ].Read() ? " 1 " : " 0 " ) + strPace + (input[ 7 ].Read() ? " 1 " : " 0 " ));
Debug.Print( " Output: Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 " );
Debug.Print( " " + (output[ 0 ].Read() ? " 1 " : " 0 " ) + strPace + (output[ 1 ].Read() ? " 1 " : " 0 " ) + strPace + (output[ 2 ].Read() ? " 1 " : " 0 " ) + strPace + (output[ 3 ].Read() ? " 1 " : " 0 " ) + strPace + (output[ 4 ].Read() ? " 1 " : " 0 " ) + strPace + (output[ 5 ].Read() ? " 1 " : " 0 " ) + strPace + (output[ 6 ].Read() ? " 1 " : " 0 " ) + strPace + (output[ 7 ].Read() ? " 1 " : " 0 " ));
}
}
}
测试结果:
2、SPI测试
模拟器中相关SPI的PIN值如下:
PIN=30
完整的测试代码如下:
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;
namespace SPITest
{
public class Program
{
static SPI _spi;
static int QAW = 0 ;
public static void Main()
{
// SPI的pin定义
_spi = new SPI( new SPI.Configuration((Cpu.Pin) 30 , true , 0 , 0 , false , false , 4000 , SPI.SPI_module.SPI1));
while ( true )
{
SPITest();
Thread.Sleep( 200 );
}
}
// 读写SPI数据
private static Int16 SPIReadWrite(Int16 value)
{
byte [] bout = new byte [ 2 ];
byte [] bin = new byte [ 2 ];
bout[ 0 ] = ( byte )(value >> 8 );
bout[ 1 ] = ( byte )(value & 0xff );
_spi.WriteRead(bout, bin);
Int16 aw0 = (Int16)((bin[ 0 ] << 8 ) + bin[ 1 ]);
return aw0;
}
// SPI测试
public static void SPITest()
{
if (QAW ++ > 100 ) QAW = 0 ;
Debug.Print( " SPI: DI= " + SPIReadWrite((Int16)QAW).ToString() + " DO= " + QAW.ToString());
}
}
}
测试结果:
3、I2C测试
模拟器中相关I2C的地址如下:
地址=100
完整的测试代码如下:
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;
namespace I2CTest
{
public class Program
{
// I2C定义 模拟器I2C地址为100 时钟速度不要设置太小否则会有问题
static I2CDevice I2CBus = new I2CDevice( new I2CDevice.Configuration( 100 , 200 ));
static int IntI2CNum = 0 ;
public static void Main()
{
while ( true )
{
I2CTest();
Thread.Sleep( 200 );
}
}
// I2C测试
public static void I2CTest()
{
if ( ++ IntI2CNum > 10 ) IntI2CNum = 0 ;
// I2C读写
byte [] bytRData = new byte [ 8 ];
byte [] bytWData = new byte [ 3 ];
bytWData[ 0 ] = ( byte )IntI2CNum;
bytWData[ 1 ] = ( byte )(IntI2CNum * 2 );
bytWData[ 2 ] = ( byte )(IntI2CNum * 3 );
I2CDevice.I2CTransaction[] i2c = new I2CDevice.I2CTransaction[ 2 ];
i2c[ 0 ] = I2CBus.CreateReadTransaction(bytRData);
i2c[ 1 ] = I2CBus.CreateWriteTransaction(bytWData);
I2CBus.Execute(i2c, 100 ); // 执行
string strPace = " " ;
string strInfo = " I2C(Byte0-7): " + bytRData[ 0 ].ToString() + strPace + bytRData[ 1 ].ToString() + strPace + bytRData[ 2 ].ToString() + strPace + bytRData[ 3 ].ToString() + strPace + bytRData[ 4 ].ToString() + strPace + bytRData[ 5 ].ToString() + strPace + bytRData[ 6 ].ToString() + strPace + bytRData[ 7 ].ToString();
Debug.Print(strInfo);
}
}
}
测试结果:
4、其它测试
串口、鼠标等等测试,暂略,有兴趣的朋友可以自行测试。
三、嵌入式开发资源
• Windows Embedded中文官方网站
http://www.microsoft.com/china/windows/embedded
• .NET Micro Framework
http://msdn2.microsoft.com/zh-cn/embedded/bb267253.aspx
• Microsoft Robotics Studio
http://msdn2.microsoft.com/zh-cn/robotics/default.aspx
• 微软嵌入式开发者论坛
http://forums.microsoft.com/china/default.aspx?ForumGroupID=493&SiteID=15
• 微软中国嵌入式开发者博客
• Mike Hall的博客
http://msdn2.microsoft.com/zh-cn/embedded/ Aa731228.aspx
• Windows Embedded 专题
• 叶帆工作室(CSDN)
http://blog.csdn.net/yefanqiu/
• 叶帆工作室(博客园)