资讯详情

激光PM2.5传感器C51单片机程序

#include

#define uchar unsigned char

#define uint unsigned int

typedef unsigned charU8; // ???8???

typedef signed charS8; // ???8???

typedef unsigned int U16; // ???16???

typedef signed int S16; // ???16???

typedef unsigned longU32; // ???32???

typedef signed longS32; // ???32???

typedef float F32; // ???32?

typedef double F64; // ???64?

uchar Uart_Con=0;

uchar Uart_Change=0;

uintPM25_Data=0;

uintPM25_Data_L=0;

uintPM25_Data_H=0;

uchar PM25_View_Data=0;

uchar PM25_Set=100;

uchar PM25_View_change=0;

uchar num;

uchar buf;

uchar PM25set=205;

//#define Data_0_time 4

sbit lcdrs=P2^5;

sbit lcdrw=P2^6;

sbit lcden=P2^7;

sbit deng=P1^1;

sbit BEEP=P1^0;

sbit A=P2^0;

uchar table1[]={" PM25= ---ug/m3 "}; // uchar code table[]={" 2016-5-7 sat "};

uchar table2[]={" PM25_set=200 "}; // uchar code table1[]={" 00:00:00 "};

uchar PM25,shidu;

uint n;

int i=0;

float v0;

void delay_1ms(uint t)

{

uint i,j;

for(i=0;i

for(j=0;j<123;j )

{;}

}

void Delay(U16 j)

{U8 i;

for(;j>0;j--)

for(i=0;i<27;i );

}

void DelayMS(uint x)

{ uchar t;

while(x--)

for(t=0;t<80;t );

}

void ddelay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

void InitUART(void)

{

TMOD |= 0x20;

SCON = 0x50;

TH1 = 0xFD;

TL1 = TH1;

PCON = 0x00;

TR1 = 1;

ES = 1;

EA = 1;

}

void UARTInterrupt(void) interrupt 4 //pm2.5读取部分

{

uchar Uart_Data=0;

if(RI)

{

RI = 0;

Uart_Data=SBUF;

switch(Uart_Con)

{

case 0:

{

if(Uart_Data==0xaa)

{

Uart_Con ; A=1;A=1;A=1;A=1;A=1;

}

else

Uart_Con=0;

break;

}

case 1:

{

if(Uart_Data==0xc0)

Uart_Con ;

else

Uart_Con=0;

break;

}

case 2:

{

PM25_Data_L=Uart_Data;

Uart_Con ;

break;

}

case 3:

{

PM25_Data_H=Uart_Data;

Uart_Con=0;

break;

}

default:Uart_Con = 0;

}

A=0;

PM25_Data=PM25_Data_H*256 PM25_Data_L;

Uart_Change=1;

}

else

TI = 0;

}

void PM25_disword()

{

uchar PM25bai,PM25shi,PM25ge;

if(Uart_Change==1)

{

Uart_Change=0;

PM25bai=PM25_Data/100 0x30;

PM25shi=PM25_Data/10 0x30;

PM25ge=PM25_Data 0x30;

table1[7]=PM25bai;

table1[8]=PM25shi;

table1[9]=PM25ge;

PM25_View_change=1;

}

}

void LCD_WriteCom(uchar com)

{

lcdrs=0;

lcdrw=0;

P0=com;

ddelay(5);

lcden=1;

ddelay(5);

lcden=0;

}

void LCD_WriteData(uchar dat)

{

lcdrs=1;

p>lcdrw=0;

P0=dat;

ddelay(5);

lcden=1;

ddelay(5);

lcden=0;

}

void Initlcd()

{

ddelay(100);

LCD_WriteCom(0x38);

LCD_WriteCom(0x38);

LCD_WriteCom(0x38);

LCD_WriteCom(0x0c);

LCD_WriteCom(0x06);

LCD_WriteCom(0x01);

ddelay(100);

}

void Lcd_write(uchar Adress_Com,uchar *Adress_Data)

{

uchar i;

uchar Data_Word;

LCD_WriteCom(Adress_Com);//写入字符的显示位置

for(i=0;i<16;i++)  //写入字符

{

Data_Word=*Adress_Data;

LCD_WriteData(Data_Word);

Adress_Data++;

}

}

void main()

{

BEEP=0;

Initlcd();

InitUART();

while(1)

{

PM25_disword();

Lcd_write(0x80,table1);

Lcd_write(0xc0,table2);

if(PM25_Data>=PM25set)

{

BEEP=1;

deng=0;

delay_1ms(500);

BEEP=0;

deng=1;

delay_1ms(500);

}

else

{

BEEP=0;

deng=1;

Lcd_write(0x80,table1);

Lcd_write(0xc0,table2);

}

}

}

标签: con021传感器z系列激光传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台