您好,欢迎访问三七文档
#includereg51.h#includeintrins.h#includemath.h#defineucharunsignedchar#defineuintunsignedint//¾§Õñ¶¨Ê±¼ÆÊýÖµ#defineXTAL11059200//¾§ÕñΪ11.0592M#defineT1_TIMER10000//T1¼ÆÊý(us)#defineT0_TIMER60000//T0¼ÆÊý(us)#defineT0_TIMER_S500//T0¶þ¼¶¼ÆÊý//DS18B20×ÔÉíζȷ¶Î§#defineM_temper_MIN-55//×îµÍζÈ#defineM_temper_MAX125//×î¸ßζÈ//λ¿ØÖƵçƽ¶¨Òå#defineB_RED_OFF1//ºìµÆÃ𣨸ߵçƽ£©#defineB_RED_ON0//ºìµÆÁÁ£¨µÍµçƽ£©#defineB_BUZZ_OFF1//·äÃùÆ÷#defineB_BUZZ_ON0#defineB_GREEN_OFF1//Â̵Æ#defineB_GREEN_ON0//CPUÒý½Å¶¨Òå#defineP_LEDP0sbitLED_S1=P2^0;//LED1λѡsbitLED_S2=P2^1;//LED2λѡsbitLED_S3=P2^2;//LED3λѡsbitLED_S4=P2^3;//LED4λѡsbitKEY_S1=P1^0;//ÉÏÏÂÏÞζÈÉèÖÃsbitKEY_S2=P1^1;//ζȼÓsbitKEY_S3=P1^2;//ζȼõsbitRED_ALARM=P3^1;//ºìɫָʾµÆsbitGREEN_ALARM=P3^0;//ÂÌɫָʾµÆsbitBUZZ=P3^2;//·äÃùÆ÷sbitDQ=P3^7;//DS18B20Êý¾Ý½Ó¿Ú//È«¾Ö±äÁ¿¼Ä´æÆ÷ucharM_LED1,M_LED2,M_LED3,M_LED4;//4λLEDÏÔʾ¼Ä´æÆ÷ucharLED_DISP_TAB[]=//LED±àÂë±í{0X3f,0X06,0X5b,0X4f,0X66,0X6d,0X7d,0X07,0X7f,0X6f,//0-90X40,0X23,0X1c,0X58//×Ö·û:-nuc};ucharSet_count;//ζÈÉèÖð´Å¥¼ÆÊýucharT0_count;//T0¶¨Ê±ÖжϼÆÊýuintT1_count;//T1¶¨Ê±ÖжϼÆÊýcharM_temper_up;//ζÈÉÏÏÞcharM_temper_down;//ζÈÏÂÏÞcharM_temper_AI;//ÉèÖÃÖÐζÈcharM_temper_conver;//µ±Ç°Î¶ÈbitB_set,B_set_AI;//ζÈÉèÖñêÖ¾bitB_set_T0,B_set_T1;//¶¨Ê±Æ÷ÖжϱêÖ¾//º¯ÊýÉùÃ÷voidT1int_init();/*¶¨Ê±Æ÷T1³õʼ»¯£ºÉèÖù¤×÷·½Ê½¸³³õÖµ¿ªÆôÖжÏ*/voidT0int_init();/*¶¨Ê±Æ÷T0³õʼ»¯£ºÉèÖù¤×÷·½Ê½¸³³õÖµ¿ªÆôÖжÏ*/voidSet_temper();/*ÉèÖÃÉÏÏÂÏÞζȣº°´¼ü´ÎÊý=1£ºÏÔʾÉÏÏÞζȲ¢´¦ÓÚ²Ù×÷״̬°´¼ü´ÎÊý=2£ºÏÔʾÏÂÏÞζȲ¢´¦ÓÚ²Ù×÷״̬°´¼ü´ÎÊý=3£º±£´æÉèÖÃÖµ²¢Í˳ö*/voidInc_temper();/*Ìá¸ßÉèÖõÄζÈ*/voidDec_temper();/*½µµÍÉèÖõÄζÈ*/voidLED_data_BCD(charDisp_dat);/*ÊýÂë¹ÜÏÔʾζÈ*/voidDs18b20_delay(uintcount);/*DS18B20ÑÓʱ³ÌÐò*/voidDs18b20_reset(void);/*DS18B20¸´Î»³õʼ³ÌÐò£º¸ù¾ÝDS18B20ÒªÇó¶ÔÆä½øÐи´Î»³õʼ¿ØÖÆ*/bitDs18b20_readbit(void);/*DS18B20λ¶Á×Ó³ÌÐò£º´ÓDS18B20È¡³öһλÊý¾Ý*/ucharDs18b20_readbyte(void);/*DS18B20×Ö½Ú¶Á×Ó³ÌÐò£º¶Á³öµÄÒ»×Ö½ÚÊý¾Ý²¢·µ»ØÊý¾ÝÖµ*/voidDs18b20_writebyte(uchardat);/*DS18B20×Ö½Úд×Ó³ÌÐò£º½«ÊäÈë²ÎÊýֵдÈëDS18B20*/voidDs18b20_change(void);//Æô¶¯DS18B20ζÈת»»ucharDs18b20_Gettemper(void);/*¶ÁÈ¡DS18B20ζÈÊý¾Ý£ºµ¥Ïß·½Ê½¶ÁÈ¡DS18B20ת»»µÄζÈÊý¾Ý£¬²¢·µ»Ø´¦ÀíºóµÄζÈÖµ*/voidDelay_ms(uintcount);//ÑÓʱ×Ó³ÌÐò£ºÑÓʱcount(ms)voidmain(){ucharM_temper_curr;SP=0x50;//¶ÑÕ»ÆðʼµØÖ·M_temper_up=100;//³õʼζÈÉÏÏÂÏÞM_temper_down=0;Delay_ms(500);T0int_init();//T0T1³õʼ»¯T1int_init();while(1){if(B_set_T0)//T0¶¨Ê±¶ÁȡζȱêÖ¾{EA=0;//¹Ø×ÜÖжÏB_set_T0=0;if(!B_set)//ÉÏÏÂÏÞÉèÖÃ״̬ʱ£¬È¡ÏûζȶÁÈ¡{Ds18b20_change();//¶ÁȡζÈM_temper_curr=Ds18b20_Gettemper();M_temper_conver=(M_temper_curr126?(127-M_temper_curr):M_temper_curr);//ת»»Õý¸ºÎ¶ÈLED_data_BCD(M_temper_conver);//ת»»LEDÏÔʾ}EA=1;}if(B_set_T1)//T1¶¨Ê±LEDÏÔʾɨÃè±êÖ¾{TR1=0;B_set_T1=0;++T1_count;switch(T1_count)//ÒÀ´ÎÏÔʾ4λLED{case1:LED_S4=1;P_LED=LED_DISP_TAB[M_LED1];LED_S1=0;break;case2:LED_S1=1;P_LED=LED_DISP_TAB[M_LED2];LED_S2=0;break;case3:LED_S2=1;P_LED=LED_DISP_TAB[M_LED3];LED_S3=0;break;case4:LED_S3=1;P_LED=LED_DISP_TAB[M_LED4];LED_S4=0;T1_count=0;break;}TR1=1;//¿ªT1¶¨Ê±Æ÷}if((M_temper_converM_temper_down)&&(M_temper_converM_temper_up)){//ζÈÔÚÉÏÏÂÏÞ·¶Î§ÄÚBUZZ=B_BUZZ_OFF;RED_ALARM=B_RED_OFF;GREEN_ALARM=B_GREEN_OFF;}else{BUZZ=B_BUZZ_ON;if(M_temper_converM_temper_down)RED_ALARM=B_RED_ON;//µÍÓÚÏÂÏÞζÈelseGREEN_ALARM=B_GREEN_ON;//¸ßÓÚÉÏÏÞζÈ}if(!KEY_S1)//°´¼ü¼ì²â{Set_temper();//ÉèÖÃζÈDelay_ms(200);}if(B_set){if(!KEY_S2){Inc_temper();//ζȼÓDelay_ms(200);}if(!KEY_S3){Dec_temper();//ζȼõDelay_ms(200);}}}}voidSet_temper()//ÉèÖÃζÈ{++Set_count;B_set=1;//ÉèÖñêÖ¾if(Set_count==1)//ÉèÖÃÉÏÏÞ{B_set_AI=0;LED_data_BCD(M_temper_up);M_temper_AI=M_temper_up;//½«ÔÉÏÏÞζÈÈ¡³ö}elseif(Set_count==2)//ÉèÖÃÏÂÏÞ{B_set_AI=1;M_temper_up=M_temper_AI;//´æÈëеÄÉÏÏÞζÈLED_data_BCD(M_temper_down);M_temper_AI=M_temper_down;//½«ÔÏÂÏÞζÈÈ¡³ö}else{//È·ÈÏÍ˳öB_set=0;Set_count=0;LED_data_BCD(M_temper_conver);M_temper_down=M_temper_AI;//´æÈëеÄÏÂÏÞζÈ}}voidInc_temper()//ζȼÓ{if(B_set_AI)//Åжϵ±Ç°ÊÇÉèÖÃÉÏÏÞ(0)»¹ÊÇÏÂÏÞ(1){if((M_temper_AIM_temper_MAX)&&(M_temper_AIM_temper_up)){M_temper_AI++;}//²»Äܼӵ½ÉÏÏßζȺÍζȷ¶Î§}else{if(M_temper_AIM_temper_MAX){M_temper_AI++;}}LED_data_BCD(M_temper_AI);}voidDec_temper()//ζȼõ{if(B_set_AI)//Åжϵ±Ç°ÊÇÉèÖÃÉÏÏÞ(0)»¹ÊÇÏÂÏÞ(1){if(M_temper_AIM_temper_MIN){M_temper_AI--;}}else{if((M_temper_AIM_temper_MIN)&&(M_temper_AIM_temper_down)){M_temper_AI--;}}LED_data_BCD(M_temper_AI);}voidDs18b20_delay(uintcount)//DS18B20Ìض¨ÑÓʱ{uinti;while(count){i=200;while(i0)i--;count--;}}voidDs18b20_reset(void)//DS18B20¸´Î»³õʼ{uinti;DQ=0;i=103;while(i0)i--;DQ=1;i=4;while(i0)i--;}bitDs18b20_readbit(void)//DS18B20λ¶Á×Ó³ÌÐò{uinti;bitdat;DQ=1;_nop_();DQ=0;i++;DQ=1;i++;i++;dat=DQ;i=8;while(i0)i--;return(dat);}ucharDs18b20_readbyte(void)//DS18B20×Ö½Ú¶Á×Ó³ÌÐò{uchari,j,dat;dat=0;for(i=1;i=8;i++){j=Ds18b20_readbit();dat=(j7)|(dat1);//°Ñ¶ÁÈ¡µÄÊý¾Ýһλһλ·Å½øÈ¥}return(dat);}voidDs18b20_writebyte(uchardat)//DS18B20×Ö½Úд×Ó³ÌÐò{uinti;ucharj;bittestb;for(j=1;j=8;j++){t
本文标题:温度控制-源程序
链接地址:https://www.777doc.com/doc-5094879 .html