Go Down

Topic: Execution Time of Control Functions (Read 813 times) previous topic - next topic

Nick Gammon

Indeed. I did a short post about using the comparator:

http://www.gammon.com.au/forum/?id=11916
http://www.gammon.com.au/electronics

Coding Badly


If I remember correctly, with the analog comparator, you do not need to actually generate an interrupt.  You can check for and reset the flag.  The advantages: all the issues with interrupts are eliminated; uses a bit less SRAM; possibly generates less code.

I think this modified version of Nick's example shows how...

Code: [Select]
ISR (ANALOG_COMP_vect)
  {
  }

void setup ()
  {
  Serial.begin (115200);
  Serial.println ("Started.");
  ADCSRB = 0;           // (Disable) ACME: Analog Comparator Multiplexer Enable
  ACSR =  _BV (ACI)     // (Clear) Analog Comparator Interrupt Flag
        | _BV (ACIS1);  // ACIS1, ACIS0: Analog Comparator Interrupt Mode Select (trigger on falling edge)
   }  // end of setup

void loop ()
  {
  if ( (ACSR & _BV (ACI)) != 0 )
    {
    Serial.println ("Triggered!");
    ACSR |=  _BV (ACI);     // (Clear) Analog Comparator Interrupt Flag
    }
 
  }  // end of loop

rickso234

Of course I'm currently using D6 as a PWM output so can't use the analog comparator?

May have to add my own comparator to the circuit and connect its output to a pin change interrupt.

Coding Badly

Of course I'm currently using D6 as a PWM output so can't use the analog comparator?


Correct.

Quote
May have to add my own comparator to the circuit and connect its output to a pin change interrupt.


Or, you could add an LED driver that supports PWM.

Nick Gammon

I did a comparator here:

http://www.gammon.com.au/forum/?id=11011

Not that they are complex. :)
http://www.gammon.com.au/electronics

Go Up