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.

FreqCount.cpp 3.2KB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* FreqCount Library, for measuring frequencies
  2. * http://www.pjrc.com/teensy/td_libs_FreqCount.html
  3. * Copyright (c) 2014 PJRC.COM, LLC - Paul Stoffregen <paul@pjrc.com>
  4. *
  5. * Version 1.1
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "FreqCount.h"
  26. #include "util/FreqCountTimers.h"
  27. #if(!defined(__IMXRT1062__))
  28. static uint16_t count_msw;
  29. static uint16_t gate_length;
  30. static uint16_t gate_index;
  31. void FreqCountClass::begin(uint16_t msec)
  32. {
  33. if (msec < 1) return;
  34. gate_index = 0;
  35. count_msw = 0;
  36. count_prev = 0;
  37. count_ready = 0;
  38. counter_init();
  39. gate_length = timer_init(msec);
  40. uint8_t status = SREG;
  41. cli();
  42. timer_start();
  43. timer_isr_latency_delay();
  44. counter_start();
  45. SREG = status;
  46. }
  47. uint8_t FreqCountClass::available(void)
  48. {
  49. return count_ready;
  50. }
  51. uint32_t FreqCountClass::read(void)
  52. {
  53. uint32_t count;
  54. uint8_t status;
  55. status = SREG;
  56. cli();
  57. #if defined(TIMER_USE_TIMER2) && F_CPU == 12000000L
  58. float correct = count_output * 0.996155;
  59. count = (uint32_t) (correct+0.5);
  60. #else
  61. count = count_output;
  62. #endif
  63. count_ready = 0;
  64. SREG = status;
  65. return count;
  66. }
  67. void FreqCountClass::end(void)
  68. {
  69. timer_shutdown();
  70. counter_shutdown();
  71. }
  72. ISR(TIMER_ISR_VECTOR)
  73. {
  74. uint16_t lsw, msw;
  75. uint32_t count;
  76. uint16_t index, length;
  77. lsw = counter_read();
  78. msw = count_msw;
  79. if (counter_overflow()) {
  80. counter_overflow_reset();
  81. if (lsw < 0xFA00) {
  82. msw = msw + 1;
  83. count_msw = msw;
  84. } else {
  85. count_msw = msw + 1;
  86. }
  87. }
  88. index = gate_index + 1;
  89. length = gate_length;
  90. if (index >= length) {
  91. gate_index = 0;
  92. count = ((uint32_t)msw << 16) + lsw;
  93. count_output = count - count_prev;
  94. count_prev = count;
  95. count_ready = 1;
  96. restore_other_interrupts();
  97. } else {
  98. if (index == length - 1) disable_other_interrupts();
  99. gate_index = index;
  100. }
  101. }
  102. #else
  103. void FreqCountClass::begin(uint32_t usec)
  104. {
  105. counter_init();
  106. timer_init(usec);
  107. //counter_start();
  108. }
  109. uint8_t FreqCountClass::available(void)
  110. {
  111. return count_ready;
  112. }
  113. uint32_t FreqCountClass::read(void)
  114. {
  115. count_output = count - count_prev;
  116. count_prev = count;
  117. //Serial.println(count - count_prev);
  118. count_prev = count;
  119. count_ready = 0;
  120. return count_output;
  121. }
  122. #endif
  123. FreqCountClass FreqCount;