Go Down

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



Still trying to understand how I write Arduino code (as opposed to AVR machine code) to do this...


Typically arduino code doesn't have functions for low level stuff like this. You'll have to use good old avr register writes.


As an inspiration, here how you coul set up the comparator on a Attiny2313:



I don't of any library that you can use to make it simpler, so you'll pretty much have to code things the same way that you'd do it in assembler.  I will make a test program today for my Uno that uses the analog comparator. 
Experience, it's what you get when you were expecting something else.


There is no reason to use  assembler.
In my test code below, I set the relevant bits according to the datasheet

Although this is for Attiny2313, I think you could adopt it for the 1280

It is more or less the code I posted earlier http://kalshagar.wikispaces.com/attiny2313

Code: [Select]

#include <avr/interrupt.h>
#include <TinyDebugKnockBang.h>
  int a=0;
  static volatile uint8_t capture;
  static volatile uint8_t flag;
  const uint8_t led = 13;

//Interrupt code of the comparator
// NB : you need to stabilize the power rail with a capacitor (otherwise you'll have ripples and misreading). With AIN1=Comp-=0v a 200uF is fine.
ISR(ANA_COMP_vect) {

    //with this test, ensure that indeed the interrupt result is 1 = V(AIN0) > V(AIN1)
    //(double check ?)
    if ( (ACSR & (1 << ACO))== 0 ) {

void setup() {
    //factory settings is to divide internal clock 8MHz by 8.
    //don't, and just run at 8 MHz (set the clock divider to 1 so no effect)
    CLKPR = (1<<CLKPCE);
    CLKPR = 0; // Divide by 1

    PORTB &= ~(1<<PB0);    // no Pull-up on PB0

    ACSR  |=  (1<<ACI);    // clear Analog Comparator interrupt
    ACSR  |=
        (0<<ACD)   |         // Comparator ON
        (0<<ACBG)  |         // Disconnect 1.23V reference from AIN0 (use AIN0 and AIN1 pins)
        (1<<ACIE)  |         // Comparator Interrupt enabled
        (0<<ACIC)  |         // input capture disabled
        (0<<ACIS1) |         // set interrupt bit on rising edge
        (0<<ACIS0);          //    (ACIS1 and ACIS0 == 11)
   // Enable the interrupt
  ACSR = ACSR | (1 << ACIE);

    sei();                 // enable global interrupts


   void loop() {/*all the job is done in the interrupt */
    //    Debug.println(flag);
       if (flag==true){
      if (flag==false){


Go Up