Go Down

Topic: Analog Comparator Interrupt (Read 7 times) previous topic - next topic

afremont

That's what I meant, I didn't mean I was going to write him an actual assembly language program.  I was referring to the fact that the OP will have to refer to register and bit names and positions to set it up.  Just like in your example.  Libraries generally eliminate (or at least abstract) that to insulate users from having to know the gory details and also to make programs more portable. 
Experience, it's what you get when you were expecting something else.

Erni

Ok, I was just looking in the 328 datasheet, I think the only difference will be the name of the Interrupt vector name:

ANALOG_COMP_vect

sirch

#12
Feb 25, 2013, 05:43 pm Last Edit: Feb 25, 2013, 06:13 pm by sirch Reason: 1
Thanks guys, that's really useful

<<Edit>>
meant to ask, is there some documentation on names like "ACIS0", or is it safe to assume if it's in the datasheet it exists in Arduino land?

Erni

In the datasheet for 1280 it is page 271 and a couple of pages more

page 272 for example:

Quote

• Bits 1, 0 - ACIS1, ACIS0: Analog Comparator Interrupt Mode Select
These bits determine which comparator events that trigger the Analog Comparator interrupt.
The different settings are shown in Table 121.
When changing the ACIS1/ACIS0 bits, the Analog Comparator Interrupt must be disabled
by clearing its Interrupt Enable bit in the ACSR Register. Otherwise an interrupt
can occur when the bits are changed.
Table 121. ACIS1/ACIS0 Settings

afremont

#14
Feb 27, 2013, 09:10 pm Last Edit: Feb 27, 2013, 09:37 pm by afremont Reason: 1
Here is the sample program I promised, sorry it took so long but life got in the way.  It's tested and works.

Code: [Select]

/*
 Analog comparator example using interrupt and testing ACO flag

Compares voltage sensed at pins 6 (AIN0) and 7 (AIN1) which are the analog inputs into the comparator.  AIN0 is
the positve input and AIN1 is the negative input to the comparator. Once the voltage on AIN0(+) rises above the voltage
sensed at AIN1(-), the comparator output becomes high and triggers the interrupt to run.  The interrupt routine toggles
the LED connected to Pin 13.  The ACO flag is tested in the loop() and Pin 9 is set to match.  

The circuit:
* Simple voltage divider made from two 10k resistors with the midpoint connected to AIN1(Pin 7 -)
The end points of the resistors connect to +5V and ground

* Varying input voltage that is normally below the set point of the divider.  I used a CDS photocell above a 10k resistor
configured as a voltage divider.  The photocell connects to +5 and the 10k resistor, the other resistor leg connects to
ground.

* On board LED connected to Pin 13

* Another LED connected to Pin 9 that reflects the status of the comparator output.


created 27 Feb 2013
by Anthony Fremont

*/

void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);

 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

 pinMode(13, OUTPUT);  // toggles when ISR runs

 pinMode(9, OUTPUT);   // indicates status of comparator output
}

void loop() {

 if(ACSR & (1 << ACO))         //  check status of comparator output flag
   digitalWrite(9, HIGH);    //   and mirror it to Pin 9
 else
   digitalWrite(9, LOW);

 delay(100);                    
}

ISR(ANALOG_COMP_vect ) {
 digitalWrite(13, !digitalRead(13));   // toggle state of Pin 13
}

Experience, it's what you get when you were expecting something else.

Go Up