Go Down

Topic: Analog comparator on Arduino Leonardo (Read 1 time) previous topic - next topic

bogdanul2003

Jul 09, 2014, 09:11 pm Last Edit: Jul 09, 2014, 10:18 pm by bogdanul2003 Reason: 1
Hi,

I'm struggling to get the analog comparator working on arduino leonardo. This is what I do: I connect AIN- to internal bandgap (1.1 V) and set an interrupt on rising edge so when AIN0 voltage is higher than 1.1 V it should generate an interrupt.
Here is the code :

Code: [Select]
static volatile char flag;
static volatile int flag2=0;

ISR(ANALOG_COMP_vect) {

   //with this test, ensure that indeed the interrupt result is 1 = V(AIN0) > V(AIN1)
   //(double check ?)
   if ( (ACSR & (1 << ACO))== 0 ) {
       flag=true;
       flag2++;
       }
     else
     {
     flag=false;
     }
     
     Serial.print("OUCH\n");
}

void setup(void) {
 Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }
 Serial.print("start123");
 noInterrupts();
 pinMode(A1, OUTPUT);
 pinMode(A3, OUTPUT);
 //pinMode(7,  INPUT);
 PORTE = 0;
 digitalWrite(A1, LOW);
 digitalWrite(A3, HIGH);
 sbi(ADCSRA,ADEN);
 cbi(ADCSRB,ACME);
 sbi(DIDR1, AIN0D);

 ACSR =
 (0<<ACD) |   // Analog Comparator: Enabled
 (0<<ACBG) |   // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
 (0<<ACO) |   // Analog Comparator Output: Off
 (1<<ACI) |   // Analog Comparator Interrupt Flag: Clear Pending Interrupt
 (1<<ACIE) |   // Analog Comparator Interrupt: Enabled
 (0<<ACIC) |   // Analog Comparator Input Capture: Disabled
 (1<<ACIS1) | (1<<ACIS0);   // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge
 
 interrupts();
}

void loop(void) {
if(flag)
{
 Serial.print("OUCH2\n");
 Serial.print(flag2);
 flag=false;
}

 delay(50);
}



When I connect the 5V pin from the arduino board to the AIN0 (digital 7)  no interrupt is generated. When I connect the GND pin to AIN0 the interrupt gets triggered.
Shouldn't it be the other way around? What am I doing wrong?
Thanks


Moderator edit: [code] [/code] tags added.

bogdanul2003

The code I posted is taken from this thread:
http://forum.arduino.cc/index.php/topic,149840.0.html

Just some minor modifications to select de internal 1.1 V for AIN-

Any clues ?

Go Up