聚丰项目 > 移动天气观测站
该作品基于stm32平台开发,可以检测当前环境温度,风向风速,当前环境的pm2.5值
kidsure
分享kidsure
团队成员
kidsure 创客
通过风力风速传感器,pm2.5传感器等检测空气质量
#include<reg52.h>
#include<intrins.h>
#include<function.h>
#define uint unsigned int
#define uchar unsigned char
typedef unsigned char byte;
typedef unsigned char bool;
//uchar code dis1[]={"123456789"} ;
uchar code dis2[]={"ppm"};
sfr AUXR1=0XA2;//不知道是什么
sfr ADC_CONTR=0XBC;//adc控制寄存器,不可位寻址,用或运算来设置
sfr ADC_RES=0XBD; //转换结果寄存器,高八位
sfr ADC_RES1=0XBE;//转换结果寄存器,低两位
sfr P1ASF=0X9D;//设置p1的那一口为输入口,不可位寻址.
#define ADC_POWER 0X80 //adc电源,第7位
#define ADC_FLAG 0X10 //转换结束标志位,第4位
#define ADC_START 0X08 //转换开始,第3位
#defineADC_SPEEDLL 0X00 //速度位,第5,6位 540
#define ADC_ SPEEDL 0X20 //360
#define ADC_SPEEDH 0X40//180
#define ADC_SPEEDHH 0X60 //90
uint i,j,display=6,t02s;
double c,cdisplay10,ppmd,ppmdou;
int cdisplay,low2;
int wendufazhi=50;
int count ,tt=30;
uint ppm;
uint getADCresult(int n);
void ADC_init()
{
P1ASF=0XFF;//P1口全部作为模拟功能AD使用
ADC_RES=0;//清零转换结果寄存器高8位
ADC_RES1=0; //清零转换结果寄存器低2位
ADC_CONTR=ADC_POWER|ADC_SPEEDLL;//开启AD电源
delay1(2);
}
void ADC0()
{
int i=0,shiwen=0;
for(i=0;i<100;i++)
{
shiwen=getADCresult(0);
c=(double)(shiwen*(5.00/1024))*1000;
cdisplay10+=c;
}
ppmd=cdisplay10/100;
cdisplay10=0;
if(cdisplay>1072)
ppm=400;
if((1049<cdisplay)&&(cdisplay<=1072))
{
ppmdou=(500-((cdisplay-1049)*(100/(1072-1049))));
ppm=(int)ppmdou;
}
if((1036<cdisplay)&&(cdisplay<=1049))
{
ppmdou=(600-((cdisplay-1036)*(100/(1049-1036))));
ppm=(int)ppmdou;
}
if((1023<cdisplay)&&(cdisplay<=1036))
{
ppmdou=(700-((cdisplay-1023)*(100/(1036-1023))));
ppm=(int)ppmdou;
}
if((1016<cdisplay)&&(cdisplay<=1023))
{
ppmdou=(800-((cdisplay-1016)*(100/(1023-1016))));
ppm=(int)ppmdou;
}
if((1006<cdisplay)&&(cdisplay<=1016))
{
ppmdou=(900-((cdisplay-1006)*(100/(1016-1006))));
ppm=(int)ppmdou;
}
if((1000<cdisplay)&&(cdisplay<=1006))
{
ppmdou=(1000-((cdisplay-1000)*(100/(1006-1000))));
ppm=(int)ppmdou;
}
if((966<cdisplay)&&(cdisplay<=1000))
{
ppmdou=(2000-((cdisplay-966)*(100/(1000-966))));
ppm=(int)ppmdou;
}
if((947<cdisplay)&&(cdisplay<=966))
{
ppmdou=(3000-((cdisplay-947)*(100/(966-947))));
ppm=(int)ppmdou;
}
if((924<cdisplay)&&(cdisplay<=947))
{
ppmdou=(4000-((cdisplay-924)*(100/(947-924))));
ppm=(int)ppmdou;
}
if((907<cdisplay)&&(cdisplay<=924))
{
ppmdou=(6000-((cdisplay-907)*(100/(924-907))));
ppm=(int)ppmdou;
}
if(ppm==400)
{
lcd_pos(1,5);
lcd_data('<');
}
if(ppm==10000)
{
lcd_pos(1,5);
lcd_data('>');
}
lcd_pos(1,1);
for(i=0;i<=8;i++)
{
lcd_data(tab2[i]);
delay2(10);
}
lcd_pos(1,6);
lcd_data(ppm/10000);
lcd_data((ppm%10000)/1000);
lcd_pos(1,7);
lcd_data((ppm%1000)/100);
lcd_data((ppm%100)/10);
lcd_pos(1,8);
lcd_data(ppm%10);
lcd_pos(1,9);
for(i=0;i<=2;i++)
{
lcd_data(dis2[i]);
}
delay1(10);
}
//void flash();
uint getADCresult(int n)
{
ADC_CONTR=ADC_POWER|ADC_SPEEDLL|n|ADC_START;
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR&ADC_FLAG));//等待转换结束
delay1(2);
ADC_CONTR&=~ADC_FLAG;//关闭adc
return(ADC_RES*4+ADC_RES1) ;
}