I am going to output a 1 Hz frequency and 50% duty cycle pwm(pulse width modulation) signal using timer-4 of stm32f103 microcontroller. Timer-4 channel-1 is used to output signal. Channel-1 corresponds to PB6 pin of stm32f103 microcontroller. At PB6 an led is connected on which pwm output can be seen. 1 Hz frequency in time domain is T=1/f > T=1/1 Hz > T=1 s. So 1 Hz frequency translates to 1 s and my duty cycle is 50% so the led at PB6 pin will blink at half a second rate.
How to Generate Desired/Specific PWM Frequency? Formula derivation and Calculations.
Stmcube-Mx code initializing steps an generating keil MDK-ARM code
Timer 3 and 4 are stand alone and they do not collide with any other peripheral function. So its good to use them. I am using Timer 4 in the project/tutorial.
I am using Internal clock source so check this check box in the timer settings. I am using timer 4, channel 1 for pwm output so i selected channel 1. Channel 1 corresponds to PB6 of stm32f103 microcontroller. The settings diagram is given on the right side.
Calculating Values for 1Hz frequency and 50% duty cycle pwm signal output
Timer Input Clock = 0.0625Mhz or 62500Hz
Required Frequency = 1 Hz (In Time Domain 1 second)
Counter Period/Pwm Resolution(Steps) = 3906 (I picked a random value)
Counter Frequency= frequency required * Counter Period
Counter Frequency= 1Hz * 3906 =3906 Hz
Timer Prescaller value=( Timer Input Clock/Counter Frequency)-1
Timer Prescaller value=(62500 Hz / 3906 Hz) – 1 = 15
Now we have both the value’s Timer Presaller value, Counter Period/Pwm Resolution(Steps) and also they are in 16-bit range. Now input the values in the timer 4 configuration. I selected the counter to be in UP mode(Count for 0 to 3906). Pulse is the duty cycle in our case its 50% so 3906*50% > 3906*0.5 = 1953.
Watch the Project Video Here…