📄 模拟比较器._c
字号:
//外部晶振 8M
#include <ioM16v.h>
#include <macros.h>
#include"delay.h"
/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
#define LED_on PORTA&=~BIT(PA0)
#define LED_off PORTA|=BIT(PA0)
//端口初始化
void port_init(void)
{
PORTA = 0xFF;//端口初始化
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//模拟比较器初始化
void comparator_init(void)
{
ACSR &= ~BIT(ACIE);//确保修改时不产生中断
ACSR = 0x48;//比较输出器变化触发中断
}
//模拟比较中断函数
#pragma interrupt_handler ana_comp_isr:17
void ana_comp_isr(void)
{
DelayUS(10);
if (ACSR&(1<<ACO)) //检测ACO
LED_on; //如果LED亮
else
LED_off; //否则 LED灭
DelayMS(200); //当电压差接近0V时,模拟比较器会产生临界抖动,故延时200mS令肉眼能看到
//通过判断ACO位来知道比较结果
}
void init_devices(void)
{
CLI(); //禁止所有中断
MCUCR = 0x00;//INT0、INT1中断触发方式
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;//外部中断使能
port_init();
comparator_init();
SEI();//开全局中断
}
//主函数
void main(void)
{
init_devices();
//在这继续添加你的代码
while(1)
{
NOP();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -