聚豐項目 > 移動天氣觀測站
該作品基于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) ;
}