📄 interrupt.c
字号:
/************************** (C) 2005 485表项目 *******************************
项 目: 485表项目
编译环境 : IARAVR 编译器 4.12A
模块名称 : Interrupt.c
版 本 : V 1.0
建立时间 : 2008-5-3 21:50
修改时间 : 2008-5-3 23:00
作 者 : 郝瑜云
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
功能描述 : 中断服务程序,包括定时器,串口以及外部中断
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
修改 : 1.2005-10-24:增加掉电禁止计量电量程序防止上下电计电量.
2.2005-10-24:增加软件防潜动功能,但不能抑制7755出脉冲,只能不计量.
3.2005-10-27:增加主振荡自动校正功能,需小批验证.
4.2005-10-27:增加脉冲检测可靠性处理,在延时20ms重检查电平后计量脉冲
******************************************************************************/
#include "Include.h"
/****************************************************************************************************
函 数 : __interrupt void INT0_vect(void)
功 能 : Int0中断处理
注 释 : 执行A相电量累计处理
***************************************************************************************************
*/
#pragma vector=INT0_vect
__interrupt void Int0(void)
{
if((--AdjustVar.CurrtConst_A) ==0x0000) // 检查CF口电平
{
AdjustVar.CurrtConst_A=AdjustVar.AdjustConst_A;
if(++AdjustVar.CurrtSumConst >=_SUMCONST)
{
AdjustVar.CurrtSumConst =0;
SETBIT(PulseBits,PulseCheck);
_PULSE_LED_ON();
SETBIT(PulseBits,EnPulseLED); // 输出脉冲,但可以不计量
TimeVar.PulseTimer = 0;
}
}
}
//*****************************************************************************
// 函数 : void Int1(void) *
// *
// 功能 : Int1中断处理 *
// 注释 : 执行B相电量累计处理 *
// *
//*****************************************************************************
#pragma vector=INT1_vect
__interrupt void Int1(void)
{
if(--AdjustVar.CurrtConst_B==0x0000) // 检查CF口电平
{
AdjustVar.CurrtConst_B=AdjustVar.AdjustConst_B;
if(++AdjustVar.CurrtSumConst >=_SUMCONST)
{
AdjustVar.CurrtSumConst =0;
SETBIT(PulseBits,PulseCheck);
_PULSE_LED_ON();
SETBIT(PulseBits,EnPulseLED); // 输出脉冲,但可以不计量
TimeVar.PulseTimer = 0;
}
}
}
//*****************************************************************************
// 函数 : void Timer0(void) *
// *
// 功能 : Timer0计数中断处理 *
// 注释 : 执行C相电量累计处理 *
//*****************************************************************************
#pragma vector=TIMER0_OVF_vect
__interrupt void BasicTimer(void)
{
TCNT0 = _TCNT0_LOAD;
if(--AdjustVar.CurrtConst_C ==0x0000) // 检查CF口电平
{
AdjustVar.CurrtConst_C=AdjustVar.AdjustConst_C;
if(++AdjustVar.CurrtSumConst >=_SUMCONST)
{
AdjustVar.CurrtSumConst =0;
SETBIT(PulseBits,PulseCheck);
_PULSE_LED_ON();
SETBIT(PulseBits,EnPulseLED); // 输出脉冲,但可以不计量
TimeVar.PulseTimer = 0;
}
}
}
//*****************************************************************************
// 函数 : __interrupt void TIMER1_CAPT(void) *
// *
// 功能 : TIMER1输入捕捉中断处理 *
// 注释 : 红外接收边沿检测 *
//*****************************************************************************
//#ifdef _COM2_HW_EN
#pragma vector=TIMER1_CAPT_vect
__interrupt void TIMER1_CAPT(void)
{
//SETBIT(TIFR0,TOV0); // 红外通讯时丢脉冲原因在此
TCNT1 = _TCNT1_LOAD; // 置定时器初始值 1/800秒
SETBIT(Com2Bits,RD2); // 置位串口2接收请求标志
ComVar.Port2BitCnt = 0; // 清楚串口2接收数据位个数
SETBIT(TIFR1,ICF1); // 禁止TIMER1捕捉中断
CLRBIT(TIMSK1,ICIE1);
}
//#endif
//*****************************************************************************
// 函数 : void Timer1(void) *
// *
// 功能 : Timer1定时溢出中断处理 *
// 注释 : *
//*****************************************************************************
#pragma vector=TIMER1_OVF_vect
__interrupt void Timer1(void)
{
INT8U i,checkp;
TCNT1 = _TCNT1_RELOAD; // 定时 1/1200 秒
if(CHKBIT(PulseBits,EnPulseLED)) // 脉冲输出处理
{
if(++TimeVar.PulseTimer >= 96) // 输出 80ms 脉冲
{
CLRBIT(PulseBits,EnPulseLED);
TimeVar.PulseTimer = 0;
_PULSE_LED_OFF();
}
// else
// {
// _PULSE_LED_ON();
// }
}
//-------------------------------------------------------------------------
if(++TimeVar.Timer10ms >=12) // 10ms 定时
{
TimeVar.Timer10ms = 0;
if(++TimeVar.SecTimer>=50)
{
TimeVar.SecTimer = 0;
SETBIT(TimeBits,OneSecond); //1/2S定时
}
if(CHKBIT(PulseBits,PulseCheck))
{
CLRBIT(PulseBits,PulseCheck);
//-----------------------------------------------------------------
// 防飞走功能
#ifdef _DEF_COUTER_FLY
if(++PowerVar.Pulse10SecCnt >= _MEASURE_FALUT_LIMIT)
{
SETBIT(PulseBits,DisMeasure);
}
#endif
//-----------------------------------------------------------------
if(CHKBIT(PulseBits,DisMeasure)==0) // 检测飞走标志
{
if(CHKBIT(ACSR,ACO) == 0) // 检测是否掉电
{
if( ++PowerVar.PulseCouter >= _PulseConst )
{
PowerVar.PulseCouter = 0;
PowerVar.ActAdd = 0xAA; // 置位电能加标志
// 若一直处于掉电状态,每0.01度即存储电量
SETBIT(ACSR,ACIE); // 允许掉电检测 允许掉电中断
}
PowerVar.PowerChange = 0xAA; // 置位电量改变标志
// SETBIT(PulseBits,EnPulseLED); // 输出脉冲,但可以不计量
}
}
}
// if(CHKBIT(PulseBits,EnPulseLED)) // 脉冲输出处理
// {
// if(++TimeVar.PulseTimer >= 8) // 输出 80ms 脉冲
// {
// CLRBIT(PulseBits,EnPulseLED);
// TimeVar.PulseTimer = 0;
// _PULSE_LED_OFF();
// }
// else
// {
// _PULSE_LED_ON();
// }
// }
}
// #ifdef _COM2_HW_EN
//*************************************************************************
if(CHKBIT(Com2Bits,RD2)||CHKBIT(Com2Bits,TD2))
{
ComVar.Port2BitCnt++; // 串口接收位数加1
//---------------------------------------------------------------------
// 模拟串口发送处理
if(CHKBIT(Com2Bits,TD2))
{
switch(ComVar.Port2BitCnt)
{
case 1:
TCCR2A = 0x42; // 启动TIMER2 发送起始位(0)
break;
case 10:
ComVar.UDR2 = ComVar.CheckP; // 发送偶校验位
if((ComVar.UDR2&0x01)==1)
{
TCCR2A = 0; // 停止TIMER2
_CLR_IO_TXD2(); // 置高TXD2 停止38k 发送1
}
else
{
TCCR2A = 0x42; // 启动TIMER2 发送0
}
break;
case 11:
TCCR2A = 0; // 停止TIMER2
_CLR_IO_TXD2(); // 置高TXD2 停止38k 发送1 停止位
CLRBIT(Com2Bits,TD2); // 清除发送请求标志 1字节发送完毕
break;
default:
if((ComVar.UDR2&0x01)==1)
{
TCCR2A = 0; // 停止TIMER2
_CLR_IO_TXD2(); // 置高TXD2 停止38k 发送1 停止位
}
else
{
TCCR2A = 0x42; // 启动TIMER2 发送0
}
ComVar.UDR2=ComVar.UDR2>>1;
break;
}
}
//-------------------------------------------------------------------------
if(CHKBIT(Com2Bits,RD2)) // 模拟串口接收处理
{
if(ComVar.Port2BitCnt< 9) // 读入8bit数据
{
ComVar.UDR2 = (ComVar.UDR2>>1);
if(CHKBIT(PINB,IO_RXD2)) // 检测RX2端口电平
{
ComVar.UDR2 |=0x80;
}
}
else
{
if(ComVar.Port2BitCnt > 9) // 跳过校验位 接收完毕
{
if(CHKBIT(PIND,IO_RXD2)) // 检测停止位
{
CLRBIT(Com2Bits,RD2); // 清除串口接收请求标志
SETBIT(TIFR1,ICF1);
SETBIT(TIMSK1,ICIE1); // 允许捕捉中断
//---------------------------------------------------------
if(Com2Var.CommEnd == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -