Now to some more hardware features of the Stm32 Cortex M3, this time let's use ... Timer3, for a change!
Pulse Width Modulation mode allows one to generate a signal with a frequency determined by the value of the TIM3_ARR register
and a duty cycle determined by the value of the TIM3_CCR3 register.
We wish to generate a 1800 Hz fixed frequency tone with a 25% Duty-Cycle using Timer3, Channel_3 in Pulse Width Modulation Output mode!
This time we use PortB_0 as the Pwm Output Pin Channel_3 and we must alter the usual initialisation section to take this change
into account. We can vary the Duty-Cycle using the 16-Bit Timer_3 capability and this reveals another difference between the
ARM and the AVR. 16-Bit versus 10-Bit Pwm Output resolution and a much higher Frequency in comparison too!
The formula for calculating the Duty-Cycle to be output is as follows:
Let PreScaler = 8
Let Period = 5000
Frequency = 72,000,000 / (PreScaler * Period)
Frequency = 1800 Hz.
Duty-Cycle = ((TIM3_CCR3 / TIM3_ARR) * 100) %
Duty-Cycle = 25 % Using the example given.
The Duty-Cycle value in R0 is sent out to Register TIM3_CCR3. The Frequency remains fixed @1800 Hz.
You can try out the formula using different values for Frequency, PreScaler, Period and Duty-Cycle.
To see the Pulse Width Modulation in action, simply attach a scope lead to Pin 11 on Header P1 onboard the Stm32 Mini!
Happy Duty-Cycling ;-)Download