not logged in | [Login]
Pin X2 is 25% duty cycle, and X3 is 50% duty cycle.
import pyb # TIM2 runs at 84 MHz # at 20000 Hz, count runs from 0 to 4199 # In center mode, the frequency will be halved, so we'll get a 10 kHz output t2 = pyb.Timer(2, freq=20000, counter_mode=pyb.Timer.COUNTER_MODE_CENTER) ch2 = t2.channel(2, mode=pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=((t2.period() + 1) // 4) - 1) ch3 = t2.channel(3, mode=pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=((t2.period() + 1) // 2) - 1)
This is what the signal looks like on a logic analyzer:
Pin X2 is OC Mode producing a 1 KHz clock. Pin X3 is PWM at 200 Hz with a 10% duty cycle.
import pyb # setup oc_clock to be a 1 kHz clock. Since it toggles we want it to toggle # 2000 times per second to get a 1000 Hz clock. t2 = pyb.Timer(2, freq=2000) oc = t2.channel(2, mode=pyb.Timer.OC, pin=pyb.Pin.board.X2, oc_mode=pyb.Timer.OC_MODE_TOGGLE) # stup PWM to be 200 Hz with a 1 clock pulse_width t5 = pyb.Timer(5, prescaler=41999, period=9) pwm = t5.channel(3, mode=pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=1)This is what the signal looks like on a logic analyzer:
Last edited by Steve Holden, 2017-02-24 10:55:40