Advertisement
dezulm

Maple ext interrupt pulse measurement

Mar 10th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. volatile int state = LOW; // must declare volatile, since it's
  2. // modified within the handler
  3. volatile int HighToLowCount = 0;
  4. volatile int LowToHighCount = 0;
  5. volatile int val;
  6. volatile int CurrentCount;
  7. volatile int Period;
  8. volatile int OldCount;
  9. volatile int PulseWidth;
  10. volatile int OverflowCount;
  11. volatile int timeroverflow;
  12.  
  13. HardwareTimer timer1(1);
  14.  
  15. void setup()
  16. {
  17. pinMode(BOARD_LED_PIN, OUTPUT);
  18. pinMode(0, INPUT);
  19. attachInterrupt(0, exti_handler, CHANGE);
  20.  
  21. CurrentCount = 0;
  22. OldCount = 0;
  23. Period = 0;
  24. PulseWidth = 0;
  25. OverflowCount = 0;
  26. timeroverflow = 0;
  27.  
  28. // Setup Counting Timers
  29. timer1.setChannel1Mode(TIMER_OUTPUT_COMPARE);
  30. timer1.pause();
  31. timer1.setCount(0);
  32. timer1.setPrescaleFactor(72); //run at 72 MHz/72 = 1MHz count every 1us
  33. timer1.setOverflow(65535);
  34. timer1.setCompare1(1);
  35. timer1.attachCompare1Interrupt(timer1_handler);
  36. timer1.refresh();
  37. timer1.resume();
  38.  
  39.  
  40. }
  41.  
  42. void loop()
  43. {
  44. SerialUSB.print("LowToHighCount: ");
  45. SerialUSB.println(LowToHighCount);
  46. SerialUSB.print("HighToLowCount: ");
  47. SerialUSB.println(HighToLowCount);
  48. SerialUSB.print("Period: ");
  49. SerialUSB.println(Period);
  50. SerialUSB.print("PulseWidth: ");
  51. SerialUSB.println(PulseWidth);
  52. SerialUSB.print("Overflow: ");
  53. SerialUSB.println(OverflowCount);
  54. delay(1000);
  55. }
  56.  
  57. void exti_handler()
  58. {
  59. timer1.pause();
  60. CurrentCount = timer1.getCount();
  61. val = digitalRead(0);
  62.  
  63.  
  64. if (val == HIGH)
  65. {
  66. LowToHighCount++;
  67. Period = CurrentCount;
  68. timer1.setCount(0);
  69. timer1.refresh();
  70. }
  71. else { // state must be LOW
  72. HighToLowCount++;
  73. PulseWidth = CurrentCount;
  74. }
  75.  
  76. timer1.resume();
  77. }
  78.  
  79. void timer1_handler()
  80. {
  81. OverflowCount = 0;
  82. timeroverflow++;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement