📄 ad.c
字号:
/*copyright(c)泰永科技股份有限公司电源事业部
*All rights reserved
*
*文件名称:AD.c
*文件标示:见配置管理计划书
*摘 要:简要描述文件内容
*
*当前版本:
*作 者:
*完成日期:
*
*取代版本:
*原作者 :
*完成日期:
*/
#include <pic18.h>
#include "inc.h"
#include <math.h>
//#include <stdio.h>
//amper A/D
unsigned char bt0 = 0;
unsigned char bt1 = 0;
unsigned char bt2 = 0;
unsigned char bt3 = 0;
unsigned char ab0 = 0;
unsigned char ab1 = 0;
unsigned char ab2 = 0;
unsigned char ab3 = 0;
unsigned char abt0 = 0;
unsigned char abt1 = 0;
unsigned char abt2 = 0;
unsigned char abt3 = 0;
unsigned char sm0 = 0;
unsigned char sm1 = 0;
unsigned char sm2 = 0;
unsigned char sm3 = 0;
unsigned char amper_flag=0;
unsigned char temper_flag=0;
unsigned char air_flag=0;
unsigned char smoke_flag=0;
unsigned int amper_count=0;
unsigned int temper_count=0;
unsigned int air_count=0;
unsigned int smoke_count=0;
unsigned int amper_result = 0;
unsigned int temper_result = 0;
unsigned int smoke_result = 0;
unsigned int air_result = 0;
unsigned char ChannelNumber=0;
unsigned char amper_ad[4] = {0,0,0,0};
unsigned char amper_ad_tmp[4] = {0,0,0,0};
unsigned int ad_reseve[16] = 0;//用于计算的AD 采样各点的值
unsigned int ad_sample[16] = 0;//AD 采样数组
unsigned char AD_Point = 0;//sample point
unsigned char AD_Flag = 0;//AD转换完成标志,=1有新的AD数据转换完成
unsigned int alam_cnt = default_alam_cnt;
unsigned char alam_amper = 0;
unsigned char alam_temper = 0;
unsigned char alam_smoke = 0;
unsigned char alam_air = 0;
unsigned char alamamper = 0;
unsigned char alamtemper = 0;
unsigned char alamsmoke = 0;
unsigned char alamair = 0;
unsigned char sw = 0;//0 amper ad,1 temper ad
/*
*函数介绍:定义一个共用体,用于存放A/D 转换的结果
*输入参数:
*输出参数:
*返回值:
*/
union adres adresult;
/*
*函数介绍:读AD
*输入参数:
*输出参数:
*返回值:
*/
unsigned int AdcRead(unsigned char channel, unsigned int times)
{
unsigned int AD_Value;
unsigned long AD_V;
unsigned int i;
ADCON0 = (channel << 2) | 0x01; // enable ADC
asm("nop");
asm("nop");
asm("nop");
delay(1);//捕捉时间约200多us
AD_Value = 0;
AD_V = 0;
for (i=0; i<times; i++)
{
ADCON0|= 0x02;//start ADC
while(ADCON0&0x02); // wait for conversion complete
adresult.adre[0]=ADRESL; //读取并存储A/D 转换结果,A/D 转换的结果通过共
adresult.adre[1]=ADRESH;
AD_V = AD_V + adresult.t;
}
AD_Value = AD_V/times;
return (unsigned int)AD_Value;
}
/*
*函数介绍:处理漏电电流值
*输入参数:
*输出参数:
*返回值:
*/
unsigned int amper_get()
{
unsigned int amper_temp;
unsigned long amper_proc;
unsigned int a0,a1,a2,a3,a4;
amper_temp = 0;
amper_proc = 0;
a0=compare_int(&er0_rd[0],3);
a1=compare_int(&er1_rd[0],3);
a2=compare_int(&er2_rd[0],3);
a3=compare_int(&er3_rd[0],3);
a4=compare_int(&er4_rd[0],3);
amper_temp = AdcRead(2, points);
if (amper_temp<=a0)//%5
{
amper_proc = amper_temp*50/a0;
}
else if ((amper_temp>a0)&&(amper_temp<=a1))//%10
{
amper_proc = amper_temp*100/a1;
}
else if ((amper_temp>a1)&&(amper_temp<=a2))//%30
{
amper_proc = amper_temp*300/a2;
}
else if ((amper_temp>a2)&&(amper_temp<=a3))//%60
{
amper_proc = amper_temp*600/a3;
}
else if (amper_temp>a3)//%90
{
amper_proc = amper_temp*900/a4;
}
amper_result = amper_proc; // 转换为整形数据
bt0=(amper_result/1000)&0x0f;
bt1=((amper_result%1000)/100)&0x0f;
bt2=((amper_result%100)/10)&0x0f;
bt3=(amper_result%10)&0x0f;
return amper_result;
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
unsigned int temper_get()
{
unsigned char i;
unsigned int temper_temp;
unsigned long temper_proc;
unsigned int t0,t1,t2,t3,t4;
unsigned int temper_max=0;
unsigned int max_tmp=0;
temper_temp = 0;
temper_proc=0;
for(i==9;i!=0;i--)
{
max_tmp = AdcRead(0, points);
if(temper_max<max_tmp)
temper_max=max_tmp;
sel_chanel=(sel_chanel+1)&0x07;
}
sel_chanel=sel_chanel&0xf8;
sw=2;
temper_temp=temper_max;
t0=compare_int(&temper0_rd[0],2);
t1=compare_int(&temper1_rd[0],2);
t2=compare_int(&temper2_rd[0],2);
t3=compare_int(&temper3_rd[0],2);
t4=compare_int(&temper4_rd[0],3);
if (temper_temp<=t0)//%20
{
temper_proc = temper_temp*20/t0;
}
else if ((temper_temp>t0)&&(temper_temp<=t1))//%40
{
temper_proc = temper_temp*40/t1;
}
else if ((temper_temp>t1)&&(temper_temp<=t2))//%60
{
temper_proc = temper_temp*60/t2;
}
else if ((temper_temp>t2)&&(temper_temp<=t3))//%80
{
temper_proc = temper_temp*80/t3;
}
else if ((temper_temp>t3)&&(temper_temp<=t4))//%100
{
temper_proc = temper_temp*100/t4;
}
temper_result=temper_proc; // 转换为整形数据
ab0=(temper_result/1000)&0x0f;
ab1=((temper_result%1000)/100)&0x0f;
ab2=((temper_result%100)/10)&0x0f;
ab3=(temper_result%10)&0x0f;
return temper_result;
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
unsigned int air_get()
{
unsigned char i;
unsigned int air_temp;
unsigned long air_proc;
unsigned int i0,i1;
unsigned int air_max=0;
unsigned int max_tmp=0;
air_temp = 0;
air_proc = 0;
for(i==5;i!=0;i--)
{
max_tmp = AdcRead(1, points);
if(air_max<max_tmp)
air_max=max_tmp;
sel_chanel=(sel_chanel+1)&0x07;
}
sel_chanel=sel_chanel&0xf8;
sw=3;
air_temp=air_max;
i0=compare_int(&air0_rd[0],3);
i1=compare_int(&air1_rd[0],4);
air_temp = AdcRead(2, points);
if (air_temp<=i0)
{
air_proc = air_temp*500/i0;
}
else
{
air_proc = air_temp*1000/i1;
}
air_result=air_proc; // 转换为整形数据
abt0=(air_result/1000)&0x0f;
abt1=((air_result%1000)/100)&0x0f;
abt2=((air_result%100)/10)&0x0f;
abt3=(air_result%10)&0x0f;
return air_result;
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
unsigned int smoke_get()
{
unsigned char i;
unsigned int smoke_temp;
unsigned long smoke_proc;
unsigned int s0,s1;
unsigned int smoke_max=0;
unsigned int max_tmp=0;
smoke_temp = 0;
smoke_proc = 0;
for(i==5;i!=0;i--)
{
max_tmp = AdcRead(3, points);
if(smoke_max<max_tmp)
smoke_max=max_tmp;
sel_chanel=(sel_chanel+1)&0x07;
}
sel_chanel=sel_chanel&0xf8;
sw=0;
smoke_temp=smoke_max;
s0=compare_float(&smoke0_rd[0],4);
s1=compare_float(&smoke1_rd[0],4);
if (smoke_temp<=s0)
{
smoke_proc = smoke_temp*120/s0;
}
else
{
smoke_proc = smoke_temp*1000/s1;
}
smoke_result=smoke_proc;
if(smoke_result<10)
{
sm3 = smoke_result;
sm2 = 0;
sm1 = 0;
sm0 = 0;
}
else if((10<=smoke_result)&&(smoke_result<100))
{
sm3 = (smoke_result%10)&0x0f;
sm2 = (smoke_result/10)&0x0f;
sm1 = 0;
sm0 = 0;
}
else if((100<=smoke_result)&&(smoke_result<1000))
{
sm3 = ((smoke_result%100)%10)&0x0f;
sm2 = ((smoke_result%100)/10)&0x0f;
sm1 = (smoke_result/100)&0x0f;
sm0 = 0;
}
else if(smoke_result>=1000)
{
sm3 = (((smoke_result%1000)%100)%10)&0x0f;
sm2 = (((smoke_result%1000)%100)/10)&0x0f;
sm1 = ((smoke_result%1000)/100)&0x0f;
sm0 = (smoke_result/1000)&0x0f;
}
return smoke_result;
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
void ad_command()
{
if (sw == 0)
{
amper_get();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
sw = 1;
}
else if(sw == 1)
{
temper_get();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
else if(sw == 2)
{
air_get();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
else if(sw == 3)
{
smoke_get();
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
}
/*
*函数介绍:将ASII转换为BCD
*输入参数:
*输出参数:
*返回值:
*/
unsigned int compare_int(unsigned char *pnt,unsigned char n)
{
unsigned char i,j;
unsigned char pnt_tmp;
unsigned int para_int=0;
unsigned long para_tmp=0;
unsigned long m;
for(i=0;i<n;i++)
{
m=1;
for(j=n-i-1;j>0;j--)
{
m=m*10;
}
pnt+=i;
pnt_tmp=*pnt;
para_tmp=(unsigned long)pnt_tmp;
para_tmp=para_tmp*m;
para_int=para_tmp;
}
return para_int;
}
/*
*函数介绍:将小数扩大100倍
*输入参数:
*输出参数:
*返回值:
*/
unsigned int compare_float(unsigned char *pint,unsigned char n)
{
unsigned char p0=0;
unsigned char p1=0;
unsigned char p2=0;
unsigned char p3=0;
unsigned int para_float=0;
unsigned long a0=0;
unsigned long a1=0;
unsigned long a2=0;
unsigned long a3=0;
if(n==4)
{
p0=*pint;
a0=(unsigned long)p0;
pint+=1;
p1=*pint;
a1=(unsigned long)p1;
pint+=1;
p2=*pint;
a2=(unsigned long)p2;
pint+=1;
p3=*pint;
a3=(unsigned long)p3;
if(a1=='.')//判断a1为小数点
{
para_float=a0*100+a2*10+a3;
}
if(a2=='.')//判断a2为小数点
{
para_float=a0*1000+a1*100+a3*10;
}
}
return para_float;
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
void amper_check()
{
unsigned int count;
unsigned int amper_set;
count=compare_int(&rdly_rd[0],2);
count=count*100;
amper_set=compare_int(&er_rd[0],4);
if (amper_set <= amper_get())
{
amper_flag=0;
amper_count=count;//60s之内判断
if(amper_flag==1)
{
if(amper_set<=amper_get())
{
alam_amper=1;
alamamper=1;
}
}
}
else
{
alam_amper = 0;
alamamper = 0;
}
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
void temper_check()
{
unsigned int count;
unsigned int temper_set;
count=compare_int(&tmprdly_rd[0],2);
count=count*100;
temper_set=compare_int(&temper_rd[0],3);
if(temper_set <= temper_get())
{
temper_flag=0;
temper_count=count;//40s之内判断
if(temper_flag==1)
{
if(temper_set<=temper_get())
{
alam_temper=1;
alamtemper=1;
}
}
}
else
{
alam_temper = 0;
alamtemper = 0;
}
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
void air_check()
{
unsigned int count;
unsigned int air_set;
count=compare_int(&airdly_rd[0],4);
count=count*100;
air_set=compare_int(&air_rd[0],4);
if(air_set <= air_get())
{
air_flag=0;
air_count=count;//1s~9999s之内再判断
if(air_flag==1)
{
if(air_set<=air_get())
{
alam_air=1;
alamair=1;
}
}
}
else
{
alam_air = 0;
alamair = 0;
}
}
/*
*函数介绍:
*输入参数:
*输出参数:
*返回值:
*/
void smoke_check()
{
unsigned int count;
unsigned int smoke_set;
count=compare_int(&smkdly_rd[0],2);
count=count*100;
smoke_set=compare_float(&smoke_rd[0],4);
if(smoke_set <= smoke_get())
{
smoke_flag=0;
smoke_count=count;//60s之内判断
if(smoke_flag==1)
{
if(smoke_set<=smoke_get())
{
alam_smoke=1;
alamsmoke=1;
}
}
}
else
{
alam_smoke = 0;
alamsmoke = 0;
}
}
/*
*函数介绍:声报警
*输入参数:
*输出参数:
*返回值:
*/
void longspk()
{
if (alam_flag==0)
{
if(alam_amper||alam_temper||alam_air||alam_smoke)
{
spk = 1;
alam_cnt = default_alam_cnt;
}
/* else
{
spk = 0;
}*/
}
else if (alam_flag==1)
{
if ((alam_amper==0)&&(alam_temper==0)&&(alam_air==0)&&(alam_smoke))
{
spk = 0;
alam_flag =0;
}
else
{
spk = 0;
alam_flag = 1;
}
}
}
/*
*函数介绍:光报警
*输入参数:
*输出参数:
*返回值:
*/
void alam_dis()
{
if(alamamper||alamtemper||alamair||alamsmoke)
{
DS0 = 0;
if(f4hz_flag&0x01)
{
DS1 = 0;
}
else
{
DS1 = 1;
}
if(alamamper==0)
{
TSA=1;//气体,温度,烟雾超标处理
}
}
else
{
DS1 = 0;
DS0 = 1;
TSA = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -