Reading PWM using registers

Hi!

I need some guidance on getting started with measuring external PWM very accurately. I understand that I need to use registers and external interrupts but my brain is smoking trying to navigate what I exactly need and how to configure them.
Could somebody give me some hints/guidance where to start solving that problem?
Thanks!

What is "very accurately"? What frequency is the pwm signal and what precision do you need for measuring the duty cycle? Is using pulseIn() not accurate enough? What type of Arduino are you wanting to use?

I'm measuring RC reciever signal 1-2ms duty cycle @500hz. For prototyping I'm using Arduino Uno, if possible the final design would be running on ATtiny.

Using pulsein() I noticed random spikes and dips at readings.

Once the measuring is done I need to send the same signal out to RC speed controller and when a certain condition is met I need to cut the PWM connection between receiver and speed controller, thats what I'm working on.

why not use an interrupt, triggered on CHANGE, capturing timestamps in an array using micros() which is processed in loop() after a sufficient # of measurements.

#define N    20;
unsigned long usec [N];
int           idx = 0;
void ISR ()
{
      usecs [idx++] = micros().
}

Another approach with interrupts could be to update an output pin each time the input/interrupt pin changes. This would give an output signal as close as possible to the input signal. Meanwhile, loop() could be measuring the signal with pulseIn(), discarding any outliers in the data and testing for the "certain condition". If that condition is met, the interrupt could be disabled, preventing the pwm output signal from reaching the speed controller. Once the condition is no longer present, the interrupt can be re-enabled.

Timers can be configured for input capture, which uses hardware directly to time incoming
waveforms without the risk of software or interrupt jitter.

I've figured out that Timer1 input capture would do the trick but how to configure it to measure time between PWM falling and rising edge. If I set input capture flag to rise at CHANGE then how that ISR would look like?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.