📄 an_compare.c
字号:
/*
**
****************************************************************************
**
**
** Copyright (c) 2007 - Atmel Corporation
** Proprietaty Information
**
** Project : ATMEGA88 + ATA6824 High Temperature H-bridge System
** Module : an_compare.c
** Description : Analog comparator is used to detect over-currents
**
**
** Version : Date: Author: Comment:
** 1.0 05.03.2007 F.G. Creation
**
** LICENSE -
**
** ATMEL - 2007
** All software programs are provided 'as is' without warranty of any kind:
** Atmel does not state the suitability of the provided materials for any
** purpose. Atmel hereby disclaim all warranties and conditions with regard
** to the provided software, including all implied warranties, fitness for
** a particular purpose, title and non-infringement.In no event will Atmel
** be liable for any indirect or consequential damages or any damages
** whatsoever resulting from the usage of the software program.
****************************************************************************
**
*/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"
#include "an_compare.h"
#include "Timer0_PWM.h"
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N S ______________________________________________*/
/*_____ P R O T O T Y P E S - D E C L A R A T I O N ________________________*/
/*_____ G L O B A L S ______________________________________________________*/
over_current_t over_current; //! variable used to monitor over-current conditions
/*! @brief an_compare_init() initate the analog comparator to detect Over-currents
* Initialize Analog comparator:
* - Disable AIN0 and AIN1 (resp. PD6 and PD7) digital input buffers
* - Enable analog comparator to generates output toggle interrupts
*/
void AN_compare_init(void)
{
DIDR1 = (1<<AIN1D) | (1<<AIN0D); // Disable digital buffers on AIN1 and AIN0
// to reduce power consumption
ACSR = (1<<ACIE); // Enable Analog comparator interrupts
// on compare output toggle
}
/*! @brief Over_current_ISR() comes as an over-current protection.
* Each transition around Over-current value (fixed by external reference)
* generate an interrupt.
* - When current increases above Over-current value, interrupt subroutine
* disables output PWM (pin is set to zero). It's reported in over_current
* variable.
* - When current decreases down to Over-current value, interrupt subroutine
* re-enables the output PWM. It's reported in over_current variable.
*/
#if defined(__ICCAVR__) //IAR COMPILER USED
#pragma vector= ANA_COMP_vect //Analog comparator vector for ATmega88
__interrupt void Over_current_ISR (void)
#elif defined (__AVR__) // Avr-gcc compiler used
ISR(ANALOG_COMP_vect)
#endif
{
if (ACSR & (1<<ACO))
{ // Input max current reference > current
RE_ENABLE_OCB0(); // Restore PWM output control
over_current.status = false;
}
else // Over-current condition
{ // Current > max current reference
DISABLE_OCB0(); // Release OCB0, PWM output
over_current.occured = true;
over_current.status = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -