?/////////////////////////////////////*PM2.5模块示例程序*//*显示使用LCD1602*//*PM2.5模块采用攀藤G3串口传感器*//*coder:karence*//*2015.12.20*/////////////////////////////////////#include<intrins.h>#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharsbitrs=P2^7;sbitrw=P2^6;sbiten=P2^5;ucharcodetable[]="PM1PM2.5PM10";ucharbuff[30];uintpm1=0;uintpm2=0;uintpm10=0;voiddelay(uintz)//延时{uintx,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}voidUartInit(void)//9600bps@12.000MHz{PCON&=0x7F;//??????SCON=0x50;//8???,?????AUXR|=0x40;//???1???Fosc,?1TAUXR&=0xFE;//??1?????1???????TMOD&=0x0F;//?????1???TMOD|=0x20;//?????1?8???????TL1=0xD9;//??????TH1=0xD9;//????????ET1=0;//?????1??TR1=1;//?????1}ucharUart_Buf;ucharRec_Addr=0;ucharRec_Uart=0;ucharRecive_Buf[22]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};bitrec_ok_flag=0;voidUART1_interrupt(void)interrupt4{EA=0;if(TI){TI=0;};if(RI){RI=0;//??????Uart_Buf=SBUF;//??????switch(Rec_Uart){case0://?????if(Uart_Buf==0x42)Rec_Uart=1;//??????elseRec_Uart=0;break;case1://?????if(Uart_Buf==0x4d)Rec_Uart=2;//??????elseRec_Uart=0;break;case2:{Recive_Buf[Rec_Addr]=Uart_Buf;//???????????????Rec_Addr++;if(Rec_Addr>=22){Rec_Uart=0;//????????rec_ok_flag=1;//?????????Rec_Addr=0;}}break;}//endswitch}//endRIEA=1;}/*数据结构xxxxxxxx|1|2|3|1|2|3|xxxxxxxxxxxxxxxx424d001411112222333344445555666600000000aaaabbbbbuff=[01020304050607080910111213141516171819202122]*/voidgetdata(void)//转换数据{pm1=Recive_Buf[2]<<8;pm1|=Recive_Buf[3];pm2=Recive_Buf[4]<<8;pm2|=Recive_Buf[5];pm10=Recive_Buf[6]<<8;pm10|=Recive_Buf[7];}voidwrite1602_com(uintcom)//写1602地址{rs=0;rw=0;P0=com;delay(5);en=1;delay(5);en=0;}voidwrite1602_date(uchardate)//写1602数据{rs=1;rw=0;P0=date;delay(5);en=1;delay(5);en=0;}voidinit1602(void)//初始化1602{write1602_com(0x38);write1602_com(0x0c);write1602_com(0x06);write1602_com(0x01);}voiddisplay(uintadd,temp)//显示xxx{write1602_com(add);//write1602_date(temp/1000+0x30);//delay(1);write1602_date(temp%1000/100+0x30);delay(1);write1602_date(temp%100/10+0x30);delay(1);write1602_date(temp%10+0x30);delay(1);}voiddisplay_head()//表头{ucharnum;write1602_com(0x80);//?úμúò?DD?aí·??ê?for(num=0;num<16;num++){write1602_date(table[num]);delay(5);}}voiddisplayall(void)//显示数据{display(0xc0,pm1);display(0xc0+6,pm2);display(0xc0+12,pm10);}voidclear(void)//清空屏幕{ucharnum;write1602_com(0x80);//第一行for(num=0;num<16;num++){write1602_date(0x10);delay(5);}write1602_com(0xc0);//第二行for(num=0;num<16;num++){write1602_date(0x10);delay(5);}}voidmain(void)//主函数{init1602();//初始化1602UartInit();//初始化UARTclear();//清屏displayall();//数据display_head();//表头ES=1;//内部中断EA=1;//总中断使能while(1){if(rec_ok_flag==1)//得到UART标记位{getdata();//转换数据displayall();//显示数据rec_ok_flag=0;//清除标记位}}}