AVR Analog Comparator Example

Publisher:灵感驿站Latest update time:2017-12-17 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

AVR analog comparator example - AVR analog comparator module can be used to compare the voltage between AIN0 (mega16 PB2 second function) and AIN1 (PB3). 
Program operation flow: Initialization >> Open interrupt >> Interrupt service program judgment, the comparison result will be synchronized to 
the fifth bit ACO of the analog comparator control and status register - ACSR, and the comparison result can be obtained by detecting the value of ACO. AIN0AIN1(ACO=1)  

// ICC-AVR  

// Target : M16 
// Crystal: 7.3728MHz 
// AVR Analog Comparator Usage Example 
#include  
#include  
#include "delay.h"  


//Pin Definition 
#define LED0 0 //PB0 
#define AIN_P 2 //PB2(AIN0) 
#define AIN_N 3 //PB3(AIN1) 

//Macro Definition 
#define LED0_ON() PORTB|= (1<on #define LED0_OFF() PORTB&=~(1<
//Constant Definition 
/* 
The positive input of the analog comparator is determined by the ACBG bit, =0 selects the AIN0 pin, =1 selects the 1.23V internal bandgap reference source 

Analog comparator multiplexed input (not commonly used, because the ADC will not be able to be used) 
can select ADC7..0
The ADC multiplexer can be used  to replace the negative input of the analog comparator. 
Of course, in order to use this function, the ADC must be turned off first. 
If the analog comparator multiplexer enable bit (ACME in SFIOR) is set and the ADC is turned off (ADEN in the ADCSRA register is 0), 
the pin that replaces the negative input of the analog comparator can be selected through MUX2..0 in the ADMUX register. 
If ACME is cleared or ADEN is set, the negative input of the analog comparator is AIN1. 
*/ 
#define AC_ADC0 0x00 //ADC0 
#define AC_ADC1 0x01 //ADC1 
#define AC_ADC2 0x02 //ADC2 
#define AC_ADC3 0x03 //ADC3 
#define AC_ADC4 0x04 //ADC4 
#define AC_ADC5 0x05 //ADC5 
#define AC_ADC6 0x06 //ADC6 
#define AC_ADC7 0x07 //ADC7 

void port_init(void) 

PORTA = 0x00; 
DDRA = 0x00; 
PORTB = ~((1<DDRB = (1<PORTC = 0x00; //m103 output only 
DDRC = 0x00; 
PORTD = 0x00; 
DDRD = 0x00; 



//Initialization steps, turn off interrupt, change the value of ACSR, configure analog comparator, turn on interrupt. 
//Comparator initialize 
// trigger on: Output toggle 
void comparator_init(void) 

ACSR = ACSR & 0xF7; //ensure interrupt is off before changing 
//The above sentence will make ACIE zero, and interrupts are not allowed 
ACSR=(1<// Enable analog comparator interrupt, and the comparator output change can trigger an interrupt. AIN0 is the positive input terminal and AIN1 is the negative input terminal. 


#pragma interrupt_handler ana_comp_isr:17 
void ana_comp_isr(void) 

//analog comparator compare event 
//Hardware automatically clears the ACI flag 
delay_us(10); 
if ((ACSR&(1<//Bit 5 ACO: analog comparator output The output of the analog comparator is directly connected to ACO after synchronization. 
LED0_ON(); //If AIN0else 
LED0_OFF(); //Otherwise LED is off 
delay_ms(200); //When the voltage difference is close to 0V, the analog comparator will produce critical jitter, so delay 200ms to make it visible to the naked eye  


//call this routine to initialize all peripherals 
void init_dev IC es(void) 

//stop errant interrupts until set up 
CLI(); //d ISA ble all interrupts 
port_init(); 
comparator_init(); 

MCUCR = 0x00; 
GICR = 0x00; 
TIMSK = 0x00; //timer interrupt sources 
SEI(); //re-enable interrupts 
//all peripherals are now initialized 


void main(void) 

init_devices(); 
while(1) 




-------------------------------------------------------------------------------- 

/* 
Program test: 

       VCC VCC 
        | | 
       | | | |                
 AIN0--|W| |W|--AIN1 PB0---R---LED-- 
       | | | | | 
        | | | 
       GND GND GND 

Two potentiometers, one end connected to VCC, the other end to ground, constitute a potentiometer voltage divider circuit 
. AIN0 and AIN1 are connected to the center tap of the potentiometer respectively. 
PBO output series resistor drives LED, high level is valid. 
Then rotate the potentiometer respectively to increase or decrease the tap voltage, and you will find that the output of PB0 (LED0) will change according to the voltage relationship of AIN0/AIN1 
Due to the influence of power supply ripple, IO current and external interference, when the voltage difference is close to 0V, the analog comparator will produce critical jitter. Connecting a small capacitor to the ground of AIN0/AIN1 can improve this situation. 

When there is only one potentiometer, you can change it. 
1 You can enable ACBG and use the 1.23V internal bandgap reference source instead of AIN0 as the positive input of the analog comparator. 
ACSR=(1<2 You can enable the internal 2.56V voltage reference of the ADC, and then connect AIN0 or AIN1 to PIN 32 AREF pin. 
ADCSRA=(1<ADMUX=(1<
Analog Comparator Control and Status Register - ACSR 

Bit 7 - ACD: Analog Comparator Disable The 
analog comparator is already working by default when it powers up, which is different from other modules 
. When ACD is set, the power supply of the analog comparator is cut off. You can set this bit at any time to turn off the analog comparator. 
This can reduce the power consumption of the device in working mode and idle mode. 
When changing the ACD bit, you must clear the ACIE bit in the ACSR register to disable the analog comparator interrupt. Otherwise, an interrupt may be generated when ACD changes. 

Bit 6 – ACBG: Bandgap Reference Source Selection for Analog Comparator 
When ACBG is set, the positive input of the analog comparator is replaced by the 1.23V bandgap reference source. Otherwise, AIN0 is connected to the positive input of the analog comparator. 

Bit 5 – ACO: Analog Comparator Output 
The output of the analog comparator is directly connected to ACO after synchronization. The synchronization mechanism introduces a delay of  1-2 clock cycles

. Bit 4 – ACI: Analog Comparator Interrupt Flag 
When the output event of the comparator triggers the interrupt mode defined by ACIS1 and ACIS0, ACI is set. 
If the global interrupt flag I in the ACIE and SREG registers is also set, the analog comparator interrupt service routine is executed and ACI is cleared by hardware. 
ACI can also be cleared by writing "1". 

Bit 3 – ACIE: Analog Comparator Interrupt Enable 
When the ACIE bit is set to "1" and the global interrupt flag I in the status register is also set, the analog comparator interrupt is activated. 
Otherwise the interrupt is disabled. 

Bit2 – ACIC: Analog Comparator Input Capture Enable 
This function is used to detect some weak trigger signal sources, saving an external op amp. 
When ACIC is set, it allows the analog comparator to trigger the input capture function of the Timer/C1. 
At this time, the output of the comparator is directly connected to the front-end logic of the input capture, so that the comparator can take advantage of the noise suppressor and trigger edge selection function of the Timer/C1 input capture interrupt logic. 
In order for the comparator to trigger the input capture interrupt of the Timer/C1, TICIE1 of the Timer Interrupt Mask Register TIMSK must be set. 
When ACIC is "0", there is no connection between the analog comparator and the input capture function. 

Bits 1, 0 – ACIS1, ACIS0: Analog Comparator Interrupt Mode Select 
These two bits determine the event that triggers the analog comparator interrupt. 
ACIS1 ACIS0 Interrupt mode 
0 0 Changes in comparator output can trigger an interrupt 
0 1 Reserved 
1 0 The falling edge of the comparator output generates an interrupt 
1 1 The rising edge of the comparator output generates an interrupt 
When changing ACIS1/ACIS0, the interrupt enable bit of the ACSR register must be cleared to disable the analog comparator interrupt. Otherwise, an interrupt may be generated when changing these two bits. 
*/ 


Keywords:AVR Reference address:AVR Analog Comparator Example

Previous article:ATMEGA8 single chip frequency meter program and circuit diagram
Next article:AVR Thermometer DS18B20

Recommended ReadingLatest update time:2024-11-17 03:55

AVR--IO setting programming
AVR's C language is based on ANSI C. It does not extend bit operations (Boolean operations) like 51, although there are SBI/CBI/SBIC/SBIS instructions in the assembly instructions. Therefore, bit logic operations are required to implement them, which must be mastered. The operation methods of IO ports and function reg
[Microcontroller]
AVR--IO setting programming
AVR minimum system circuit diagram
I recently started using avr microcontrollers. I feel that both assembly and C are more comfortable than 51. I don't understand why the current microcontroller teaching must focus on 51. I use ATmega32, which has a relatively large memory, ROM 32K, RAM 2K, EEPROM 1K, which is very suitable for writing some large proje
[Microcontroller]
AVR minimum system circuit diagram
Sine wave made by avr timer
The actual photos are as follows The waveforms of the RC circuit when the resistance is 1K and 10K are as follows    The simulation pictures are as follows:   The procedure is as follows: #include iom16v.h #include macros.h #define uchar unsigned char uchar auc_SinParam = { 128,13
[Microcontroller]
Sine wave made by avr timer
Microchip Releases Secure Application Designs for dsPIC®, PIC18® and AVR® MCUs
Microchip Releases New ISO 26262 Functional Safety Package for dsPIC®, PIC18® and AVR® MCUs to Simplify Design of ASIL B and ASIL C Safety Applications Microchip’s certified functional safety solutions accelerate development and certification of automotive safety applications To
[Automotive Electronics]
Microchip Releases Secure Application Designs for dsPIC®, PIC18® and AVR® MCUs
Homemade AVR high voltage programmer, unlocking avr microcontroller with wrong fuse bit
I'm using Atmel's AT90USB128x/64x and ATmega32U4 to design a keyboard. One of the AT90 microcontrollers was locked due to a wrong fuse setting, so the programmer couldn't flash the program to it. I found several ways to unlock it online: Use the output pin of the active crystal oscillator to the crystal oscillator i
[Microcontroller]
Homemade AVR high voltage programmer, unlocking avr microcontroller with wrong fuse bit
AVR control 4-wire touch screen program
The four-wire resistive touch screen is the most widely used and popular type in the resistive family. Its structure consists of a lower line (glass or film material) conductive ITO layer and an upper line (film The upper and lower lines are open circuit when there is no pressure on the touch screen surface. Once pres
[Microcontroller]
Temperature sensor ds18b20-AVR program code
#include   #include      // Alphanumeric LCD Module functions #asm    .equ __lcd_port=0x18 ;PORTB #endasm #include      #define  uchar unsigned char  #define  uint unsigned int  #define  BIT(x) 1 (x) #define DQ_IN DDRC&=~BIT(2)     #define DQ_OUT DDRC|=BIT(2) #define DQ_SET PORTC|=BIT(2) #define DQ_CLR PORTC&=~BIT(2)
[Microcontroller]
Analysis of the advantages and disadvantages of AVR microcontrollers with unique technology and comprehensive advantages
AVR microcontroller is an enhanced RISC (Reduced Instruction Set CPU) high-speed 8-bit microcontroller with built-in Flash developed by ATMEL in 1997. AVR microcontrollers can be widely used in various fields such as computer peripherals, industrial real-time control, instrumentation, communication equipment, and hous
[Power Management]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号