![]() Uint32_t cycleTime = pulseHighTime + pulseLowTime Serial.print("Low time (microseconds): ") Serial.print("High time (microseconds): ") Display the pulse length in microseconds PulseHighTime = fallingEdgeTime - firstRisingEdgeTime TCCR1B |= _BV(ICES1) // Switch to Rising Edge TCCR1B &= ~_BV(ICES1) // Switch to Falling EdgeįallingEdgeTime = overflows // Upper 16 bitsįallingEdgeTime = (fallingEdgeTime << 16) | ICR1 PulseLowTime = secondRisingEdgeTime - fallingEdgeTime įirstRisingEdgeTime = overflows // Upper 16 bitsįirstRisingEdgeTime = (firstRisingEdgeTime << 16) | ICR1 SecondRisingEdgeTime = (secondRisingEdgeTime << 16) | ICR1 SecondRisingEdgeTime = overflows // Upper 16 bits so this is the second rising edge, ending the low part If (firstRisingEdgeTime) // Already have the first rising edge. and the timer count was close to zero, count the If an overflow happened but has not been handled yet Static uint32_t secondRisingEdgeTime = 0 ![]() TIMSK1 |= _BV(TOIE1) // Enable Timer 1 Overflow Interrupt TIMSK1 |= _BV(ICIE1) // Enable Timer 1 Input Capture Interrupt _BV(ICES1) // Input Capture Edge Select (1=Rising, 0=Falling) ![]() TCCR1B = _BV(CS10) | // start Timer 1, no prescaler TIFR1 |= _BV(TOV1) // clear Overflow Flag so we don't get a bogus interrupt TIFR1 |= _BV(ICF1) // clear Input Capture Flag so we don't get a bogus interrupt For testing, uncomment one of these lines and connect Note: Since this uses Timer1, Pin 9 and Pin 10 can't be used for Here is an example sketch I wrote to demonstrate the use of the Input Capture Register of Timer1: // Measures the HIGH width, LOW width, frequency, and duty-cycle of a pulse train ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |