2

I am creating pwm from an arduino pin with 50% duty cycle. I just want to know how can i detect its every falling edge using timer interrupt? I don't want to connect that PWM pin to an external interrupt pin. Please help me.

asked Feb 27, 2016 at 15:18

2 Answers 2

3

You can define an interrupt service routine (ISR) for each Timer and PWM pin that it handles. These are known as TIMERn_COMPA_vect, TIMERn_COMPB_vect and TIMERn_COMPC_vect. The Mega has six Timers (n=0..5).

ISR(TIMERn_COMPA_vect)
{
// Timer/Counter compare match A
}

Check the PWM configuration for how the pin is set, cleared or toggled.

Cheers!

answered Feb 27, 2016 at 16:51
1
  • 1
    Besides creating the ISR(... you also need to enable that interrupt (e.g. TIMSK1 |= _BV(OCIE1A)) Commented Feb 27, 2016 at 17:11
2

Referring to the datasheet of ATMega328P: If you want to generate an interrupt every falling edge you must know if the Pin is inverse mode or not. As you can see from the timing diagram the Pin stays high until the compare unit matches (current value of the TCNCT == value stored in OCRn ) and then goes low in the normal mode or works as opposite in the inverting mode.

This could be regulated by changing the bits COMnx in the TCCRn register. If, for example: COM0 = 1 and COM1 = 1 the table says: Set OC0A on Compare Match, clear OC0A at BOTTOM, (inverting mode).

Now with these settings the falling edge happens when the timer overflow, so the ISR must occur when the timer reach its maximum value.

PWM timing diagram

answered Feb 27, 2016 at 19:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.