pyb.Timer.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. .. currentmodule:: pyb
  2. .. _pyb.Timer:
  3. class Timer -- control internal timers
  4. ======================================
  5. Timers can be used for a great variety of tasks. At the moment, only
  6. the simplest case is implemented: that of calling a function periodically.
  7. Each timer consists of a counter that counts up at a certain rate. The rate
  8. at which it counts is the peripheral clock frequency (in Hz) divided by the
  9. timer prescaler. When the counter reaches the timer period it triggers an
  10. event, and the counter resets back to zero. By using the callback method,
  11. the timer event can call a Python function.
  12. Example usage to toggle an LED at a fixed frequency::
  13. tim = pyb.Timer(4) # create a timer object using timer 4
  14. tim.init(freq=2) # trigger at 2Hz
  15. tim.callback(lambda t:pyb.LED(1).toggle())
  16. Example using named function for the callback::
  17. def tick(timer): # we will receive the timer object when being called
  18. print(timer.counter()) # show current timer's counter value
  19. tim = pyb.Timer(4, freq=1) # create a timer object using timer 4 - trigger at 1Hz
  20. tim.callback(tick) # set the callback to our tick function
  21. Further examples::
  22. tim = pyb.Timer(4, freq=100) # freq in Hz
  23. tim = pyb.Timer(4, prescaler=0, period=99)
  24. tim.counter() # get counter (can also set)
  25. tim.prescaler(2) # set prescaler (can also get)
  26. tim.period(199) # set period (can also get)
  27. tim.callback(lambda t: ...) # set callback for update interrupt (t=tim instance)
  28. tim.callback(None) # clear callback
  29. *Note:* Timer(2) and Timer(3) are used for PWM to set the intensity of LED(3)
  30. and LED(4) respectively. But these timers are only configured for PWM if
  31. the intensity of the relevant LED is set to a value between 1 and 254. If
  32. the intensity feature of the LEDs is not used then these timers are free for
  33. general purpose use. Similarly, Timer(5) controls the servo driver, and
  34. Timer(6) is used for timed ADC/DAC reading/writing. It is recommended to
  35. use the other timers in your programs.
  36. *Note:* Memory can't be allocated during a callback (an interrupt) and so
  37. exceptions raised within a callback don't give much information. See
  38. :func:`micropython.alloc_emergency_exception_buf` for how to get around this
  39. limitation.
  40. Constructors
  41. ------------
  42. .. class:: pyb.Timer(id, ...)
  43. Construct a new timer object of the given id. If additional
  44. arguments are given, then the timer is initialised by ``init(...)``.
  45. ``id`` can be 1 to 14.
  46. Methods
  47. -------
  48. .. method:: Timer.init(\*, freq, prescaler, period)
  49. Initialise the timer. Initialisation must be either by frequency (in Hz)
  50. or by prescaler and period::
  51. tim.init(freq=100) # set the timer to trigger at 100Hz
  52. tim.init(prescaler=83, period=999) # set the prescaler and period directly
  53. Keyword arguments:
  54. - ``freq`` --- specifies the periodic frequency of the timer. You might also
  55. view this as the frequency with which the timer goes through one complete cycle.
  56. - ``prescaler`` [0-0xffff] - specifies the value to be loaded into the
  57. timer's Prescaler Register (PSC). The timer clock source is divided by
  58. (``prescaler + 1``) to arrive at the timer clock. Timers 2-7 and 12-14
  59. have a clock source of 84 MHz (pyb.freq()[2] \* 2), and Timers 1, and 8-11
  60. have a clock source of 168 MHz (pyb.freq()[3] \* 2).
  61. - ``period`` [0-0xffff] for timers 1, 3, 4, and 6-15. [0-0x3fffffff] for timers 2 & 5.
  62. Specifies the value to be loaded into the timer's AutoReload
  63. Register (ARR). This determines the period of the timer (i.e. when the
  64. counter cycles). The timer counter will roll-over after ``period + 1``
  65. timer clock cycles.
  66. - ``mode`` can be one of:
  67. - ``Timer.UP`` - configures the timer to count from 0 to ARR (default)
  68. - ``Timer.DOWN`` - configures the timer to count from ARR down to 0.
  69. - ``Timer.CENTER`` - configures the timer to count from 0 to ARR and
  70. then back down to 0.
  71. - ``div`` can be one of 1, 2, or 4. Divides the timer clock to determine
  72. the sampling clock used by the digital filters.
  73. - ``callback`` - as per Timer.callback()
  74. - ``deadtime`` - specifies the amount of "dead" or inactive time between
  75. transitions on complimentary channels (both channels will be inactive)
  76. for this time). ``deadtime`` may be an integer between 0 and 1008, with
  77. the following restrictions: 0-128 in steps of 1. 128-256 in steps of
  78. 2, 256-512 in steps of 8, and 512-1008 in steps of 16. ``deadtime``
  79. measures ticks of ``source_freq`` divided by ``div`` clock ticks.
  80. ``deadtime`` is only available on timers 1 and 8.
  81. You must either specify freq or both of period and prescaler.
  82. .. method:: Timer.deinit()
  83. Deinitialises the timer.
  84. Disables the callback (and the associated irq).
  85. Disables any channel callbacks (and the associated irq).
  86. Stops the timer, and disables the timer peripheral.
  87. .. method:: Timer.callback(fun)
  88. Set the function to be called when the timer triggers.
  89. ``fun`` is passed 1 argument, the timer object.
  90. If ``fun`` is ``None`` then the callback will be disabled.
  91. .. method:: Timer.channel(channel, mode, ...)
  92. If only a channel number is passed, then a previously initialized channel
  93. object is returned (or ``None`` if there is no previous channel).
  94. Otherwise, a TimerChannel object is initialized and returned.
  95. Each channel can be configured to perform pwm, output compare, or
  96. input capture. All channels share the same underlying timer, which means
  97. that they share the same timer clock.
  98. Keyword arguments:
  99. - ``mode`` can be one of:
  100. - ``Timer.PWM`` --- configure the timer in PWM mode (active high).
  101. - ``Timer.PWM_INVERTED`` --- configure the timer in PWM mode (active low).
  102. - ``Timer.OC_TIMING`` --- indicates that no pin is driven.
  103. - ``Timer.OC_ACTIVE`` --- the pin will be made active when a compare match occurs (active is determined by polarity)
  104. - ``Timer.OC_INACTIVE`` --- the pin will be made inactive when a compare match occurs.
  105. - ``Timer.OC_TOGGLE`` --- the pin will be toggled when an compare match occurs.
  106. - ``Timer.OC_FORCED_ACTIVE`` --- the pin is forced active (compare match is ignored).
  107. - ``Timer.OC_FORCED_INACTIVE`` --- the pin is forced inactive (compare match is ignored).
  108. - ``Timer.IC`` --- configure the timer in Input Capture mode.
  109. - ``Timer.ENC_A`` --- configure the timer in Encoder mode. The counter only changes when CH1 changes.
  110. - ``Timer.ENC_B`` --- configure the timer in Encoder mode. The counter only changes when CH2 changes.
  111. - ``Timer.ENC_AB`` --- configure the timer in Encoder mode. The counter changes when CH1 or CH2 changes.
  112. - ``callback`` - as per TimerChannel.callback()
  113. - ``pin`` None (the default) or a Pin object. If specified (and not None)
  114. this will cause the alternate function of the the indicated pin
  115. to be configured for this timer channel. An error will be raised if
  116. the pin doesn't support any alternate functions for this timer channel.
  117. Keyword arguments for Timer.PWM modes:
  118. - ``pulse_width`` - determines the initial pulse width value to use.
  119. - ``pulse_width_percent`` - determines the initial pulse width percentage to use.
  120. Keyword arguments for Timer.OC modes:
  121. - ``compare`` - determines the initial value of the compare register.
  122. - ``polarity`` can be one of:
  123. - ``Timer.HIGH`` - output is active high
  124. - ``Timer.LOW`` - output is active low
  125. Optional keyword arguments for Timer.IC modes:
  126. - ``polarity`` can be one of:
  127. - ``Timer.RISING`` - captures on rising edge.
  128. - ``Timer.FALLING`` - captures on falling edge.
  129. - ``Timer.BOTH`` - captures on both edges.
  130. Note that capture only works on the primary channel, and not on the
  131. complimentary channels.
  132. Notes for Timer.ENC modes:
  133. - Requires 2 pins, so one or both pins will need to be configured to use
  134. the appropriate timer AF using the Pin API.
  135. - Read the encoder value using the timer.counter() method.
  136. - Only works on CH1 and CH2 (and not on CH1N or CH2N)
  137. - The channel number is ignored when setting the encoder mode.
  138. PWM Example::
  139. timer = pyb.Timer(2, freq=1000)
  140. ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=8000)
  141. ch3 = timer.channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=16000)
  142. .. method:: Timer.counter([value])
  143. Get or set the timer counter.
  144. .. method:: Timer.freq([value])
  145. Get or set the frequency for the timer (changes prescaler and period if set).
  146. .. method:: Timer.period([value])
  147. Get or set the period of the timer.
  148. .. method:: Timer.prescaler([value])
  149. Get or set the prescaler for the timer.
  150. .. method:: Timer.source_freq()
  151. Get the frequency of the source of the timer.
  152. class TimerChannel --- setup a channel for a timer
  153. ==================================================
  154. Timer channels are used to generate/capture a signal using a timer.
  155. TimerChannel objects are created using the Timer.channel() method.
  156. Methods
  157. -------
  158. .. method:: timerchannel.callback(fun)
  159. Set the function to be called when the timer channel triggers.
  160. ``fun`` is passed 1 argument, the timer object.
  161. If ``fun`` is ``None`` then the callback will be disabled.
  162. .. method:: timerchannel.capture([value])
  163. Get or set the capture value associated with a channel.
  164. capture, compare, and pulse_width are all aliases for the same function.
  165. capture is the logical name to use when the channel is in input capture mode.
  166. .. method:: timerchannel.compare([value])
  167. Get or set the compare value associated with a channel.
  168. capture, compare, and pulse_width are all aliases for the same function.
  169. compare is the logical name to use when the channel is in output compare mode.
  170. .. method:: timerchannel.pulse_width([value])
  171. Get or set the pulse width value associated with a channel.
  172. capture, compare, and pulse_width are all aliases for the same function.
  173. pulse_width is the logical name to use when the channel is in PWM mode.
  174. In edge aligned mode, a pulse_width of ``period + 1`` corresponds to a duty cycle of 100%
  175. In center aligned mode, a pulse width of ``period`` corresponds to a duty cycle of 100%
  176. .. method:: timerchannel.pulse_width_percent([value])
  177. Get or set the pulse width percentage associated with a channel. The value
  178. is a number between 0 and 100 and sets the percentage of the timer period
  179. for which the pulse is active. The value can be an integer or
  180. floating-point number for more accuracy. For example, a value of 25 gives
  181. a duty cycle of 25%.