Analog Comparator issues (ATtiny24)

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

it's been a while ago, but it's nice to be back at avrfreaks.net 8)

I need to measure the duty cycle of a PWM signal. The PWM frequency can vary in the range of 100 Hz to 1kHz. The PWM amplitude can vary in the range of 100mV to 2.5V, too.

The simpliest way to measure the duty cycle is by utilizing the input capture feature of Timer1 (ATtiny24). The PWM signal isn't as simple as a TTL signal, so I want to use the build in analog comparator as input capture trigger source.

With Timer0 I want to generate a PWM which goes through a low pass filter to the positive analog comparator input. This my reference DC voltage.

The PWM signal I need to measure, is connected to the negative analog comparator input.

My question is, how much ripple can I tolerate on my DC reference? I have attached a picture which shows the problem. The PWM input signal must pass a low pass filter before it reaches the analog comparator input. Thus it is possible, that one PWM edge triggers several input capture events.

I looked at the ATtiny24 datasheet but couldn't find any information about the analog comparator hysteresis. The analog comparator output is not connected to an IO pin. So I can't add any hysteresis via a resistor.
Any ideas?

Regards
Sebastian

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmm--how about this, which should work unless the PWM signal has very low or very high duty cycles: Put a filter on the incoming PWM to generate a DC voltage, and use that as your analog comparator "reference" input.

Quote:

The PWM amplitude can vary in the range of 100mV to 2.5V, too.

Next alternative: generate ~50mV signal and use that as the analog comparator "reference" in all cases.

Next alternative: Always apply a gain to the incoming PWM signal. Using an op amp powered by Gnd & Vcc should limit the values seen by the AVR, but should be close enough to the rails so that logic-level tripping can always be used.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The comparator has no built-in hysteresis. And, as you observe, it is difficult to add externally. If you REALLY need hysteresis, it would be simpler to use an external comparator.

If the PWM signal varies in amplitude and/or DC level, the easiest way to detect it would be to feed the PWM, unfiltered, into one side of the comparator. Also. feed the same PWM into an RC lowpass filter which feeds the other side of the comparator. The RC output should always be close to the average voltage of the PWM and thus, somewhere between the max and min values. I would set the corner frequency of the filter to be maybe 1/10 of the lowest PWM frequency.

It is not clear to me what the red line in your graph represents or where it comes from.

There should be no problem with several or even many capture events. You deal with THAT in software!

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmm--too bad the 20x gain via ADC cannot be used.

However, as ADC channels could be used as one leg of the comparator you could make a resistor ladder with different voltages on various ADC pins, and use the one most appropriate for the "current" level of the incoming signal.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can filter the hell out of the PWM by using an external opamp low pass filter. :D Very good filtering can be achieved with a single opamp (second order). With a quad opamp package you can really go overboard.

Note that for a step change in the PWM applied to a higher order filter there will be a small overshoot at the output before the output settles to the final value (this determines the settling time of the filter). Be aware of it and it is easy to design around it. :D

If you think education is expensive, try ignorance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Many thanks for your valuable help and sorry for my late response!

ka7ehk wrote:
It is not clear to me what the red line in your graph represents or where it comes from.
The blue line represents my analog comparator reference signal generated with a PWM and filtered via a RC low pass filter. The red line shall represent the rising edge of the PWM signal I want to measure. Yes, it doesn't really look like an rising edge but the picture shall only show how several input capture events can be triggered on only one in coming edge of the PWM signal if I have some ripple on my reference signal (which I will always have).

ka7ehk wrote:
There should be no problem with several or even many capture events. You deal with THAT in software!
How can I deal that in software? There might be as many capture events on the rising edge like on the falling edge, so If I calculate the average value I'll get a nearly perfect duty cycle result - but the calculated frequency (which, btw., I have to measure, too) would be far too high.

At least your suggestions (Lee, Jim) to connect the PWM signal to the analog comparator on one pin directly and to the other pin via a low pass filter look promising to me. The ripple on the filtered PWM signal has always the same frequency as the unfiltered PWM signal, so I shouldn't get several input capture events on each edge. And it is a very simple solution - I like it simple.

Once again, many thanks!!!

Regards
Sebastian