AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

AVR Analog Comparator - ACO

Hardware/Software Example Utilizing the AVR Analog Comparator via Polling


This AVR tutorial will look at writing simple programs that utilizes the Analog Comparator of an AVR microcontroller. Before you start this AVR tutorial it is recommended that you go through the analog comparator introductory tutorial, paying special attention to the ACSR, The Analog Comparator Control and Status Register, referred to in that tutorial.

To get a practical idea of the applications of the AVR microcontroller's Analog Comparator this AVR tutorial will be done implementing a control program for a critical temperature indicator utilizing the AVR ATMega32 microcontroller.


The Temperature Indicator



System Hardware Description

The figure below gives the simple schematic diagram for an embedded control system utilizing an Atmel ATMega32 microcontroller, which monitors the temperature of a room and indicates, on the LED, whether or not the temperature of the room is above or below a certain preset value. The control system makes use of an Atmel AVR ATMega32 microcontroller’s Analog Comparator which takes two inputs:

  1. A preset temperature represented by a voltage – Set using the variable resistor and applied to PB2.
  2. The current temperature of the room – Outputted from a temperature sensor in the form of a voltage and applied to PB3.

Important Note: Notice that there is no external clock connections in the schematic, so for the hardware to operate the Internal Oscillator of the ATMega32 microcontroller MUST be enabled. Also although the codes below were written for the ATMega32, they can be easily ported to other AVR microcontrollers such as the ATMega8515, ATMega16, ATTiny13, ATTiny2313, etc.

ATMega32 Microcontroller Analog Comparator



The Software Description

Based on the description of the system above the LED should be turn ON when Analog Comparator Output bit (ACO) = 0 and turn OFF when ACO = 1. Below are two(2) codes that accomplish this task. Software I is written in Assembly language and Software II is written in C language.



Software I - An Assembly Control Program for the ATMega32

/*
 *  Written in AVR Studio 5
 *  Author: AVR Tutorials
 *  Website: www.AVR-Tutorials.com
*/
 
.include"m32def.inc"
 
		SBI	DDRD, 0		; Set PD0 as output
 
		LDI	R16, 0x00  	; These two lines configures
		OUT     ACSR, R16	; the Analog Comparator
 
loop:	        SBIS    ACSR, ACO	; Skip next instruction if ACO = 1
		SBI	PORTD, 0  	; Turn LED ON if ACO = 0
		SBIC    ACSR, ACO	; Skip next instruction if ACO = 0
		CBI	PORTD, 0        ; Turn LED OFF if ACO = 1
 
		RJMP    loop	        ; Go back to loop



Software II - The C Control Program for the ATMega32

/*
 *  Written in AVR Studio 5
 *  Compiler: AVR GNU C Compiler (GCC)
 *
 *  Author: AVR Tutorials
 *  Website: www.AVR-Tutorials.com
*/
 
#include<avr/io.h>
 
int main()
{
	unsigned char temp;
 
	DDRD = 0x01;			// Set PD0 as output
 
	ACSR = 0x00; 			// Configured the Analog Comparator
 
	while(1)
	{
		temp = ACSR;		// Get the values from ACSR
		temp &= 0x20;		// Isolate the ACO bit 
 
		if(temp & 0x20)		// Test if ACO is set
			PORTD &= ~0x01;	// Turn LED OFF  - if ACO = 1
		else
			PORTD |= 0x01;	// Turn LED ON - if ACO = 0
	}
 
	return 0;
}

AVR Tutorials hope this tutorial on the AVR Analog Comparator was benificial to you and looks forward to your next visit.