PWM - Input capture Interrupt

Hello everybody,

I wanted to measure the width of a PWM signal, but the ISR can not be called, for testing I've tried Serial.print ("toook4").to generate a PWM Signal i connect Pin 10 to Pin 8. fyi i use an arduino mega
maybe someone can help me?

thanks

void setup() {

Serial.begin(9600);
analogWrite(10,255/2);
noInterrupts (); // protected code
// reset Timer 1
TCCR1A= 0;
TCCR1B= 0;
TCNT1= 0;

TIFR1|= (1<<ICF1); // clear flags so we don't get a bogus interrupt
// start Timer 1, no prescaler
TCCR1B|=(1<<CS10); // plus Input Capture Edge Select (rising on D8)
TCCR1B|=(1<<ICES1);// Input capture Eadge Select

TIMSK1|=(1<<ICIE1); // Enable Timer 1 Input Capture Interrupt Enable
interrupts ();
}
ISR(TIMER1_CAPT_vect)
{
Serial.print ("Took: 4");
unsigned int timer1CounterValue;
timer1CounterValue = ICR1; // see datasheet, page 117 (accessing 16-bit registers)

}

aaaaaaaaaaaaaa.GIF

You don't need all that. Just make sure timer1 is turned on for normal counting, TCCRB1 = 1, no prescaler so every tick is 62.5ns. Save TCNT1 before and after to a uint32_t and do the math. You don't need to mess with the HIGH and LOW bytes as the compiler will take care of that for you. The high ordered byte is double-buffered so this is actually an atomic instruction. With no prescaler the ticks rollover at 65,535 so your maximum timing window (without counting rollover) is just over 4ms or about 244Hz. Bigger gaps than that and you need to adjust the prescaler, next stop 8 which will give you 0.5us ticks and extend your timing capabilities to about 32ms.

Did you want the number of clock cycles between rising edge and falling edge?

volatile unsigned PulseTime = 0;
ISR(TIMER1_CAPT_vect)
{
  static unsigned RisingEdgeTime = 0;
  static unsigned FallingEdgeTime = 0;
  
  // Which edge is armed?
  if (TCCR1B & (1 << ICES1))
  {
    // Rising Edge
    RisingEdgeTime = ICR1;
    TCCR1B &= ~(1 << ICES1); // Switch to Falling Edge
  }
  else
  {
    // Falling Edge
    FallingEdgeTime = ICR1;
    TCCR1B |= (1 << ICES1); // Switch to Rising Edge
    PulseTime = FallingEdgeTime - RisingEdgeTime;
  }
}


void loop() {
  unsigned pulseTime = 0;
  noInterrupts();
  pulseTime = PulseTime;
  PulseTime = 0;
  interrupts();


  if (pulseTime)
  {
    Serial.println(pulseTime);
  }
}[code]

hi,
yes i want the on time but it is working now on timer 4. but i would like to know why not on timer 1

samuel522:
it it is working now on timer 4. but i would like to know why not on timer 1

I missed it before but after some research I see that you can't (easily) use Timer1 for Input Capture when you use Arduino MEGA. The Arduino MEGA doesn't have a pin that connects to Input Capture Pin for Timer1 (ICP1). You can use:
Timer4 ICP4 (Pin 49)
Timer5 ICP5 (Pin 48)
The ICP1 pin is #47 on the chip (counting counterclockwise from the 'pin 1' corner). It is not connected to any Arduino MEGA pin but you could solder a wire to it if you were desperate.
Timer0, Timer2, and Timer3 don't have the Input Capture feature.

johnwasser thank u