|
基于單片機的溫度控制的設(shè)計
單片機源程序如下:
- #include <reg51.h>
- #include <intrins.h>
- #include <DS18B20.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define SMGIO P0
- sbit SET=P3^2; //定義設(shè)定上下限鍵
- sbit ADD=P3^4; //定義增加鍵
- sbit DEC=P3^5; //定義減小限鍵
- sbit SMG1=P2^0; //定義數(shù)碼管第一位
- sbit SMG2=P2^1; //定義數(shù)碼管第二位
- sbit SMG3=P2^2; //定義數(shù)碼管第三位
- sbit SMG4=P2^3; //定義數(shù)碼管第四位
- sbit BEEP=P2^7; //定義蜂鳴器報警
- sbit LED1=P1^0; //風(fēng)扇降溫
- sbit LED2=P1^1; //加熱棒加熱
- uint tempValue; //變量 存儲當(dāng)前溫度值
- uint HI_Alarm=30; //初始上限
- uint LO_Alarm=25; //初始下限
- uchar SMG_CC[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92, 0x82,0xf8,0x80,0x90,0xff};
- uchar SMG_CC_DOT[]={0x40,0x79,0x24,0x30,0x19,0x12, 0x02,0x78,0x00,0x10,0xff};
- /********************************************************************/
- /* 1ms延時 */
- /********************************************************************/
- void delayms(uint x)
- {
- uchar i;
- while(x--)
- {
- for(i=150;i>0;i--);
- }
- }
- void Display_Temperature() //顯示溫度
- {
- char smg1,smg2,smg3,smg4;
- smg1=tempValue/1000; //以下表示將溫度值拆分開
- smg2=(tempValue-smg1*1000)/100;
- smg3=(tempValue%100)/10;
- smg4=tempValue%10;
- if(tflag==1) //負(fù)數(shù)顯示
- {
- SMGIO=SMG_CC[smg4]; //顯示小數(shù)位
- SMG4=0;
- delayms(1);
- SMG4=1;
- SMGIO=SMG_CC_DOT[smg3]; //顯示個數(shù)位
- SMG3=0;
- delayms(1);
- SMG3=1;
- if(smg2==0)
- {
- SMGIO=0x40; //顯示十?dāng)?shù)位
- SMG2=0;
- delayms(1);
- SMG2=1;
- SMGIO=0x00; //顯示百數(shù)位
- SMG1=0;
- delayms(1);
- SMG1=1;
- }
- else
- {
- SMGIO=SMG_CC[smg2]; //顯示十?dāng)?shù)位
- SMG2=0;
- delayms(1);
- SMG2=1;
- SMGIO=0x40; //顯示十?dāng)?shù)位
- SMG1=0;
- delayms(1);
- SMG1=1;
- }
- }
- else //正數(shù)顯示
- {
- SMGIO=SMG_CC[smg4]; //顯示小數(shù)位
- SMG4=0;
- delayms(1);
- SMG4=1;
- SMGIO=SMG_CC_DOT[smg3]; //顯示個數(shù)位
- SMG3=0;
- delayms(1);
- SMG3=1;
- if(smg1==0)
- {
- SMGIO=0x00;
- SMG1=0;
- delayms(1);
- SMG1=1;
- if(smg2==0)
- {
- SMGIO=0x00;
- SMG1=0;
- delayms(1);
- SMG1=1;
- }
- else
- {
- SMGIO=SMG_CC[smg2];
- SMG2=0;
- delayms(1);
- SMG2=1;
- }
- }
- else
- {
- SMGIO=SMG_CC[smg1];
- SMG1=0;
- delayms(1);
- SMG1=1;
- SMGIO=SMG_CC[smg2];
- SMG2=0;
- delayms(1);
- SMG2=1;
- }
- }
- }
- void Display_HI_Alarm()
- {
- SMGIO=~0x76; //顯示H
- SMG1=0;
- delayms(1);
- SMG1=1;
- SMGIO=~0x40; //顯示-
- SMG2=0;
- delayms(1);
- SMG2=1;
- SMGIO=SMG_CC[HI_Alarm/10]; //顯示報警高值十位
- SMG3=0;
- delayms(1);
- SMG3=1;
- SMGIO=SMG_CC[HI_Alarm%10]; //顯示報警高值個數(shù)位
- SMG4=0;
- delayms(1);
- SMG4=1;
- }
- void Display_LO_Alarm()
- {
- SMGIO=~0x38; //顯示L
- SMG1=0;
- delayms(1);
- SMG1=1;
- SMGIO=~0x40; //顯示-
- SMG2=0;
- delayms(1);
- SMG2=1;
- SMGIO=SMG_CC[LO_Alarm/10]; //顯示報警高值十位
- SMG3=0;
- delayms(1);
- SMG3=1;
- SMGIO=SMG_CC[LO_Alarm%10]; //顯示報警高值個數(shù)位
- SMG4=0;
- delayms(1);
- SMG4=1;
- }
- void SET_KEY()
- { bit SET_FLAG=1;
- if(SET==0) //設(shè)定按鍵按下
- {
- delayms(40);
- if(SET==1) //延時去除抖動再次判斷是否按下按鍵
- {
- while(SET_FLAG==1)
- {
- Display_HI_Alarm(); //設(shè)定上限溫度值
- if(ADD==0) //增加按鍵
- {
- delayms(40); //延時去除抖動再次判斷是否按下按鍵
- if(ADD==1)
- HI_Alarm++; //溫度值加一
- }
- if(DEC==0) //減小按鍵
- {
- delayms(40);
- if(DEC==1) //延時去除抖動再次判斷是否按下按鍵
- HI_Alarm--; //溫度值減一
- }
- if(SET==0) //以下表示 再次按下設(shè)定按鍵 進入設(shè)定下限程序
- {
- delayms(40);
- if(SET==1) //延時去除抖動再次判斷是否按下按鍵
- {
- while(1)
- {
- Display_LO_Alarm(); //設(shè)定下限溫度值
- if(ADD==0) //增加按鍵
- {
- delayms(40);
- if(ADD==1) //延時去除抖動再次判斷是否按下按鍵
- LO_Alarm++; //溫度值加一
- }
- if(DEC==0) //減小按鍵
- {
- delayms(40);
- if(DEC==1) //延時去除抖動再次判斷是否按下按鍵
- LO_Alarm--; //溫度值減一
- }
- if(SET==0) //設(shè)定按鍵按下
- {
- delayms(40);
- if(SET==1)
- { //延時去除抖動再次判斷是否按下按鍵
- SET_FLAG=0;
- break; //上下限設(shè)定完成 退出
- }
- }
- }
- }
- }
- }
- }
- }
- }
- void BJ_LED()
- {
- if(tempValue>=HI_Alarm*10) //如果溫度超過上限
- {
- BEEP=0;LED1=0; LED2=1;
- }
- if(tempValue<=LO_Alarm*10) //如果溫度低過下限
- {
- BEEP=0;LED2=0;LED1=1;
- }
- if(tempValue>LO_Alarm*10&&tempValue<HI_Alarm*10)
- {
- BEEP=1;LED1=1;LED2=1;
- }
- }
- void main()
- {
- BEEP=1;
- LED1=1;
- LED2=1; //表示先清除報警
- while(1)
- {
- tempValue=ReadTemperature(); //讀取溫度值
- Display_Temperature(); //顯示溫度數(shù)據(jù)
- BJ_LED(); //調(diào)用報警函數(shù)
- SET_KEY(); //調(diào)用按鍵讀取函數(shù)
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
1-程序.zip
(61.92 KB, 下載次數(shù): 47)
2018-5-9 15:53 上傳
點擊文件名下載附件
溫度控制
|
|