#include "PM2.5.H"
unsigned char ADC_date[10]; //10次AD采样后存储的数组
unsigned char flag_PM=0,count=0;
unsigned int date,U_date,PM_value;
/************粉尘传感器初始化*****************/
void PM_init()
{
TMOD = 0x11; //设定定时器T0工作方式
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
PT0=0; //设置中断0为低优先级
EA=1;
TR0=1;
ET0=1;
PM_LED=0;
}
void PM_
time0()interrupt 1 using 0
{
unsigned char j;
TH0=(65535-1000)/256; //定时周期1MS
TL0=(65535-1000)%256;
PM_LED=1; //启动粉尘传感器
for(j=0;j<28;j++)Delay_10us(); //延时0.28ms
Red_0809 (); //模拟量转化为数字量
flag_PM=1;
for(j=0;j<5;j++);
TR0 = 0; //先关闭定时器0
EA = 0;
PM_LED=0; //关闭粉尘传感器
}
/*******粉尘传感器经AD转换后的数字量的值*****/
unsigned int PM_date()
{
if(flag_PM==1)
{
ADC_date[count]=getdate; //把AD转化后的值存在 ADC_date数组中
count++;
if(count==10)
{
count=0; //次数清零,重新计数
flag_PM=0;
date=ADC_date[0]+ADC_date[1]+ADC_date[2]+ADC_date[3]+ADC_date[4]+
ADC_date[5]+ADC_date[6]+ADC_date[7]+ADC_date[8]+ADC_date[9]; //取10次AD采样的平均值
U_date=date*1.0/256*5000; //转化为电压MV
PM_value=U_date*0.17-0.1; //固体悬浮颗粒浓度计算 Y=0.17*X-0.1 X--采样电压V ,单位 毫克每立方
}
}
if(PM_value<0)PM_value=0;
if(PM_value>760)PM_value=760;//限位
return(PM_value);
}
主程序部分
#include "lcd.h"
#include "Delay.h"
#include "ADC0809.h"
#include "PM2.5.H"
void PM_display(unsigned int date); //PM2.5浓度显示
void main()
{
lcd_init();
ADC0809_init();
PM_init();
while(1)
{
PM_display(PM_date());
TR0=1;
EA=1;
}
}
/*****PM2.5浓度显示******/
void PM_display(unsigned int date)
{
LcdWriteCom(0xc0);
LcdWriteData('P');
LcdWriteData('M');
LcdWriteData(':');
LcdWriteData('0'+date%10000/1000);
LcdWriteData('0'+date%1000/100);
LcdWriteData('0'+date%100/10);
LcdWriteData('0'+date%10);
}
0