Analog Comparator in AVR ATmega16/ATmega32

Introduction to Analog Comparator

  • ATmega16/ATmega32 based on AVR has two pins for analog voltage compare i.e. AIN0 and AIN1. AIN0 is the positive terminal whereas AIN1 is the negative terminal.
  • When the voltage on positive pin AIN0 is higher than negative pin AIN1, the ACO bit of ACSR register is set.
  • It is possible to use ADC channels (PA0 to PA7) as a negative terminal (AIN1) of a comparator. In this condition, AIN1 will not be considered as a negative input to the comparator. ADC multiplexer is used to select the ADC channel to be connected. ADC must be switched off to use this feature.

It means it is possible to compare nine analog signals with one positive analog signal, sequentially but not simultaneously.

 

Pins of Analog Comparator

Analog Comparator Pins of ATmega16 / 32

 

The register configurations for analog comparator are explained below.

SFIOR: Special Function IO Register

ADTS2ADTS1ADTS0ADHSMACMEPUDPSR2PSR10

 

Bit 3 – ACME: Analog Comparator Multiplexer Enable

When the ACME bit is one and the ADC is switched off, the ADC multiplexer selects the input connected at ADC channels as a negative input to the Analog Comparator and AIN1 is not considered for negative input to the comparator. When the ACME bit is zero, AIN1 acts as the negative terminal of the Analog Comparator.

 

ACSR: Analog Comparator Control and Status Register

ACDACBGACOACIACIEACICACIS1ACIS0

 

Bit 7 – ACD: Analog Comparator Disable

When ACD is one, then the power to the Analog Comparator is switched Off. That means analog comparator is disabled.

Bit 6 – ACBG: Analog Comparator Bandgap Select

When ACBG is one, then the bandgap reference voltage replaces the positive input to the analog comparator.

When ACBG is zero, then AIN0 is applied to the positive of the analog comparator.

Bit 5 – ACO: Analog Comparator Output

This bit indicates the output of the comparator, when AIN0 voltage is higher than the negative pin voltage then the ACO bit is set, otherwise it is clear.

Bit 4 – ACI: Analog Comparator Interrupt Flag

ACI bit is set by hardware. When a comparator output event triggers, the interrupt mode is defined by ACIS1 and ACIS0. The Analog Comparator Interrupt routine is executed if the ACIE bit is set and the I-bit in SREG is set. ACI is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, ACI is cleared by writing a logic one to the flag.

Bit 3 – ACIE: Analog Comparator Interrupt Enable

When the ACIE bit set to one and the I-bit in the Status Register is set, the Analog Comparator Interrupt is activated. When set to zero, the interrupt is disabled.

Bit 2 – ACIC: Analog Comparator Input Capture Enable

When ACIC set to one, this bit enables the Input Capture function in Timer/Counter1 to be triggered by the Analog Comparator. When Set to zero, the connection between Analog  Comparator and Input Capture function does not exist.

Bit 1:0 - ACIS1:ACIS0: Analog Comparator Interrupt Mode Select

ACIS1ACIS0Interrupt Mode
00Comparator Interrupt on Output Toggle
01Reserved
10Comparator Interrupt on Falling Output Edge
11Comparator Interrupt on Rising Output Edge

 

ADMUX: ADC Multiplexer Selection Register

REFS1REFS0ADLRAMUX4MUX3MUX2MUX1MUX0

 

Bit 2:0 – MUX2: MUX0 – To select the ADC input (from ADC0 to ADC7) for the negative input of the comparator.

ACMEADENMUX2.0Analog Comparator Negative Input
0xxxxAIN1
11xxxAIN1
10000ADC0
10001ADC1
10010ADC2
10011ADC3
10100ADC4
10101ADC5
10110ADC6
10111ADC7
Selection of ADC pins

 

ATmega16/32 Analog Comparator Circuit diagram

Analog Comparator Hardware Connections

 

  • In the above circuit diagram, the two input signals are connected to the AIN0 and AIN1 pins of PORTB.
  • The output of the comparator is shown by the LED which is connected to the PC7 pin (Pin No. 29).
  • When the AIN0 pin voltage is greater than the AIN1 pin voltage then the output LED will glow, otherwise LED will off.

 

Programming of ATmega16/32 Analog Comparator

Programming steps for taking negative input from AIN1

  1. Enable AIN1 for negative input, by clearing ACME bit of SFIOR register
  2. Clear ACSR register.
  3. Monitor ACO bit of ACSR register, and take decision accordingly.

ATmega16/32 Analog Comparator Program

/*
 * ATmega16_Analog_Comparator.c
 *
 * http://www.electronicwings.com
 */ 

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

int main()
{
	DDRC |= 0x80;		/* Make pin 7 of PORTC is output */
	SFIOR &= (1<<ACME);	/* Enable AIN1 for -ve input */
	ACSR &= 0x00;		/* Clear ACSR register */
		
	while(1)
	{
		if (ACSR & (1<<ACO))/* Check ACO bit of ACSR register */
		PORTC = 0x80;	/* Turn ON PC7 pin */ 
		else		/* If ACO bit is zero */ 
		PORTC = 0x00;	/* Then	turn OFF PC7 pin */		
	}	
}

 

Programming steps to take negative input from ADC channel

  1. Disable ADC by clearing ADEN bit of ADCSRA register
  2. Select the ADC pin by setting the ADMUX register.
  3. Enable ADC for negative input, by setting ACME bit of SFIOR register
  4. Clear ACSR register.
  5. Monitor ACO bit of ACSR register, and take a decision as per requirement.

 

ATmega16/32 Analog Comparator using ADC Program

/*
 * ATmega16_analog_comparator_using_ADC.c
 *
 * http://www.electronicwings.com
 */ 

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

int main()
{
	DDRC |= 0x80;		/* Make pin 7 of PORTC as output */
	ADCSRA &= (1<<ADEN);	/* Disable ADC */
	ADMUX = 0x00;		/* Select ADC0 as a -ve pin of comparator */
	SFIOR |= (1<<ACME);	/* Enable analog comparator */
	ACSR = 0x00;		/* Clear ACSR resister */
	
	while(1)
	{
		if (ACSR & (1<<ACO))/* Check ACO bit of ACSR register */
		PORTC = 0x80;	/* Turn ON PC7 pin */ 
		else		/* If ACO bit is zero */ 
		PORTC = 0x00;	/* Then	turn OFF PC7 pin */		
	}	

}

 

Video


Components Used

ATmega 16
ATmega 16
1
Atmega32
Atmega32
1
LED 5mm
LED 5mm
1
Breadboard
Breadboard
1

Downloads

ATmega16 Analog Comparator project file Download
ATmega16 analog comparator using ADC project file Download
Atmega16 Analog-Comparator Simulation Download
Ad