Analog Comparator

Dia duit mo chairde,

I am trying to set up the analog comparator, but I would just like to ask a few basic questions about it first.

I will be using a reference voltage of about 0.6v, but do I need to specify which pin I am using as the reference?

Also, the way this works is ...when the system is below the reference voltage, it does not jumo to an ISR, but when it goes above the reference voltage, it does go to the ISR?

It can't be that simple ...can it?

Thanks,

Seán

I will be using a reference voltage of about 0.6v, but do I need to specify which pin I am using as the reference?

The positive terminal of the comparator is dedicated to A0 (but can be switched to the internal bandgap voltage also if needed). The minus terminal of the comparator can be set to any of the other analog inputs.

Also, the way this works is ...when the system is below the reference voltage, it does not jumo to an ISR, but when it goes above the reference voltage, it does go to the ISR?

You can program the ISR for one of three conditions...falling edge, rising edge or toggle. I posted a sample sketch using the comparator here http://arduino.cc/forum/index.php/topic,70322.15.html that might be a good starting point for you.

Hey,

Thanks for the reply! I will read over the post you suggest.

I am trying a few different methods for saving power, I'm just not sure what way to go.

But thanks again!!

Seán

To correct a previous post: The +ve input of the comparator can be connected either to the internal 1.1V bandgap reference or to pin AIN0 (which is the same Arduino pin 6, not A0).

The -ve input can be connected either to AIN1 (Arduino pin 7) or to the ADC multiplexor (and thus any of the analog inputs).

See section 22 of the datasheet for more details.

Ooops...you are correct! I've never actually used the AIN0/1 pins I've always used the bandgap and ADC pins for the comparator. Good to know.

MarkT:
To correct a previous post: The +ve input of the comparator can be connected either to the internal 1.1V bandgap reference or to pin AIN0 (which is the same Arduino pin 6, not A0).

The -ve input can be connected either to AIN1 (Arduino pin 7) or to the ADC multiplexor (and thus any of the analog inputs).

See section 22 of the datasheet for more details.

Thanks for the reply!

Pins 6 and 7 are the digital pins, yes?
And I take it you connect to the internal 1.1V bandgap reference through the code?

I have been reading the datasheet, but it can be a bit confusing ...to me anyway.

Thanks.

Seán

Yes, digital 6 and 7 (If you look at the top of the datasheet at the pin diagrams you'll see that PD6 is also AIN0 and PD7 is AIN1 amongst other things.)

The problem is that the datasheet has to explain everything, so its often hard work to extract only the information useful to you... Maybe google will find you some code that already drives the comparator?

Cool!

Well, even just knowing where to connect into is a help! I can always try and figure the code out, and get help where I can.

Thanks again.

Seán

Hi there,

Just have some more to add-on to this one.

I am wondering if this code makes sense -- I'm largely making it up as I go along. I want to put the system into sleep mode, after a specific amount of time, and wake up and when it detects an analog input, through the FSR.

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

int fsr = 0;
int count = 0;
int analogPin = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(analogPin, INPUT);
  
  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
}

void sleepNow()
{
  set_sleep_mode(SLEEP_MODE_IDLE);
  
  sleep_enable();    //enables the sleep mode bits in the register
  
  power_spi_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_twi_disable();
  
  sleep_mode();
  
  sleep_disable();
  
  power_all_enable();
}

void loop()
{
  count++;
  delay(1000);
  if(count++ == 100)
  {
    Serial.print("Sleeping");
    sleepNow();
  }
}

ISR(ANALOG_COMP_vect)
{
  //Code to read from fsr
}

Thanks in advance.

Seán