timer.rst 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Hardware timers
  2. ===============
  3. Timers can be used for a great variety of tasks, calling a function periodically,
  4. counting events, and generating a PWM signal are among the most common use cases.
  5. Each timer consists of two 16-bit channels and this channels can be tied together to
  6. form one 32-bit timer. The operating mode needs to be configured per timer, but then
  7. the period (or the frequency) can be independently configured on each channel.
  8. By using the callback method, the timer event can call a Python function.
  9. Example usage to toggle an LED at a fixed frequency::
  10. from machine import Timer
  11. from machine import Pin
  12. led = Pin('GP16', mode=Pin.OUT) # enable GP16 as output to drive the LED
  13. tim = Timer(3) # create a timer object using timer 3
  14. tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode
  15. tim_ch = tim.channel(Timer.A, freq=5) # configure channel A at a frequency of 5Hz
  16. tim_ch.irq(handler=lambda t:led.toggle(), trigger=Timer.TIMEOUT) # toggle a LED on every cycle of the timer
  17. Example using named function for the callback::
  18. from machine import Timer
  19. from machine import Pin
  20. tim = Timer(1, mode=Timer.PERIODIC, width=32)
  21. tim_a = tim.channel(Timer.A | Timer.B, freq=1) # 1 Hz frequency requires a 32 bit timer
  22. led = Pin('GP16', mode=Pin.OUT) # enable GP16 as output to drive the LED
  23. def tick(timer): # we will receive the timer object when being called
  24. global led
  25. led.toggle() # toggle the LED
  26. tim_a.irq(handler=tick, trigger=Timer.TIMEOUT) # create the interrupt
  27. Further examples::
  28. from machine import Timer
  29. tim1 = Timer(1, mode=Timer.ONE_SHOT) # initialize it in one shot mode
  30. tim2 = Timer(2, mode=Timer.PWM) # initialize it in PWM mode
  31. tim1_ch = tim1.channel(Timer.A, freq=10, polarity=Timer.POSITIVE) # start the event counter with a frequency of 10Hz and triggered by positive edges
  32. tim2_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=5000) # start the PWM on channel B with a 50% duty cycle
  33. tim2_ch.freq(20) # set the frequency (can also get)
  34. tim2_ch.duty_cycle(3010) # set the duty cycle to 30.1% (can also get)
  35. tim2_ch.duty_cycle(3020, Timer.NEGATIVE) # set the duty cycle to 30.2% and change the polarity to negative
  36. tim2_ch.period(2000000) # change the period to 2 seconds
  37. Additional constants for Timer class
  38. ------------------------------------
  39. .. data:: Timer.PWM
  40. PWM timer operating mode.
  41. .. data:: Timer.A
  42. .. data:: Timer.B
  43. Selects the timer channel. Must be ORed (``Timer.A`` | ``Timer.B``) when
  44. using a 32-bit timer.
  45. .. data:: Timer.POSITIVE
  46. .. data:: Timer.NEGATIVE
  47. Timer channel polarity selection (only relevant in PWM mode).
  48. .. data:: Timer.TIMEOUT
  49. .. data:: Timer.MATCH
  50. Timer channel IRQ triggers.