forked from stm32duino/STM32Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimebase_callback.ino
44 lines (35 loc) · 1.15 KB
/
Timebase_callback.ino
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
/*
Timebase callback
This example shows how to configure HardwareTimer to execute a callback at regular interval.
Callback toggles pin.
Once configured, there is only CPU load for callbacks executions.
*/
#if defined(LED_BUILTIN)
#define pin LED_BUILTIN
#else
#define pin D2
#endif
void Update_IT_callback(HardwareTimer*)
{ // Toggle pin. 10hz toogle --> 5Hz PWM
digitalWrite(pin, !digitalRead(pin));
}
void setup()
{
#if defined(TIM1)
TIM_TypeDef *Instance = TIM1;
#else
TIM_TypeDef *Instance = TIM2;
#endif
// Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
HardwareTimer *MyTim = new HardwareTimer(Instance);
// configure pin in output mode
pinMode(pin, OUTPUT);
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.
MyTim->setOverflow(10, HERTZ_FORMAT); // 10 Hz
MyTim->attachInterrupt(Update_IT_callback);
MyTim->resume();
}
void loop()
{
/* Nothing to do all is done by hardware. Even no interrupt required. */
}