您好,欢迎访问三七文档
当前位置:首页 > 法律文献 > 理论/案例 > 单片机控制二氧化碳传感器程序
#includereg52.h//头文件#includeintrins.h#defineucharunsignedchar//宏定义无符号字符型#defineuintunsignedint//宏定义无符号整型typedefunsignedcharBYTE;typedefbitBOOL;sbitST=P3^0;//A/D启动转换信号sbitOE=P3^1;//数据输出允许信号sbitEOC=P3^2;//A/D转换结束信号sbitCLK=P3^3;//时钟脉冲sbitLCD_RS=P2^0;sbitLCD_RW=P2^1;sbitLCD_EN=P2^2;ucharm;uintAD0809,i,cdisplay,ppm;uintppmdou;//定义数据类型voiddelay(intms){//延时子程序while(ms--){for(i=0;i250;i++){_nop_();_nop_();_nop_();_nop_();}}}BOOLlcd_bz(){//测试LCD忙碌状态BOOLresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;_nop_();_nop_();_nop_();_nop_();result=(BOOL)(P0&0x80);LCD_EN=0;returnresult;}voidlcd_wcmd(BYTEcmd){//写入指令数据到LCDwhile(lcd_bz());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;_nop_();_nop_();_nop_();_nop_();LCD_EN=1;_nop_();_nop_();_nop_();_nop_();LCD_EN=0;}voidlcd_pos(BYTEpos){//设定显示位置lcd_wcmd(pos|0x80);}voidlcd_wdat(BYTEdat){//写入字符显示数据到LCDwhile(lcd_bz());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;_nop_();_nop_();_nop_();_nop_();LCD_EN=1;_nop_();_nop_();_nop_();_nop_();LCD_EN=0;}voidlcd_init(){//LCD初始化设定lcd_wcmd(0x38);//16*2显示,5*7点阵,8位数据delay(1);lcd_wcmd(0x0c);//显示开,关光标delay(1);lcd_wcmd(0x06);//移动光标delay(1);lcd_wcmd(0x01);//清除LCD的显示内容delay(1);}voidtimer0()interrupt1//定时器0工作方式1{TH0=(65536-2)/256;//重装计数初值TL0=(65536-2)%256;//重装计数初值CLK=!CLK;//取反}/*************************************************************************主函数**************************************************************************/voidmain(){lcd_init();//初始化LCDdelay(10);TMOD=0X01;//定时器中断0CLK=0;//脉冲信号初始值为0TH0=(65536-2)/256;//定时时间高八位初值TL0=(65536-2)%256;//定时时间低八位初值EA=1;//开CPU中断ET0=1;//开T/C0中断TR0=1;ST=0;OE=0;while(1)//无限循环{ST=0;//使采集信号为低ST=1;//开始数据转换ST=0;//停止数据转换while(!EOC);//等待数据转换完毕OE=1;//允许数据输出信号m=P1;//读取数据OE=0;//关闭数据输出允许信号AD0809=(uint)m;cdisplay=(uint)((AD0809*(5.00/255))*1000);if(cdisplay1072){ppm=400;}elseif((1049cdisplay)&&(cdisplay=1072)){ppmdou=(500-((cdisplay-1049)*(100/(1072-1049))));ppm=(uint)ppmdou;}elseif((1036cdisplay)&&(cdisplay=1049)){ppmdou=(600-((cdisplay-1036)*(100/(1049-1036))));//ppmdou=(500-(ppmd-1049)*(100/(1072-1049)));ppm=(uint)ppmdou;}elseif((1023cdisplay)&&(cdisplay=1036)){ppmdou=(700-(cdisplay-1023)*(100/(1036-1023)));ppm=(uint)ppmdou;}elseif((1016cdisplay)&&(cdisplay=1023)){ppmdou=(800-((cdisplay-1016)*(100/(1023-1016))));ppm=(uint)ppmdou;}elseif((1006cdisplay)&&(cdisplay=1016)){ppmdou=(900-((cdisplay-1006)*(100/(1016-1006))));ppm=(uint)ppmdou;}elseif((1000cdisplay)&&(cdisplay=1006)){ppmdou=(1000-((cdisplay-1000)*(100/(1016-1000))));ppm=(uint)ppmdou;}elseif((966cdisplay)&&(cdisplay=1000)){ppmdou=(2000-((cdisplay-966)*(1000/(1000-966))));ppm=(uint)ppmdou;}elseif((947cdisplay)&&(cdisplay=966))//2000-3000{ppmdou=(3000-((cdisplay-947)*(1000/(966-947))));ppm=(uint)ppmdou;}elseif((924cdisplay)&&(cdisplay=947))//3000-4000{ppmdou=(4000-((cdisplay-924)*(1000/(947-924))));ppm=(uint)ppmdou;}elseif((907cdisplay)&&(cdisplay=924))//4000-6000{ppmdou=(6000-(cdisplay-907)*(2000/(924-907)));ppm=(uint)ppmdou;}elseif((891cdisplay)&&(cdisplay=907))//6000-8000{ppmdou=(8000-(cdisplay-891)*(2000/(907-891)));ppm=(uint)ppmdou;}elseif((874cdisplay)&&(cdisplay=891))//8000-10000{ppmdou=(10000-(cdisplay-874)*(2000/(891-874)));ppm=(uint)ppmdou;}elseif((0cdisplay)&&(cdisplay=874))//8000-10000{ppm=10000;}lcd_pos(0x81);lcd_wdat(ppm/10000+0x30);lcd_pos(0x82);lcd_wdat((ppm%10000)/1000+0x30);lcd_pos(0x83);lcd_wdat((ppm%1000)/100+0x30);lcd_pos(0x84);lcd_wdat((ppm%100)/10+0x30);lcd_pos(0x85);lcd_wdat(ppm%10+0x30);lcd_pos(0x86);lcd_wdat('p');lcd_pos(0x87);lcd_wdat('p');lcd_pos(0x88);lcd_wdat('m');}}
本文标题:单片机控制二氧化碳传感器程序
链接地址:https://www.777doc.com/doc-5067903 .html