zoukankan      html  css  js  c++  java
  • 树莓派 Windows10 IoT Core 开发教程

    入门指引

    现在让我们把LED连接到安装了Windows10 IoT Core 的硬件设备,并创建一个应用程序来让它们闪烁。

    在Visual Studio中加载工程

    首先在这里找到例程,这里有C++和C#的版本可供选择。本教程仅介绍使用C#的版本。将工程文件夹拷贝到磁盘中,然后用Visual Studio打开。
    然后检查你的Windows IoT设备,确保打开了远程调试功能(Remote Debugging),可以参考这里的Hello World程序。
    请注意如果Windows 10找不到可用的GPIO接口,应用程序将不会工作。比如你将windows10安装在了VM虚拟机中。

    将LED连接到 Windows 10 设备

    准备好下面的东西:
    一个LED灯
    一个阻值220欧姆电阻
    若干杜邦线和面包板

    components-0

    将LED的负极连接到Raspberry Pi2的GPIO 5引脚(Board编号29),正极串联嗲足后连接到3.3v电源。(请务必注意极性,在直插型封装的LED中,较长的引脚是正极+,较短的引脚是负极-)

    RP2_Pinout-0

    breadboard_assembled_rpi2-0

    部署应用程序

    对于Raspberry Pi2来说,应该在architecture的下拉菜单中选择ARM。

    以上的步骤都做好了以后。可以按下F5,程序会自动运行,然后就可以看到闪烁的LED和下面的模拟界面。

    blinky-screenshot-0

    可以通过改变滑块的位置来调整LED闪烁的有效时间

    代码详解

    下面就是这个程序的代码,基本工作原理是当定时器的时间达到后,调用事件Tick改变LED的状态。

    定时器代码

    这里是设置定时器的C#代码

    public MainPage()
    {
        // ...
    
        this.timer = new DispatcherTimer();
        this.timer.Interval = TimeSpan.FromMilliseconds(500);
        this.timer.Tick += Timer_Tick;
        this.timer.Start();
    
        // ...
    }
    
    private void Timer_Tick(object sender, object e)
    {
        FlipLED();
    }
    

    初始化GPIO引脚

    为了能够驱动GPIO,首先需要对它进行初始化,这里是初始化程序的C#代码

    using Windows.Devices.Gpio;
    
    private void InitGPIO()
    {
        var gpio = GpioController.GetDefault();
    
        // Show an error if there is no GPIO controller
        if (gpio == null)
        {
            pin = null;
            GpioStatus.Text = "There is no GPIO controller on this device.";
            return;
        }
    
        pin = gpio.OpenPin(LED_PIN);
    
        // Show an error if the pin wasn't initialized properly
        if (pin == null)
        {
            GpioStatus.Text = "There were problems initializing the GPIO pin.";
            return;
        }
    
        pin.Write(GpioPinValue.High);
        pin.SetDriveMode(GpioPinDriveMode.Output);
    
        GpioStatus.Text = "GPIO pin initialized correctly.";
    }
    

    简单的解释就是:
    ~首先,使用GpioController.GetDefault()获取GPIO控制权限
    ~如果设备不具有可用的GPIO资源,则返回null
    ~接下来通过调用GpioController.OpenPin()函数来打开GPIO引脚
    ~当我们获取了GPIO的控制权限并打开了GPIO引脚后,使用GpioPin.Write()函数来将LED关闭(参数设置High)
    ~这里还使用了GpioPin.SetDriveMode()函数将GPIO引脚的工作模式设置为输出模式。

    改变GPIO引脚的状态

    使用GpioPinValue.Low参数打开LED:
    this.pin.Write(GpioPinValue.Low);
    使用GpioPinValue.High参数关闭LED:
    this.pin.Write(GpioPinValue.High);

    因为我们将LED的正极连接到了3.3V电源,所以这里通过将GPIO引脚置低电平来打开LED。

    本文来自:树莓派实验室
    链接地址:http://shumeipai.nxez.com/2015/05/01/raspberrypi-develop-win10-samples-blinky.html
  • 相关阅读:
    pythonselenium 3种等待方式
    SQL Server2008生成数据库字典
    C# 二分法查找和排序
    JavaScript 中的面向对象的初步认识
    分享一个通过面向对象方式完成的拖拽功能
    javascript 面向对象的继承的实现
    【转】ASP.NET MVC 3 Service Location, Part 3: View Engines/View Pages
    【转】ASP.NET MVC 3 Service Location, Part 2: Controllers
    【转】ASP.NET MVC 3 Service Location, Part 8: Value Providers
    【转】ASP.NET MVC 3 Service Location, Part 6: Model Validation
  • 原文地址:https://www.cnblogs.com/zjoch/p/5494395.html
Copyright © 2011-2022 走看看