Here is the example. I configured TIM2 on pin PA_0 (MOT1 encoder A pin). I setup the timer with 2ms period (500Hz) and resolution of 2000 (the Auto Reload Register = 1999). As a test signal I used pwm on pin PE_9 (servo 1 pwm pin) with period 1ms (1000Hz) so it can be measured with the timer. This way we can measure the input pwm signal’s duty with 0.1% accuracy (input signal takes half of the timer period).
Connect the pins like that:
The code is written using mbed, so at the beginning you will need to set up your environment using our template:
All necessary steps are described in the
The code is here:
main.cpp (6.4 KB)
To get the timer configuration part I used
STM32CubeMx tool from
STM32Cube IDE and then copied generated config code to my mbed program.
In case you want to check the functionality now, here is the
.hex file. You can upload it to your CORE2 using
Use button 1 and button 2 to increase and decrease the test pwm duty cycle. Open serial monitor with baudrate 230400 to see measurement information: