Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- volatile int state = LOW; // must declare volatile, since it's
- // modified within the handler
- volatile int HighToLowCount = 0;
- volatile int LowToHighCount = 0;
- volatile int val;
- volatile int CurrentCount;
- volatile int Period;
- volatile int OldCount;
- volatile int PulseWidth;
- volatile int OverflowCount;
- volatile int timeroverflow;
- HardwareTimer timer1(1);
- void setup()
- {
- pinMode(BOARD_LED_PIN, OUTPUT);
- pinMode(0, INPUT);
- attachInterrupt(0, exti_handler, CHANGE);
- CurrentCount = 0;
- OldCount = 0;
- Period = 0;
- PulseWidth = 0;
- OverflowCount = 0;
- timeroverflow = 0;
- // Setup Counting Timers
- timer1.setChannel1Mode(TIMER_OUTPUT_COMPARE);
- timer1.pause();
- timer1.setCount(0);
- timer1.setPrescaleFactor(72); //run at 72 MHz/72 = 1MHz count every 1us
- timer1.setOverflow(65535);
- timer1.setCompare1(1);
- timer1.attachCompare1Interrupt(timer1_handler);
- timer1.refresh();
- timer1.resume();
- }
- void loop()
- {
- SerialUSB.print("LowToHighCount: ");
- SerialUSB.println(LowToHighCount);
- SerialUSB.print("HighToLowCount: ");
- SerialUSB.println(HighToLowCount);
- SerialUSB.print("Period: ");
- SerialUSB.println(Period);
- SerialUSB.print("PulseWidth: ");
- SerialUSB.println(PulseWidth);
- SerialUSB.print("Overflow: ");
- SerialUSB.println(OverflowCount);
- delay(1000);
- }
- void exti_handler()
- {
- timer1.pause();
- CurrentCount = timer1.getCount();
- val = digitalRead(0);
- if (val == HIGH)
- {
- LowToHighCount++;
- Period = CurrentCount;
- timer1.setCount(0);
- timer1.refresh();
- }
- else { // state must be LOW
- HighToLowCount++;
- PulseWidth = CurrentCount;
- }
- timer1.resume();
- }
- void timer1_handler()
- {
- OverflowCount = 0;
- timeroverflow++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement