Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyb
- from pyb import Timer
- import micropython
- import stm
- # Use with pyb.freq(96000000) and prescaler=11 for .25 usec timer ticks.
- xfmr_pulse_period = 1200 # (= usec * 4) Same as toggle_half_cycle duration.
- xfmr_pulse_w = 300 # (= usec * 4)
- max_push_pulls = 8
- def force_inactive1():
- # Put (t2ch2/t2ch1) mode to FORCED_INACTIVE to start...
- # TODO which isn it, ch1 or ch2?
- # TODO link to PDF and page number for register field description
- ccmr1 = stm.mem16[stm.TIM2 + stm.TIM_CCMR1]
- ccmr1 &= 0b1111111110001111 # OC2M "100"....OC1M "100"
- ccmr1 |= 0b0000000001000000
- stm.mem16[stm.TIM2 + stm.TIM_CCMR1] = ccmr1
- def force_inactive2():
- # Put (t2ch2/t2ch1) mode to FORCED_INACTIVE to start...
- # TODO which isn it, ch1 or ch2?
- # TODO link to PDF and page number for register field description
- ccmr1 = stm.mem16[stm.TIM2 + stm.TIM_CCMR1]
- ccmr1 &= 0b1000111111111111 # OC2M "100"....OC1M "100"
- ccmr1 |= 0b0100000000000000
- stm.mem16[stm.TIM2 + stm.TIM_CCMR1] = ccmr1
- def force_inactive():
- # Put t2ch2 t2ch1 mode to FORCED_INACTIVE to start...
- ccmr1 = stm.mem16[stm.TIM2 + stm.TIM_CCMR1]
- ccmr1 &= 0b1000111110001111 # OC2M "100"....OC1M "100"
- ccmr1 |= 0b0100000001000000
- stm.mem16[stm.TIM2 + stm.TIM_CCMR1] = ccmr1
- def force_active():
- # TODO what to put here?
- micropython.alloc_emergency_exception_buf(100)
- events = 0
- events_max_push_pulls = max_push_pulls
- def disable_me(the_active_timer):
- global events
- events += 1
- if events>events_max_push_pulls:
- force_inactive1()
- the_active_timer.deinit()
- # Timer 2 to give .125 usec timer ticks counting up:
- t2 = pyb.Timer(2, prescaler=11, period=xfmr_pulse_period, mode=Timer.CENTER)
- t2ch2 = t2.channel(2, pyb.Timer.PWM, pulse_width=xfmr_pulse_w,
- polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP26)
- t2ch1 = t2.channel(1, pyb.Timer.PWM_INVERTED, pulse_width=xfmr_pulse_period - xfmr_pulse_w,
- polarity=pyb.Timer.LOW, pin=pyb.Pin.board.JP25)
- t2ch1.callback(disable_me)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement