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 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:
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.
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: 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.