Analog Input Comparator Sleep/Wakeup

Greetings,

I am working on a design that requires my ATMEGA 328p to sleep and wake-up from a 3-axis analog accelerometer. I am reading over the datasheet for the AVR and believe the Analog Input Comparator would be suitable for this.

On page 246 of the datasheet it says:

It is possible to select any of the ADC7..0 pins to replace the negative input to the Analog Comparator.
The ADC multiplexer is used to select this input, and consequently, the ADC must be
switched off to utilize this feature. If the Analog Comparator Multiplexer Enable bit (ACME in
ADCSRB) is set and the ADC is switched off (ADEN in ADCSRA is zero), MUX2..0 in ADMUX
select the input pin to replace the negative input to the Analog Comparator, as shown in Table
22-1. If ACME is cleared or ADEN is set, AIN1 is applied to the negative input to the Analog
Comparator

In my case I would like to use ADC0 for testing the X-axis input. According to Table 22-1 ADC0 requires ACME = 1, ADEN = 0.

What other setup is necessary to configure the Analog Comparator?

I want to setup the Analog Comparator, then the AVR sleep and power libraries to put the controller asleep. If possible the Analog Comparator can keep running and bring the controller back to life when the accelerometer threshold is met. For now I am simply using a potentiometer instead.

I will provide code samples as I am working on the project.

If there is any helpful examples of sleeping/waking up the 328p or other AVR chips it would be much appreciated.

See Gammon Forum : Electronics : Microprocessors : Using the Arduino Analog Comparator
Modify for ADC0 and add the appropriate sleep code.

Thanks for the tip.

I wrote some code based heavily on examples Gammon provided.

I imagine my setup for ADCSRA bits are incorrect.

#include <avr/interrupt.h>
#include <avr/sleep.h>


volatile boolean triggered;
ISR (ANALOG_COMP_vect)
  {
  triggered = true;
  }

void setup ()
  {
  Serial.begin (115200);
  Serial.println ("Started.");
  
 
  ADCSRA = 
      (0<<ADEN)  |  // ADC Disable
      (0<<ADSC)  |  // ADC Start Conversion
      (0<<ADATE) |  // ADC Auto Trigger Disable
      (0<<ADIF)  |  // ADC Interrupt Flag
      (0<<ADIE)  |  // ADC Interrupt Enable
      (0<<ADPS2) |  // ADC Prescaler Select Bit 2
      (0<<ADPS1) |  // ADC Prescale Select Bit 1
      (0<<ADPS0);   // ADC Prescale Select Bit 0
   ADCSRB = 
      (1<<ACME); // Analog Comparator Multiplex Enable
   ACSR = 
      (0<<ACD)   |   //Comparator, Enabled
      (0<<ACBG)  |  //AIN0 is applied to the positive input
      (0<<ACO)   |   // Analog Comparator Output: Off
      (1<<ACI)   |   //Clear Pending Interrupt
      (1<<ACIE)  |  //Analog Comparator Interrupt, Enabled
      (0<<ACIC)  |  //Analog Comparator Input Capture, Disabled
      (1<<ACIS1) | (1<ACIS0); //Capture on Rising Edge
      
   }  // end of setup

void loop ()
  {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); 
  sleep_enable();
  sleep_mode();
  if (triggered)
    {
    sleep_disable(); 
    Serial.println ("Triggered!"); 
    triggered = false;
    }
  
  }  // end of loop

Unfortunately, I don't have a board to test until tomorrow.

Gammon's example was extremely helpful!