Go Down

Topic: comparator to compare between AIN0 and AIN1 (Read 6514 times) previous topic - next topic

ThomasB_

Sorry, I don't get it done but it has to be so easy. I want to use A0 and A1 to do a comparision of two voltages. So I use a voltage divider for a threshold on A0 and A1 is used for the voltage to be looked at. My addtional hardware is fine, so I test both pins using the ADC and I can "see" the right adjuested threshold on A0 and also the variable voltage on A1. I learnt from 328p datasheet that the initial configuration of the comparator is to use A0 for the positive input and A1 for the inverting one.

Change the voltage on A1 do not call my ISR. Here my code:
Code: [Select]

int detected = false;

void setup()
{
  ACSR =
    (0<<ACD) | // Analog Comparator: Enabled
  (0<<ACBG) |  // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
  (0<<ACO) |   // only readable
  (1<<ACI) |   // Analog Comparator Interrupt Flag: Clear Pending Interrupt
  (1<<ACIE) |   // Analog Comparator Interrupt: Enabled
  (0<<ACIC) |   // Analog Comparator Input Capture: Disabled
  (0<<ACIS1) | (0<ACIS0);   // Analog Comparator Interrupt Mode: Comparator Interrupt on any edge
  Serial.begin(19200);
}
void loop()
{
  if (detected)
  {
    Serial.println("ISR");
  }
}

ISR(ANALOG_COMP_vect )
{
  detected = true;
}


I also tried similar code snippets but it doesn't work. Any idea what I have overseen?

PaulS

Quote
I want to use A0 and A1 to do a comparision of two voltages. So I use a voltage divider for a threshold on A0 and A1 is used for the voltage to be looked at.

Why not feed the reference voltage in VRef, use analogReference(EXTERNAL), and just apply the voltage to be measured to another pin, and let the Arduino manage stuff?
The art of getting good answers lies in asking good questions.

Magician

Quote
It is possible to select any of the ADC7..0 pins to replace the negative input to the Analog Com-
parator. .....
If ACME is cleared or ADEN is set, AIN1 is applied to the negative input to the Analog
Comparator.

A0 and A1  are not the same as AIN0 and AIN1.
Quote
PC1 (ADC1/PCINT9) pin  and PC0 (ADC0/PCINT8)
PCINT23 /AIN1 - pin D7  and PCINT22/OC0A/AIN0 - pin D6)

There is "working" example:
Code: [Select]
//Configure Analog Comparator, Video Input Capture
  ADCSRB &= ~(1<<ACME);
  ACSR   &= ~(1<<ACD) ;           // Analog Comparator disable off
  ACSR   |=  (1<<ACIC);

  ACSR   |=  (1<<ACIS1);          // comparator detection edge
  ACSR   |=  (1<<ACIS0);          //** 10 - falling, 11- rising:  ACSR &= ~(1<<ACIS0)
  DIDR1  |=  (1<< AIN0D);         // disable digital input buffer AIN0/1
  DIDR1  |=  (1<< AIN1D);

Digital pins 6 and 7 as comparator inputs.
BTW, should you clear a flag after printing?

ThomasB_

..thanks Magician, you gave me the important hint. The AIN0 and AIN1 pins are not the analog input pins. It was only this wrong assumption of me ... :smiley-roll-blue:

Go Up