I have two timers as pwm that drive two pins. How can the timers (actually pin output) be synchronised upon start and change of PWM width?
I would like to do this in python, without inline assembler or DMA.
With the attached code below I get the following results.
I can't tell the difference between the timers but I guess it is less than 25µs (sirst comment)
The difference between change of X1 and X2 at pwm width change is approx. 10µs (second comment).
I would like to achieve differences around 1-2 µs.
One thing that have crossed my mind in this case is to connect one pwm timer to both pins, is that possible and how to I accomplish that?
Suggestions?
/Klas
Code: Select all
import pyb
hled = pyb.LED(4)
hled.off()
heat = 0
oldHeat =0
sw=pyb.Switch()
pinh =pyb.Pin('X2',pyb.Pin.OUT_PP) #X2 has timer5, ch2
pinv =pyb.Pin('X1',pyb.Pin.OUT_PP) #X1 has timer2, ch1
trigger = pyb.Pin('X5',pyb.Pin.OUT_PP) #X5 a trigger debug pin for time measurement
trigger.low()
timh = pyb.Timer(5,freq=1000)
timv = pyb.Timer(2,freq=1000)
trigger.high()
# Reset the two timers to get them start at the same time, or at least as good as possible.
timh.counter(0)
timv.counter(0)
# The above solution yields a difference off approximately 7 µs beetween the levelchange two pins, not good enough.
trigger.low()
chh =timh.channel(2, pyb.Timer.PWM,pin=pinh)
chv =timv.channel(1, pyb.Timer.PWM,pin=pinv)
chv.pulse_width_percent(0)
chv.pulse_width_percent(0)
def pressed():
global heat
pyb.LED(3).toggle()
heat = heat + 200
if 1000 < heat:
heat = 0
sw.callback(pressed)
while True:
if oldHeat != heat:
oldHeat = heat
pwmheat = heat/10
trigger.high()
# Change the pulse width
chv.pulse_width_percent(pwmheat)
chh.pulse_width_percent(pwmheat)
# The above solution yields a difference off approximately 7 µs beetween the levelchange two pins, not good enough.
trigger.low()
hled.intensity(heat>>2)
print(heat, heat>>2, pwmheat)
pyb.delay(100)