timer.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. The Timers
  2. ==========
  3. The pyboard has 14 timers which each consist of an independent counter
  4. running at a user-defined frequency. They can be set up to run a function
  5. at specific intervals.
  6. The 14 timers are numbered 1 through 14, but 3 is reserved
  7. for internal use, and 5 and 6 are used for servo and ADC/DAC control.
  8. Avoid using these timers if possible.
  9. Let's create a timer object::
  10. >>> tim = pyb.Timer(4)
  11. Now let's see what we just created::
  12. >>> tim
  13. Timer(4)
  14. The pyboard is telling us that ``tim`` is attached to timer number 4, but
  15. it's not yet initialised. So let's initialise it to trigger at 10 Hz
  16. (that's 10 times per second)::
  17. >>> tim.init(freq=10)
  18. Now that it's initialised, we can see some information about the timer::
  19. >>> tim
  20. Timer(4, prescaler=624, period=13439, mode=UP, div=1)
  21. The information means that this timer is set to run at the peripheral
  22. clock speed divided by 624+1, and it will count from 0 up to 13439, at which
  23. point it triggers an interrupt, and then starts counting again from 0. These
  24. numbers are set to make the timer trigger at 10 Hz: the source frequency
  25. of the timer is 84MHz (found by running ``tim.source_freq()``) so we
  26. get 84MHz / 625 / 13440 = 10Hz.
  27. Timer counter
  28. -------------
  29. So what can we do with our timer? The most basic thing is to get the
  30. current value of its counter::
  31. >>> tim.counter()
  32. 21504
  33. This counter will continuously change, and counts up.
  34. Timer callbacks
  35. ---------------
  36. The next thing we can do is register a callback function for the timer to
  37. execute when it triggers (see the [switch tutorial](tut-switch) for an
  38. introduction to callback functions)::
  39. >>> tim.callback(lambda t:pyb.LED(1).toggle())
  40. This should start the red LED flashing right away. It will be flashing
  41. at 5 Hz (2 toggle's are needed for 1 flash, so toggling at 10 Hz makes
  42. it flash at 5 Hz). You can change the frequency by re-initialising the
  43. timer::
  44. >>> tim.init(freq=20)
  45. You can disable the callback by passing it the value ``None``::
  46. >>> tim.callback(None)
  47. The function that you pass to callback must take 1 argument, which is
  48. the timer object that triggered. This allows you to control the timer
  49. from within the callback function.
  50. We can create 2 timers and run them independently::
  51. >>> tim4 = pyb.Timer(4, freq=10)
  52. >>> tim7 = pyb.Timer(7, freq=20)
  53. >>> tim4.callback(lambda t: pyb.LED(1).toggle())
  54. >>> tim7.callback(lambda t: pyb.LED(2).toggle())
  55. Because the callbacks are proper hardware interrupts, we can continue
  56. to use the pyboard for other things while these timers are running.
  57. Making a microsecond counter
  58. ----------------------------
  59. You can use a timer to create a microsecond counter, which might be
  60. useful when you are doing something which requires accurate timing.
  61. We will use timer 2 for this, since timer 2 has a 32-bit counter (so
  62. does timer 5, but if you use timer 5 then you can't use the Servo
  63. driver at the same time).
  64. We set up timer 2 as follows::
  65. >>> micros = pyb.Timer(2, prescaler=83, period=0x3fffffff)
  66. The prescaler is set at 83, which makes this timer count at 1 MHz.
  67. This is because the CPU clock, running at 168 MHz, is divided by
  68. 2 and then by prescaler+1, giving a frequency of 168 MHz/2/(83+1)=1 MHz
  69. for timer 2. The period is set to a large number so that the timer
  70. can count up to a large number before wrapping back around to zero.
  71. In this case it will take about 17 minutes before it cycles back to
  72. zero.
  73. To use this timer, it's best to first reset it to 0::
  74. >>> micros.counter(0)
  75. and then perform your timing::
  76. >>> start_micros = micros.counter()
  77. ... do some stuff ...
  78. >>> end_micros = micros.counter()