Sorry about the mis-information about freq=5.
It turns out that one of the factors influencing the frequency of the timer is determined by the value of the PSC register.
For timers, 2, 3, 4, 5, 6, 7, 12, 13, and 14 it works out to 84 MHz.
For timers 1, 8, 9, 10, 11 it works out to 168 MHz (I think I haven't confirmed this yet).
If you do:
pwm = PWM(pyb.Pin.board.X1)
then you'll get timer 2.
The PSC value of 83 corresponds to a prescaler of 84, so the timer clock is 84,000,000 / 84 = 1,000,000 or 1 MHz.
The ARR value is 9999 which means that the timer reloads every 10,000 timer clocks or 1,000,000 / 10,000 = 100 times per second.
So if you set PSC to 41, then your LED will get PWM with a frequency of 200 Hz.
If you set PSC to 20 then LED will get PWM with a frequeny of 400 Hz
In particular, I'm referring to line 110 which says:
Code: Select all
stm.mem32[pwm_timer + stm.TIM_PSC] = 83 #gives 10,000 steps between 0 and 3.3v
I was able to confirm this on my logic analyzer. I attached a screenshot of what the signal looks like with a PSC of 41 and pwm.duty(50)
- Screen shot taken with PSC=41 and duty(50)
- Screenshot-2.png (95.74 KiB) Viewed 5821 times
Over on the right it shows the frequency of about 200 Hz (199.92 Hz) and 50/255 = 19.6% duty cycle. 19.6% of 5.002 msec = 0.980 msec (The Width value over on the right of the screenshot)