Atmega328P analog comparator hysteresis

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

Greetings AVR experts! My first post here...

 

In my current project I need to compare an external signal to a reference and wanted to use the internal analog comparator for that purpose. However, the external signal is changing too slowly and creates the usual issue of multiple triggering (see attached trace, red: input, blue: test output pin used in analog comparator ISR code to diagnose).

 

I'm currently using a discrete comparator and adding positive feedback in the usual fashion to get around this but I was wondering if there was any way to improve the situation with the internal analog comparator. Less components on the board = better in my opinion. :-)

 

Any suggestions would be welcome!

Attachment(s): 

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

Welcome to the Forum.

 

Perhaps others will have a better suggestion, but how attached are you to the M328?

 

The XmegaE5 series also has an analog comparator, but it has a more advanced AC module.

The AC on the E5 series has selectable hysteresis, (None, Small, Large).

Additionally, there is a "window" output interrupt, (Signal above window, within window, and below window).

 

If you haven't used the Xmega's before, note that there are TWO manuals for each chip, one for the family, one for the specific uC.

 

Depending upon the frequency response you need, essentially how fast you need to respond to the change in the input signal once hit passes the threshold, you could also (heavily) filter the input.

That helps the problem, but doesn't really solve the problem, and impacts the response time.

 

JC

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

I usually answer these kinds of questions with something like 'The Datasheet is your friend'. However, the analog comparator hysteresis is not to be found there, as far as I can see.

 

Bummer. And, there is no direct way to connect the analog comparator to an output.

 

But, you can get around this, generating positive feedback this by 'copying' the comparators' state to an output pin of your choice, during the analog comparator interrupt. 

For example, for PORTD5...

 

ISR(ANALOG_COMP_vect){
 unsigned char state = ACSR & _BV(ACO);
 if(state){
  PORTD |= _BV(5);
 else{
  PORTD &= ~ _BV(5);
}

 

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

Thanks for the suggestion JC!

 

I'm not particularly attached to the 328P, however I am far more familiar with it (and other megaAVRs) than the Xmega family. I'll have a look at the E5 series to find out if it's an option too. I certainly don't need anything super fast... just reliable comparator triggered interrupts without multiple transitions.

 

I'm also thinking about 'cheating' by disabling the comparator interrupt after the ISR executes, then re-enable it later on so that the incoming signal has plenty of time to go above / below the reference voltage. I have counters running for other purposes that I could use to re-arm the AC ISR before the next cycle. (incoming signal has a known frequency at 100Hz so there's plenty of time...)

 

- Rob

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

http://legacy.avrfreaks.net/inde...

 

A prior discussion, where a 'Freak measured a few mV and also thought there was internal noise.

 

Also http://legacy.avrfreaks.net/inde...

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

Well, for sure I wouldn't even consider asking without having read the datasheet ;-) and yes if only the AC output was available on a pin... that would solve the issue immediately.

 

Just to clarify: the code performs various other tasks in the comparator ISR which don't involve setting any output pins based on the AC state. The scope trace is using debug code which toggles a pin to clearly show the AC ISR firing several times during the transition. However, I still need those steps in the ISR to execute only once on each transition. (That said I may have misunderstood your suggestion)

 

EDIT: Also wanted to add that everything works perfectly with a good old LM393 doing the job - I'm just trying to find out if there's a way for me to use what's already on board the chip. Since I have a very slowly changing incoming signal, an external schmitt configured external comparator may well be the only viable option. I wanted to ask the experts in case it's doable with the internal AC too... looks like I'm out of luck so far.

Last Edited: Wed. Aug 12, 2015 - 09:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Should be able to introduce some hysteresis in your analog comparator if you have another output pin you can use. Put the reference to the - input through a resistor, then connect a higher value resistor from the - input to this spare output pin so you can pull the reference up or down a little. Then, when you see the + input go high, put the output pin low to lower the reference a little. When you see the + input go low, you can raise the reference a little by setting the output pin high.

 

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Torby wrote:

Should be able to introduce some hysteresis in your analog comparator if you have another output pin you can use. Put the reference to the - input through a resistor, then connect a higher value resistor from the - input to this spare output pin so you can pull the reference up or down a little. Then, when you see the + input go high, put the output pin low to lower the reference a little. When you see the + input go low, you can raise the reference a little by setting the output pin high.

 

 

Nice one. I'm currently using the internal bandgap reference so it won't work as it is now but I can certainly re-configure it to use an external reference that can be pushed up or down with a voltage divider from a spare pin to implement software hysteresis.

 

I'll post back my results.

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

How fast do you need to respond once the 100 Hz signal hits the trigger point?

 

You could take an entirely different approach and read the signal value with the ADC input.

 

Set up a Timer/Counter in CTC mode to fire every X mSec, (or whatever).

Read the last ADC value and trigger the next reading.

 

Look at the last value to see if it crossed the threshold or not.

 

With this approach it would also be easy to add some digital filter, even simplistically, (e.g. 4 samples in a row all exceeded the threshold, so set a flag, or just go process the other stuff).

 

I still like the E5's comparator as a "better" solution.

Just be aware that it is only a 3 V device, in case your current setup is running on 5 V.

 

It, by the way, also has a decent ADC, and a 32 MHz clock rate, giving you a little bit more processing capability than the other Mega's and Tinies.

 

JC

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

if only the AC output was available on a pin...

 Hmmm--some AVR8 models have an ACO pin...but I don't know which ones.  See e.g. Tiny441 family.

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

Ok, I finally got this solved and it works with the internal comparator now. Of course it wouldn't be a proper learning exercise if it didn't involve a little stupidity from my side :-)

 

So I started trying the software based positive feedback mechanism proposed earlier in the thread and configured the analog comparator to use an external reference (made by a simple voltage divider) that was to be modified using one of the spare output pins.

 

However in the process of testing my register settings, I noticed that I would still get the multiple comparator triggers but a much lower extent (only a few). So what was the difference with my previous setup?

 

Well, and this is the shame on me moment, I realized that the day before the scope probe was connected directly to the AIN1 input (which is derived from the input signal by a voltage divider), whereas today it was hooked to the input signal directly. Since the divider's output resistance is quite high (100k + 33k) the scope probe as well as the comparator were loading it too much causing a change in the signal that was enough to start a series of spurious triggers... lesson learned.

 

From there it was easy: changing the voltage divider resistors to smaller values got rid of most of the extra triggers, but there was still a tiny bump in the input signal each time the comparator triggered (which tells me that maybe the input impedance of AIN1 has the possibility to cause issues if the source is high output resistance). I took care of this last bit by simply coupling the pulse to ground via a 100nF film capacitor.

 

Result in attached pictures: completely reliable triggering with the input signal and the internal bandgap reference as a comparison point.

 

Summary: the analog comparator needs a low source impedance signal and cleaning it up via some decoupling works wonders.

 

Thanks to everyone who provided suggestions, hopefully this will also help other people who encounter those issues with the analog comparator.

Attachment(s): 

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

"there is no direct way to connect the analog comparator to an output."

 

On the ATmega328PB, the ACO output is available on pin 3 which is a ground on the ATmega328P. The ACO pin is not available on the ATmega328P.

In the ACSR,

"Bit 5 – ACO Analog Comparator Output
The output of the analog comparator is synchronized and then directly connected to ACO. The
synchronization introduces a delay of 1 - 2 clock cycles."

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

The post you're quoting (indeed the thread) is 4.5 years old. The 328PB did not exist (well, the datasheet had been around for a month, but I don't think devices were).
There are other models (tinies?) which do route ACO to a pin, I'm not near a computer to dig them up.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Mon. Jan 13, 2020 - 01:20 AM