// C++ code
//
int i=0;
void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
DDRD = B11111111; // Port D at Arduino Uno (pin 0-7) como saidas
// Serial.println("hello world");
// Serial.flush();
//Desabilita o Registrador de entrada digital dos pinos AIN1 e AIN0.
DIDR1 = 0b00000000;
//ACD = 0, habilita a energia no comparador analogico
//ACBG = 0, liga Comparador(+) no pino AIN0
//ACIE = 0, desabilita interrupção do comparador
//ACIS1:0 = 00, interrupção por troca - não usado interrupção
ACSR=(0<<ACD)|(1<<ACBG)|(1<<ACIE)|(0<<ACIS1)|(0<<ACIS0);
ADCSRA &= (0 << ADEN); // Disable the ADC module
ADCSRB &= (0 << ACME); // Habilita AIN1 no Comparador(-)
delay(100); // estabilização
Serial.println("ACSR="+String(ACSR));
}
void loop()
{
int comp=0;
i++;
Serial.print("TesteComp[" + String(i) + "]= ");
comp=ACSR&(1<<ACI);
Serial.println(ACSR,BIN);
// Serial.println();
if (comp) digitalWrite(7,comp);
delay(1000); // Wait for 500 millisecond(s)
}
ISR (ANALOG_COMP_vect) // Interrumption vector for the Analog comparator
{
if(ACSR & B00000010) //If we are into falling edge
{
if(!(ACSR & B00100000))//If ACO is 0 (we have that ! for inverse)
{ //A change from HIGH to LOW was detected
//Do what you want to do here..
digitalWrite(7, HIGH); digitalWrite(6, LOW);
digitalWrite(2,LOW);
ACSR &= ~(1<<ACIE);
ACSR |= (1<<ACIS1) | (1<<ACIS0);// Remember top set back the interrupt on rising edge for next ISR
ACSR |= (1<<ACIE);
}
}
if(ACSR & B00000011) // if we are into rising edge
{
if((ACSR & B00100000)) //If ACO is 1
{ //A change from LOW to HIGH was detected
//Do what you want to do here...
digitalWrite(6, HIGH); digitalWrite(7, LOW);
digitalWrite(2,HIGH);
ACSR &= ~(1<<ACIE);
ACSR |= (1<<ACIS1); ACSR &= ~(1<<ACIS0); // Remember set back interrupt on falling edge for next ISR
ACSR |= (1<<ACIE);
}
}
}