forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
77 lines (63 loc) · 2.39 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Copyright (c) 2018 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//
using Windows.Devices.Gpio;
using System;
using System.Threading;
using Windows.Devices.Pwm;
namespace PwmTest
{
public class Program
{
public static void Main()
{
bool goingUp = true;
float dutyCycle = .00f;
// there is no PWM output pin connected to an LED in STM32F769I_DISCO
// the closest one is LD3 connected to PA12 and exposed in Arduino connector, pad D13
// to see the PWM output in this LED need to short these two pins
// set this GPIO pin as input so it doesn't mess up with PWM output
GpioPin dummyPad = GpioController.GetDefault().OpenPin(PinNumber('A', 12));
dummyPad.SetDriveMode(GpioPinDriveMode.Input);
PwmController pwmController;
PwmPin pwmPin;
// we'll be using PA11, exposed in Arduino connector, pad D10
// as the PWM output pin, this one is TIM11_CH4
pwmController = PwmController.FromId("TIM1");
pwmController.SetDesiredFrequency(5000);
// open the PWM pin
pwmPin = pwmController.OpenPin(PinNumber('A', 11));
// set the duty cycle
pwmPin.SetActiveDutyCyclePercentage(dutyCycle);
// start the party
pwmPin.Start();
for (;;)
{
if (goingUp)
{
// slowly increase light intensity
dutyCycle += 0.05f;
// change direction if reaching maximum duty cycle (100%)
if (dutyCycle > .95) goingUp = !goingUp;
}
else
{
// slowly decrease light intensity
dutyCycle -= 0.05f;
// change direction if reaching minimum duty cycle (0%)
if (dutyCycle < 0.10) goingUp = !goingUp;
}
// update duty cycle
pwmPin.SetActiveDutyCyclePercentage(dutyCycle);
Thread.Sleep(50);
}
}
static int PinNumber(char port, byte pin)
{
if (port < 'A' || port > 'J')
throw new ArgumentException();
return ((port - 'A') * 16) + pin;
}
}
}