10 years, 10 months ago.

PWM measuring with timer interrupt

Hi, I have a problem with PWM measuring. I want to measure PWM and I already have two pins with interrupt on rising and falling edge. The Timer starts on rising edge and stops on falling edge. Measured time from timer is duty cycle.

But I dont want to measure every duty cycle. I would like to do a timer interrupt which would start measuring a duty cycle by interrupt on falling and rising edge. but I dont know, how to write this code. Is there anybody, who can help me? Vojtěch

What do you mean? You only want to measure it once and not continiously?

posted by Erik - 29 Jan 2014

I want to measure continuously once in every two seconds. Not every pulse.

posted by Vojtěch Kolomazník 29 Jan 2014

Hi, on my mind the best is to work with a capture interrupt. First you set the capture on rising edge and get the time from a free running timer, next you set the capture on falling edge. You calculate the high time by taking the differnce between captured timer values. The advantage is that the resolution can be as high as the count rate of the timer without any error because of interrupt response time

posted by Christian Jorde 30 Jan 2014

2 Answers

10 years, 10 months ago.

..

10 years, 10 months ago.

You can attach the interrupts like you are doing now, so it does its measurements. When it completed its duty cycle measurement, remove the interrupts (pin.rise(NULL); does that for removing the rising edge interrupt).

Then you use a ticker to re-attach the interrupts every two seconds and make a new measurement.

Big thanks to you :) I did it as you said and it works :) But results are a bit different, when I measured 50hz PWM with duty cycle of 1500us the result was 1496us instead of 1500us. Every measuring missed 4us. I dont know where is the mistake, but I miserably solved it by: (timer_PWM.read_us() + 4 ); :) Thanks, Vojtěch

posted by Vojtěch Kolomazník 29 Jan 2014