PWM interrupt cmp1 and tc simultaneously?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
user_4495571
Level 2
Level 2
5 replies posted 10 sign-ins 5 questions asked

Hi there,

I need an PWM signal so that I can trigger specific action depending on the level of the PWM. So I set up the PWM with interrupts enabled for terminal count event and compare 1 event. I expected to get an interrupt when the compare value matches the counter and at the end when the counter reaches zero. But I get two interrupts simultaneously, or only microseconds apart.

I clock the PWM with 10 kHz and set the period to 12000 and the compare value to 9000. The compare type is greater. I get a PWM signal with 300ms high value and a period of 1200ms as expected. But not compare 1 interrupt after 300ms as expected.

What do I miss here?

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.

user,

Simple.  The issue is that the PWM is a count-down counters.  Therefore the count starts with period value and counts down.  A "Greater than" occurs once the period value is reloaded into the counter after the terminal count.

The simple fix is to use "Lesser than" and use a NOT gate on the PWM output.

I've attached an example project with these changes.

Here's a scope pic of the outputs:

Len_CONSULTRON_0-1618272066691.png

 

Len
"Engineering is an Art. The Art of Compromise."

View solution in original post

0 Likes
6 Replies
user_4495571
Level 2
Level 2
5 replies posted 10 sign-ins 5 questions asked

If I change CMP Type 1 from greater to less I get two distinct interrupts 300ms apart. I need to start with a high pulse though..

0 Likes
lock attach
Attachments are accessible only for community members.

user,

Simple.  The issue is that the PWM is a count-down counters.  Therefore the count starts with period value and counts down.  A "Greater than" occurs once the period value is reloaded into the counter after the terminal count.

The simple fix is to use "Lesser than" and use a NOT gate on the PWM output.

I've attached an example project with these changes.

Here's a scope pic of the outputs:

Len_CONSULTRON_0-1618272066691.png

 

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
Ekta_N
Moderator
Moderator
Moderator
750 replies posted First like given 250 solutions authored

Hi @user_4495571 ,

As correctly mentioned by Len the PWM is implemented as a count down counter, this is mentioned in the PWM datasheet as well:

Ekta_0-1618293108593.png

Using a "lesser than" option with a not gate should resolve the issue.

Thank and Regards

Ekta

0 Likes
user_4495571
Level 2
Level 2
5 replies posted 10 sign-ins 5 questions asked

Hi,

thank you Len and Ekta.

Yes, I already realized it counts down and a lesser compare type helps. What i still don't understand: The 'Interrupt On Compare 1 Event' gets fired the first time the counter is greater than the compare value? So that is why it fires immediately on period start?

0 Likes

The PWM counter is reloaded with the period value.  The period value IS GREATER than the compare value.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
Ekta_N
Moderator
Moderator
Moderator
750 replies posted First like given 250 solutions authored

Hi @user_4495571 

You have set the  compare type is greater, that the reason the Interrupt On Compare 1 Event' gets fired the first time the counter is greater than the compare value.

Best Regards

Ekta

0 Likes