Attiny2313 analog comparator

Here is what I would use:

/*
 */
//some chips use ana_comp_vect and others analog_comp_vect
#if defined(ANA_COMP_vect)
ISR(ANA_COMP_vect) {
#else
ISR(ANALOG_COMP_vect) {
#endif
	//clear the flag
	//_comp_isr_ptr();				//run user isr handler
        //insert your code here
        //example
        if (ACSR & (1<<ACO)) {//aco asserted. do something}
        else {//aco cleared. do something else}
}

//reset the comparator
void comp_init(void) {
	//ain0/ain1 assumed to be input

	//configure output pin

	//configure the analog comparator
#if defined(DIDR)
	DIDR = 	(1<<AIN1D) |			//disable ain1's digital input
			(1<<AIN0D)				//disable ain0's digital input
			;
#endif

#if defined(DIDR1)
	DIDR1 = (1<<AIN1D) |			//disable ain1's digital input
			(1<<AIN0D)				//disable ain0's digital input
			;
#endif

	//configure ACSRB if available
#if defined(ACSRB)
	ACSRB &=~(0<<ACME);				//disable acme
#endif

	//configure ACSR
	ACSR &=~(1<<ACIE);				//disable interrupt
	ACSR = 	(0<<ACD) |				//analog comparator power on
#if defined(ACBG)
			(0<<ACBG) |				//bandgap reference not selected. Use AIN1 for non-inverting input
#endif
			(1<<ACI) |				//clear acif by writing '1' to it
			(0<<ACIE) |				//interrupt not yet enabled
			(0<<ACIC) |				//analog comparator input capture disabled
			(0<<ACIS1) | (0<<ACIS0)	//interrupt on output toggle
			;

	ACSR |= (1<<ACIE);				//enable the interrupt

	//rest comp_ptr
}

In your code, you would need to run comp_init() to initialize the analog comparator module. After that, the processing of ACO is done in the isr.

The code is written to run a variety of mcus and with user-installed isr so if you want, you can delete some of the stuff that's not relevant to your module.