//PT100自身温度范围#defineM_temper_MIN-200//最低温度#defineM_temper_MAX500//最高温度//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;//温度减//全局变量寄存器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);/*数码管显示温度*/voidPT100_delay(uintcount);/*PT100延时程序*/voidPT100_reset(void);/*PT100复位初始程序:根据PT100要求对其进行复位初始控制*/bitPT100_readbit(void);/*PT100位读子程序:从PT100取出一位数据*/ucharPT100_readbyte(void);/*PT100字节读子程序:读出的一字节数据并返回数据值*/voidPT100_writebyte(uchardat);/*PT100字节写子程序:将输入参数值写入PT100*/voidPT100_change(void);//启动PT100温度转换ucharPT100_Gettemper(void);/*读取PT100温度数据:单线方式读取PT100转换的温度数据,并返回处理后的温度值*/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)//上下限设置状态时,取消温度读取{PT100_change();//读取温度M_temper_curr=PT100_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);}voidPT100_delay(uintcount)//PT100特定延时{uinti;while(count){i=200;while(i0)i--;count--;}}voidPT100_reset(void)//PT100复位初始{uinti;DQ=0;i=103;while(i0)i--;DQ=1;i=4;while(i0)i--;}bitPT100_readbit(void)//PT100位读子程序{uinti;bitdat;DQ=1;_nop_();DQ=0;i++;DQ=1;i++;i++;dat=DQ;i=8;while(i0)i--;return(dat);}ucharPT100_readbyte(void)//PT100字节读子程序{uchari,j,dat;dat=0;for(i=1;i=8;i++){j=PT100_readbit();dat=(j7)|(dat1);//把读取的数据一位一位放进去}return(dat);}voidPT100_writebyte(uchardat)//PT100字节写子程序{uinti;ucharj;bittestb;for(j=1;j=8;j++){testb=dat&0x01;dat=dat1;if(testb)//write1{DQ=0;i++;i++;DQ=1;i=8;while(i0)i--;}else{DQ=0;//write0i=8;while(i0)i--;DQ=1;i++;i++;}}}voidPT100_change(void)//启动PT100温度转换{PT100_reset();PT100_delay(1);PT100_writebyte(0xcc);PT100_writebyte(0x44);}ucharPT100_Gettemper()//读取PT100温度数据{uinttemp;ucharM_temper_L=0,M_temper_H=0;PT100_reset();PT100_delay(1);PT100_writebyte(0xcc);PT100_writebyte(0xbe);M_temper_L=PT100_readbyte();M_temper_H=PT100_readbyte();temp=M_temper_H;//温度数据处理temp=8;temp=temp|M_temper_L;if(M_temper_H0xf0)//正(0)负(1)温度值{temp=((0x1000-(temp&0x0fff))/16)+127;//计算温度}else{temp=temp/16;}M_temper_H=temp;returnM_temper_H;}voidLED_data_BCD(charDisp_dat)//显示数据LED编码处理{if(Disp_dat0)//负温度{M_LED1=0X0a;//调用第10个M_LED2=(0-Disp_dat)/10;M_LED3=(0-Disp_dat)%10;}else{M_LED1=Disp_dat/100;//正温度M_LED2=(Disp_dat%100)/10;M_LED3=(Disp_dat%100)%10;}if(B_set)//设置状态时显示字符{if(B_set_AI){M_LED4=0x0c;}else{M_LED4=0x0b;}}else{M_LED4=0x0d;}}voidT0int_init()//定时器T0初始化{TMOD=0X11;TH0=(65536-XTAL/12*T0_TIMER)/256;TL0=(65536-XTAL/12*T0_TIMER)%256;EA=1;ET0=1;TR0=1;}voidT1int_init()//定时器T1初始化{TMOD=0X11;TH1=(65536-XTAL/12*T1_TIMER)/256;TL1=(65536-XTAL/12*T1_TIMER)%256;EA=1;ET1=1;TR1=1;}voidDelay_ms(uin