Zhang happy Windows 10 IoT Development Notes: use the software PWM in Lightning to drive RGB LEDs, iotpwm

Source: Internet
Author: User

Zhang happy Windows 10 IoT Development Notes: use the software PWM in Lightning to drive RGB LEDs, iotpwm

It seems that Windows 10 IoT has never been used up again. So, let's write a blog about the story. In fact, I wanted to write it about half a year ago. At that time, I wanted to build a Windows 10 IoT-based car, but Raspberry Pi native didn't support PWM. Baidu also couldn't find it, and went around GitHub, and found Lightning at @ ms-iot, and then looked at the last update time, or in mid-2016 ...... Windows 10 IoT is so miserable in China that no one has written a tutorial for such a long time ...... Don't talk nonsense ......

Example address: https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/RgbLed

Lightning Project address: https://github.com/ms-iot/lightning

:

 

  1. Change the default controller driver

Open Windows Device Portal of Raspberry Pi, set the Default Controller Driver option in the Devices menu, change the Default Inbox Driver to Direct Memory Mapped Driver, and restart.

 

 

  Ii. Change Package. appxmanifest Configuration

Create a UWP project. This document is called RgbLedDemo ". Open Package. appxmanifest in the form of "view code.

Add a namespace under the Package label and change the IgnorableNamespaces attribute.

xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"IgnorableNamespaces="uap mp iot"

Add the following content to Capabilities:

<iot:Capability Name="lowLevelDevices" /><DeviceCapability Name="109b86ad-f53d-4b76-aa5f-821e2ddf2141"/>

 

  3. Reference Microsoft. Iot. Lightning

Search for Lightning in the NuGet Package Manager.

Windows IoT Extensions for the UWP

 

  4. Use Lightning

Note reference

using Windows.Devices.Pwm;using Microsoft.IoT.Lightning.Providers;

  1. Determine whether Lightning is enabled.

This step is necessary because the default controller driver must be disabled when Lightning is used. If it is not enabled, an exception will be thrown.

if (!LightningProvider.IsLightningEnabled){    throw new NullReferenceException("Lightning isn't enabled !");}

  2. Obtain the software PWM Controller

Under normal circumstances, we should get the software PWM controller in Lightning. Lightning also integrates other hardware PWM controllers. Therefore, when PwmController. GetControllersAsync () is called, a set is returned. The second is the software PWM controller we need. After obtaining the controller, you also need to set the PWM frequency. The software PWM controller's frequency range is between 40-1000Hz (low poor ......), A number that is not in this range throws an exception.

PwmController controller = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1];controller.SetDesiredFrequency(1000);

  3. Set the PWM pin

Take the Red pin as an example. First, open the pin through the controller, which is at the position of GPIO 17. Then you need to set the Duty Cycle Percentage. The common point is the proportion of the voltage and the decimal point between 0 and 1.

PwmPin redPin = controller.OpenPin(17);redPin.SetActiveDutyCyclePercentage(0);redPin.Start(); 

Then, you need to change the brightness of the LED or the value of SetActiveDutyCyclePercentage (value.

To release the instance, disable PWM first.

redPin.Stop();redPin.Dispose();

 

  5. Note

After Lightning is used, the SPI code will report an error based on the I2C written based on the default controller driver. However, Lightning integrates I2C, SPI, GPIO, and other controllers. Just replace them.

 

The project parsing section in this article is over. The following is a test code for a breathing lamp. I use a common cathode RGB LED. The Code is available in GitHub projects.

/// <summary>/// Breathing LED/// </summary>/// <param name="delay">Delay Time</param>public async Task BreathingAsync(int delay){    double red = 255;    double green = 0;    double blue = 0;    while (red != 0 && green != 255)    {        redPin.SetActiveDutyCyclePercentage(red / 255.0);        greenPin.SetActiveDutyCyclePercentage(green / 255.0);        red--;        green++;        await Task.Delay(delay);    }    while (green != 0 && blue != 255)    {        greenPin.SetActiveDutyCyclePercentage(green / 255.0);        bluePin.SetActiveDutyCyclePercentage(blue / 255.0);        green--;        blue++;        await Task.Delay(delay);    }    while (blue != 0 && red != 255)    {        bluePin.SetActiveDutyCyclePercentage(blue / 255.0);        redPin.SetActiveDutyCyclePercentage(red / 255.0);        blue--;        red++;        await Task.Delay(delay);    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.