树莓派windows iot 开发(三) 闪烁的LED
<div class='showpostmsg'><p align="left"><span style="font-size:16px;">目录</span></p><hr />
<p align="left"><span style="font-size:16px;"><a href="https://home.eeworld.com.cn/thread-1108717-1-1.html" target="_blank" title="树莓派windows iot 开发(一)">树莓派windows iot 开发(一)</a></span></p>
<p align="left"><span style="font-size:16px;"><a href="https://home.eeworld.com.cn/thread-1108724-1-1.html" target="_blank" title="树莓派windows iot 开发(二) USB摄像机">树莓派windows iot 开发(二) USB摄像机</a></span></p>
<p align="left"><span style="font-size:16px;"><a href="https://home.eeworld.com.cn/thread-1108877-1-1.html" target="_blank" title="树莓派windows iot 开发(三) 闪烁的LED">树莓派windows iot 开发(三) 闪烁的LED</a></span></p>
<p align="left"><span style="font-size:16px;"><a href="https://home.eeworld.com.cn/thread-1109072-1-1.html" target="_blank" title="树莓派windows iot 开发(四) UART接口">树莓派windows iot 开发(四) UART接口</a></span></p>
<p align="left"><span style="font-size:16px;"><a href="https://bbs.eeworld.com.cn/thread-1109475-1-1.html" target="_blank">树莓派windows iot 开发(五) SPI接口</a></span></p>
<p align="left"> </p>
<p> </p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">树莓派windows iot开发(三)闪烁的LED</span></span></p>
<hr />
<p> </p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">首先需要准备的材料:</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">树莓派开发板3B一块,还有电源、网线、显示器、鼠标等等主要设备;</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">实验项目材料:</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">一个电阻1K欧姆,和LED灯一个,杜邦导线两条。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">我只是简单的做个实验,也没必要非弄个“面包板”</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">然后到网站上下载资料。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">https://docs.microsoft.com/en-us/samples/microsoft/windows-iotcore-samples/hello-blinky/</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">根据扩展端口开始连线,项目中的电源是选择的3.3V电源,板子上也可以是5V。我选择的是3.3V。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">输出引脚选择的是“GPIO 5”作为输出。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">将项目用VS2019打开。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">配置好远程主机。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">就可以开始试着运行了。程序启动后,可以看到led灯不停的闪烁。与屏幕上红色亮点是同步的。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"></span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">其实这个项目没有什么难度,只是能够证明windows 也可以用来做控制用。记得公司使用PC104来开发项目,那时主要的考虑是因为intel cpu的计算能力比较强。因为系统中的仪器需要比较复杂的计算。看来使用树莓派这种板子也是可以的。而且价格也是PC104不能比的。下面分析一下代码:</span></span></p>
<pre>
<code class="language-cs">
public sealed partial class MainPage : Page
{
private const int LED_PIN = 5;
private GpioPin pin;
private GpioPinValue pinValue;
private DispatcherTimer timer;
private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);
public MainPage()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += Timer_Tick;
InitGPIO();
if (pin != null)
{
timer.Start();
}
}
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);
pinValue = GpioPinValue.High;
pin.Write(pinValue);
pin.SetDriveMode(GpioPinDriveMode.Output);
GpioStatus.Text = "GPIO pin initialized correctly.";
}
private void Timer_Tick(object sender, object e)
{
if (pinValue == GpioPinValue.High)
{
pinValue = GpioPinValue.Low;
pin.Write(pinValue);
LED.Fill = redBrush;
}
else
{
pinValue = GpioPinValue.High;
pin.Write(pinValue);
LED.Fill = grayBrush;
}
}
</code></pre>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">第一步:初始化设备</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"> var gpio = GpioController.GetDefault();</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">这是个函数是让驱动程序初始化一下。接下来判断一下是否成功。因为有些设备不一定有GPIO驱动。尤其是intel的有些主板。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"> pin = gpio.OpenPin(LED_PIN);<br />
pinValue = GpioPinValue.High;<br />
pin.Write(pinValue);<br />
pin.SetDriveMode(GpioPinDriveMode.Output);</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">打开GPIO5,设置默认输出,在定义GPIO为输出。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">利用pin.Write(pinValue);函数设置引脚的电平。</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;"> pinValue = GpioPinValue.High;<br />
pin.Write(pinValue);</span></span></p>
<p><span style="font-size:16px;"><span style="font-family:Microsoft YaHei;">利用windows丰富的图形界面可以做出很多不错的程序。<br />
<br />
<b><font color="#5e7384">此内容由EEWORLD论坛网友bigbat原创,如需转载或用于商业用途需征得作者同意并注明出处</font></b></span></span><br />
</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]