我想获得两个PWM信号(即PWM输入)的频率和占空比,并根据输入将它们设置为另一个(即PWM输出).这些PWM信号的占空比为50%,而其频率范围为1kHz至20kHz.
我检查了一下网页,我从Windows 10 IoT Core找到了Microsoft IoT Lightning库(即总线提供商).即使使用PWM Consumer示例,这个库似乎也是我需要的!
然而,当我在测试基于PWM Consumer的第一个示例时,我注意到PWM控制器的频率范围限制在40Hz到1kHz之间.因此,第一个问题:似乎不支持频率范围.
此外,当PWM控制器属性“ActualFrequency”返回通过“SetDesiredFrequencyMethod”设置的频率时,PWMPin对象仅提供有关当前占空比的信息.
因此,我用谷歌搜索寻找答案,我发现this question比以前的两个问题更让我困惑.
您知道是否可以以及如何使用MS-IoT Lightning Library在Raspberry Pi2上设置/获取1kHz至20kHz的PWM信号?
这里,示例中的几行代码:
public async void Run(IBackgroundTaskInstance taskInstance) { if (!LightningProvider.IsLightningEnabled) { // Lightning provider is required for this sample return; } var deferral = taskInstance.GetDeferral(); // Use the PAC9685 PWM provider,LightningPCA9685PwmControllerProvider pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[0]; motorPin = pwmController.OpenPin(0); secondMotorPin = pwmController.OpenPin(1); //// To use the software PWM provider,LightningSoftwarePwmControllerProvider,with GPIO pins 5 and 6,//// uncomment the following lines and comment the ones above //pwmController = (await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider()))[1]; //motorPin = pwmController.OpenPin(5); //secondMotorPin = pwmController.OpenPin(6); pwmController.SetDesiredFrequency(50); motorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth); motorPin.Start(); secondMotorPin.SetActiveDutyCyclePercentage(RestingPulseLegnth); secondMotorPin.Start(); timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick,TimeSpan.FromMilliseconds(500)); } private void Timer_Tick(ThreadPoolTimer timer) { iteration++; if (iteration % 3 == 0) { currentPulseLength = ClockwisePulseLength; secondPulseLength = CounterClockwisePulseLegnth; } else if (iteration % 3 == 1) { currentPulseLength = CounterClockwisePulseLegnth; secondPulseLength = ClockwisePulseLength; } else { currentPulseLength = 0; secondPulseLength = 0; } double desiredPercentage = currentPulseLength / (1000.0 / pwmController.ActualFrequency); motorPin.SetActiveDutyCyclePercentage(desiredPercentage); double secondDesiredPercentage = secondPulseLength / (1000.0 / pwmController.ActualFrequency); secondMotorPin.SetActiveDutyCyclePercentage(secondDesiredPercentage); }
一切顺利,洛伦佐