You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example shows how to fully configure a PWM with HardwareTimer.
4
+
PWM is generated on `LED_BUILTIN` if available.
5
+
PWM is generated by hardware: no CPU load.
6
+
Nevertheless, in this example both interruption callback are used on Compare match (Falling edge of PWM1 mode) and update event (rising edge of PWM1 mode).
7
+
Those call back are used to toggle a second pin: `pin2`.
8
+
Once configured, there is only CPU load for callbacks executions.
9
+
*/
10
+
11
+
// 'pin' PWM will be mangaed automatically by hardware whereas 'pin2' PWM will be managed by software through interrupt callback
12
+
#if defined(LED_BUILTIN)
13
+
#definepin LED_BUILTIN
14
+
15
+
#if LED_BUILTIN == D3
16
+
#error LED_BUILTIN == D3
17
+
#else
18
+
#definepin2 D3
19
+
#endif
20
+
21
+
#else
22
+
#definepin D2
23
+
#definepin2 D3
24
+
#endif
25
+
26
+
voidUpdate_IT_callback(HardwareTimer*)
27
+
{ // Update event correspond to Rising edge of PWM when configured in PWM1 mode
28
+
digitalWrite(pin2, LOW); // pin2 will be complementary to pin
29
+
}
30
+
31
+
voidCompare_IT_callback(HardwareTimer*)
32
+
{ // Compare match event correspond to falling edge of PWM when configured in PWM1 mode
33
+
digitalWrite(pin2, HIGH);
34
+
}
35
+
36
+
voidsetup()
37
+
{
38
+
// No need to configure pin, it will be done by HardwareTimer configuration
39
+
// pinMode(pin, OUTPUT);
40
+
41
+
// Need to configure pin2, as it is not managed by HardwareTimer
42
+
pinMode(pin2, OUTPUT);
43
+
44
+
// Automatically retrieve TIM instance and channel associated to pin
45
+
// This is used to be compatible with all STM32 series automatically.
MyTim->setMode(2, TIMER_OUTPUT_COMPARE); // In our case, channekFalling is configured but not really used. Nevertheless it would be possible to attach a callback to channel compare match.
35
+
MyTim->setOverflow(10, HERTZ_FORMAT); // 10 Hz
36
+
MyTim->attachInterrupt(Update_IT_callback);
37
+
MyTim->resume();
38
+
}
39
+
40
+
41
+
voidloop()
42
+
{
43
+
/* Nothing to do all is done by hardware. Even no interrupt required. */
0 commit comments