骐's profile出入愁苦之间PhotosBlogListsMore Tools Help

Blog


    December 07

    我的第一个.Net Micro Framework设备驱动程序

    在2006春天参加在美国Las Vergas的MEDC就看到了Micro Framework创建的设备以及机器人,当然对微软使用Micro Framework创建的微软手表以及做笔记本的第二显示屏的Sideshow仰慕已久。我在Windows Mobile和Symbian等手机平台上做过多年C++/C#的开发,但从来没有写过设备驱动,但心里向往编写驱动程序但没有机会,所以一拿到Micro Framework的开发板,就开始自己的第一个设备驱动程序之旅。

    基于.Net Micro Framework设备开发很简单,需要一台运行Visual Studio 2005的电脑,下载并安装免费的Microsoft .NET Micro Framework SDK( http://www.microsoft.com/downloads/details.aspx?FamilyId=32F5DF20-6C95-4FE8-A76C-0ED56A839AD2&displaylang=en ),然后选择一个开发板或者和微软签协议自己创建开发板,去一些芯片厂商或代理的网站订购GPS或传感器的芯片,就可以编写驱动和应用了。如果应用和设备都测试完成,就可以去找Design House设计外型,找工厂批量生产。如果只是学习如何开发应用,所有的工作都可以在模拟器上完成,包括编写模拟设备。

    在这个入门的项目中,参考微软.Net Micro Framework团队的博客文章”Building a Thermostat with the Microsoft .NET Micro Framework”( http://blogs.msdn.com/netmfteam/archive/2007/11/12/building-a-thermostat-with-the-microsoft-net-micro-framework.aspx ), 选择Embedded Fusion的Tahoe开发板和AD7314开发一个温度计。首先介绍一下准备使用的Analog Devices公司的 AD7314,是一个MSOP-8封装的温度传感器,包含一个10位的带隙温度传感器和一个10位的A/D转换器。另外,AD7314还具有一个串行接口,其与SPI、QSPI和MICROWIRE及DSP总线协议兼容,使得它与微处理器接口十分方便。AD7314的外形也非常的小,这正是有一些手机和硬盘使用的这个芯片的原因。具体的技术特点如下:

    (1)10位数字温度传感器;

    (2)工作温度范围–35ºC 至 +85ºC;

    (3)精度:+2ºC;

    (4)SPI和DSP兼容串行接口;

    (5)休眠模式。

    AD7314的图片如下,就是最下面的那个小黑块:

    PA300372

    因为AD7314使用广泛,所以采购也很简单,在digikey.com的网站上就可以直接网上定购(定购可参考: http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail?name=AD7314ARMZ-ND)买一个需要1.63美元,因为我本人这段时间在温哥华,运费也很低,如果中国的朋友感兴趣,我可以购买之后让朋友带回去 。 

    选择开发板

    微软Micro Framework的主页上有很多公司提供硬件运行.NET Micro Framework,因为相比之下张欣告诉我的Embedded Fusion比较便宜,再加上他们公司有一个embedded MVP叫Steve Maillet非常热心,所以选择了Embedded Fusion的Tahoe开发板( http://www.embeddedfusion.com )。其实也可以选择飞思卡尔的i.MXS开发工具包,零售价499.10美元 (http://www.freescale.com/webapp/sps/site/overview.jsp?nodeId=02XPgQ8217297301A5).

    PB060443

    在编写驱动之前,需要把AD7314和开发板用线连起来。由于AD7314实在是太小了,像米粒那么小的芯片上引出八支脚,测试时连线比较困难,所以在digikey.com上购买了一个供AD7314使用的面包板33108CA-ND(定购可参考:http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail?name=33108CA-ND ), 虽然引脚间的距离很小,还是需要小心的每支脚都焊起来。

    PB060444

    连线

    接下来就得连线了,AD7314温度传感器芯片引脚示意图如下:

    PB060445 PA300399

    在芯片上有一个小记号代表引脚1,通常习惯面包析朝下时,引脚1放在左上脚。因为AD7314使用SPI总线通讯,所以把AD7314 引脚和开发板上的SPI端口连起来。使用SPI需要一个引脚来激活,通常使用一个GPIO的引脚就行了,在这里可以选择一个在开发板上离SPI端口最近的PA7,也就是GPIO pin 5,一会儿在驱动程序中需要指定是Pin5来激活。

    连线的任务很简单,VDD代表电源连到开发板的+3.3v, SCLK (serial clock,串口时钟)连到开发板的串口时钟,SDI (serial data in) 和 SDO (serial data out)分别连到开发板的MISO(master in, slave out)和MOSI(master out, slave in).

    PB060435 PB060441

    连线有个诀窍,除了必需使用焊接外在测试时尽量少用。在这里使用连线的诀窍有两个:一个是使用网线里的双绞线做连线,二是使用橡皮圈来连接引脚和电线。废弃的网线随处可得,使用双绞线的好处是里面的铜丝比较硬,连接时各自都有自己形状,不会互相干扰; 使用橡皮圈连接引脚和连线可以避免使用焊枪,一则可以重用连线,二则实现也简单,橡皮圈可以找气门芯或者粗一点的电线皮代替。

    在下图中,在主板上使用了一个连接头S5682-ND,(定购参考:http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail?name=S5682-ND ),这样可以直接把连线插到连接头里,双绞线比较坚硬,可以直接插到连接头里,而且很牢固,使用焊接是个体力活,做起来很花时间。

    PB060437

    创建驱动的项目

    使用Embedded Fusion的Tahoe开发板另一个好处是提供了一系列模板,当然包括SPI Device Driver的模板,在VS2005中新建项目如下图:

    NewDriver

    项目创建完毕之后添加一个常量声明:

    public partial class AD7314 : SPIDeviceDriver;

    const int ClockRateKhz = 4000;

    然后修改AD7314的构造函数为:

    public AD7314()

    {

    SPI.Configuration cfg = new SPI.Configuration((Cpu.Pin)5

    , true

    , 0

    , 0

    , false

    , false

    , ClockRateKhz

    , SPI.SPI_module.SPI1

    );

    base.Config = cfg;

    }

    注:第一个参数(Cpu.Pin)5是因为在上面文章中使用第5个引脚来激活温度传感器。

    这样就可以使用基类中WriteRead函数的读取数据了:

    public float ReadTemperature()

    {

    // Create the output and input arrays of bytes

    byte[] bout = new byte[2];

    byte[] bin = new byte[2];

    // Set the output bytes both to 0

    // This sensor doesn't need any commands. All this method does is read

    // the input data, so the output is simply for SPI clocking

    bout[0] = 0;

    bout[1] = 0;

    // Write the clocking info, then read the input data

    base.WriteRead(bout, bin);

    根据温度的数据格式转换数据格式:

    table

    代码如下:

    // Convert the 2 byte value according to the AD7314 data sheet

    // Put the raw bytes into an int

    // Mask the upper bit, which is a sign indicator (+/-)

    int s = (((bin[0] << 8) + bin[1]) & 0xBFFF);

    // Convert to a float

    float f = s;

    // Shift right

    f /= 128;

    // Detect negative

    if ((bin[0] & 0x40) == 0x40)

    f = -f;

    // Return the temperature

    return f;

    }

    如果不想使用Embedded Fusion的Tahoe项目模板,也很简单,只需要在Class AD7314里根据cfg创建一个SPI的成员变量,调用SPI的WriteRead函数。

    读取温度

    在.Net Micro Framework中使用温度也非常的简单,只需要在项目中引用刚才创建的Class Library, 只需要在需要读取温度的地方如下调用:

    // Make sure the sensor object is valid

    if (_sensor == null)

    _sensor = new AD7314();

    // Read the value from the sensor

    double temp = _sensor.ReadTemperature();

    注:本文章参考微软.Net Micro Framework团队的博客文章”Building a Thermostat with the Microsoft .NET Micro Framework”:
    http://blogs.msdn.com/netmfteam/archive/2007/11/12/building-a-thermostat-with-the-microsoft-net-micro-framework.aspx

    有关.Net Micro Framework的开发的信息:

    1. Mobile MVP张欣的第一个Digi开发板上的.Net Micro Framework程序:Hello Morse Code; Hello .NET Micro Framework.(http://mobileside.cn/blogs/breakstring/archive/2007/10/06/MorseCodeOnDotNETMF.aspx)

    2. Embedded MVP刘洪峰的博客:http://blog.csdn.net/yefanqiu/,包括Digi开发板初探和模拟器改造一系列的文章

    3. 微软.Net Micro Framework主页:http://msdn.microsoft.com/embedded/netmf

    4. 微软.Net Micro Framework开发团队博客:http://blogs.msdn.com/netmfteam/

    5. Windows Vista Sideshow:
    http://www.microsoft.com/china/windowsvista/features/foreveryone/sideshow.mspx

    6. 微软手表: http://direct.msn.com/

    Comments (6)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    钻研精神十分可嘉!
    Dec. 16
    vincentwrote:
    签名留念。邹侃
    Feb. 23
    Hao Huwrote:
    非常D感兴趣阿
    Jan. 14
    Chenwrote:
    真高深,看不懂
    Dec. 8
    可恶的小指老师兼同学~沙发是我的我的我的~~~
    Dec. 7
    Kenn Zhangwrote:
    哈哈哈...我也是用网线作连接线
    Dec. 7

    Trackbacks

    The trackback URL for this entry is:
    http://maqi.spaces.live.com/blog/cns!8C079D3799712029!653.trak
    Weblogs that reference this entry
    • None