PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

114 lines
4.9KB

  1. /* Encoder Library - SpeedTest - for measuring maximum Encoder speed
  2. * http://www.pjrc.com/teensy/td_libs_Encoder.html
  3. *
  4. * This example code is in the public domain.
  5. */
  6. // This SpeedTest example provides a simple way to verify how much
  7. // CPU time Encoder is consuming. Connect a DC voltmeter to the
  8. // output pin and measure the voltage while the encoder is stopped
  9. // or running at a very slow speed. Even though the pin is rapidly
  10. // pulsing, a DC voltmeter will show the average voltage. Due to
  11. // software timing, it will read a number much less than a steady
  12. // logic high, but this number will give you a baseline reading
  13. // for output with minimal interrupt overhead. Then increase the
  14. // encoder speed. The voltage will decrease as the processor spends
  15. // more time in Encoder's interrupt routines counting the pulses
  16. // and less time pulsing the output pin. When the voltage is
  17. // close to zero and will not decrease any farther, you have reached
  18. // the absolute speed limit. Or, if using a mechanical system where
  19. // you reach a speed limit imposed by your motors or other hardware,
  20. // the amount this voltage has decreased, compared to the baseline,
  21. // should give you a good approximation of the portion of available
  22. // CPU time Encoder is consuming at your maximum speed.
  23. // Encoder requires low latency interrupt response. Available CPU
  24. // time does NOT necessarily prove or guarantee correct performance.
  25. // If another library, like NewSoftSerial, is disabling interrupts
  26. // for lengthy periods of time, Encoder can be prevented from
  27. // properly counting the intput signals while interrupt are disabled.
  28. // This optional setting causes Encoder to use more optimized code,
  29. // but the downside is a conflict if any other part of your sketch
  30. // or any other library you're using requires attachInterrupt().
  31. // It must be defined before Encoder.h is included.
  32. //#define ENCODER_OPTIMIZE_INTERRUPTS
  33. #include <Encoder.h>
  34. #include "pins_arduino.h"
  35. // Change these two numbers to the pins connected to your encoder
  36. // or shift register circuit which emulates a quadrature encoder
  37. // case 1: both pins are interrupts
  38. // case 2: only first pin used as interrupt
  39. Encoder myEnc(5, 6);
  40. // Connect a DC voltmeter to this pin.
  41. const int outputPin = 12;
  42. /* This simple circuit, using a Dual Flip-Flop chip, can emulate
  43. quadrature encoder signals. The clock can come from a fancy
  44. function generator or a cheap 555 timer chip. The clock
  45. frequency can be measured with another board running FreqCount
  46. http://www.pjrc.com/teensy/td_libs_FreqCount.html
  47. +5V
  48. | Quadrature Encoder Signal Emulator
  49. Clock |
  50. Input o----*-------------------------- ---------------------------o Output1
  51. | |14 | |
  52. | _______|_______ | | _______________
  53. | | CD4013 | | | | CD4013 |
  54. | 5 | | 1 | | 9 | | 13
  55. ---------| D Q |-----|----*----| D Q |------o Output2
  56. | | | | | | |
  57. | | 3 | | | 11 | |
  58. | ----|> Clk | ---------|> Clk |
  59. | | | | |
  60. | 6 | | 8 | |
  61. | ----| S | ----| S |
  62. | | | | | | |
  63. | | 4 | _ | 2 | 10 | _ | 12
  64. | *----| R Q |--- *----| R Q |----
  65. | | | | | | | |
  66. | | |_______________| | |_______________| |
  67. | | | | |
  68. | | | 7 | |
  69. | | | | |
  70. --------------------------------------------------------------
  71. | | |
  72. | | |
  73. ----- ----- -----
  74. --- --- ---
  75. - - -
  76. */
  77. void setup() {
  78. pinMode(outputPin, OUTPUT);
  79. }
  80. #if defined(__AVR__) || defined(TEENSYDUINO)
  81. #define REGTYPE unsigned char
  82. #else
  83. #define REGTYPE unsigned long
  84. #endif
  85. void loop() {
  86. volatile int count = 0;
  87. volatile REGTYPE *reg = portOutputRegister(digitalPinToPort(outputPin));
  88. REGTYPE mask = digitalPinToBitMask(outputPin);
  89. while (1) {
  90. myEnc.read(); // Read the encoder while interrupts are enabled.
  91. noInterrupts();
  92. *reg |= mask; // Pulse the pin high, while interrupts are disabled.
  93. count = count + 1;
  94. *reg &= ~mask;
  95. interrupts();
  96. }
  97. }