電腦可調(diào)溫度控制器源程序
/*-------------------------------
溫度控制器V1.5
顯示為三個(gè)共陽極LED
溫度傳感器用單總線DS18B20
CPU為2051,四個(gè)按鍵,分別為UP,DOWN,SET
溫度調(diào)節(jié)上限為125度,下限為-55度
只能用于單只18B20
本軟件僅供學(xué)習(xí)與參考,引用時(shí)請(qǐng)注明版權(quán)
http://www.cdle.net /> 明浩 2004/1/2
pnzwzw@cdle.net
-------------------------------*/
#include
#include
#define Key_UP P3_0 //上調(diào)溫度
#define Key_DOWN P3_1 //下調(diào)溫度
#define Key_SET P1_7 //設(shè)定鍵(溫度設(shè)定,長(zhǎng)按開電源)
#define RelayOutPort P3_5 //繼電器輸出
#define LEDPort P1 //LED控制口
#define LEDOneC P3_2 //LED DS1控制(百位)
#define LEDTwoC P3_3 //LED DS2控制(十位)
#define LEDThreeC P3_4 //LED DS3控制(個(gè)位)
#define TMPort P3_7 //DS1820 DataPort
unsigned char code LEDDis[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF}; //0-9的LED筆劃,0xFF為空,0xF7為負(fù)號(hào)
static unsigned char bdata StateREG; //可位尋址的狀態(tài)寄存器
sbit DS1820ON = StateREG^0; //DS1820是否存在
sbit SetTF = StateREG^1; //是否是在溫度設(shè)置狀態(tài)
sbit KeySETDown = StateREG^2; //是否已按過SET鍵標(biāo)識(shí)
sbit PowTF = StateREG^3; //電源電源標(biāo)識(shí)
sbit KeyTF = StateREG^4; //鍵盤是否允許
//sbit KeySETDowning = StateREG^5; //SET是否正在按下
static unsigned char bdata TLV _at_ 0x0029; //溫度變量高低位
static unsigned char bdata THV _at_ 0x0028;
static signed char TMV; //轉(zhuǎn)換后的溫度值
static unsigned char KeyV,TempKeyV; //鍵值
static signed char TMRomV _at_ 0x0027; //高溫限制
static signed char TMSetV _at_ 0x0026; //溫度設(shè)定值
static unsigned char KSDNum; //SET鍵連按時(shí)的采集次數(shù)
static unsigned char IntNum,IntNum2,IntNum3; //中斷發(fā)生次數(shù),IntNum用于SET長(zhǎng)按檢測(cè),IntNum2用于設(shè)定狀態(tài)時(shí)LED閃爍
static signed char LED_One,LED_Two,LED_Three; //LED的顯示位 LED_One為十位,LED_Two為個(gè)位
static unsigned char Sign; //負(fù)號(hào)標(biāo)識(shí)
void main(void)
{
void InitDS1820(void); //定義函數(shù)
void ROMDS1820(void);
void TMVDS1820(void);
void TMRDS1820(void);
void TMWDS1820(void);
void TMREDS1820(void);
void TMERDS1820(void);
void ReadDS1820(void);
void WriteDS1820(void);
void Delay_510(void);
void Delay_110(void);
void Delay_10ms(void);
void Delay_4s(void);
void V2ToV(void);
StateREG = 0; //初始化變量
SetTF = 1;
PowTF = 1; //關(guān)電源
THV = 0;
TLV = 0;
TMV = 0;
KeyV = 0;
TempKeyV = 0;
KSDNum = 0;
IntNum = 0;
IntNum2 = 0;
IntNum3 = 0;
LED_One = 0;
LED_Two = 0;
InitDS1820(); //初始化
ROMDS1820(); //跳過ROM
TMERDS1820(); //E2PRAM中溫度上限值調(diào)入RAM
InitDS1820(); //初始化
ROMDS1820(); //跳過ROM
TMRDS1820(); //讀出溫度指令
ReadDS1820(); //讀出溫度值和上限值
TMSetV = TMRomV; //拷貝保存在DS18B20ROM里的上限值到TMSetV
EA = 1; //允許CPU中斷
ET0 = 1; //定時(shí)器0中斷打開
TMOD = 0x1; //設(shè)定時(shí)器0為模式1,16位模式
TH0=0xB1;
TL0=0xDF; //設(shè)定時(shí)值為20000us(20ms)
TR0 = 1; //開始定時(shí)
while(1);
}
//定時(shí)器0中斷外理中鍵掃描和顯示
void KeyAndDis_Time0(void) interrupt 1 using 2
{
TH0=0xB1;
TL0=0xDF; //設(shè)定時(shí)值為20000us(20ms)
LEDPort = 0xFF;
if (!Key_UP)
KeyV = 1;
if (!Key_DOWN)
KeyV = 2;
if (!Key_SET)
KeyV = 3;
//KeySETDowning = 0; //清除
if (KeyV != 0) //有鍵按下
{
Delay_10ms(); //延時(shí)防抖 按下10ms再測(cè)
if (!Key_UP)
TempKeyV = 1;
if (!Key_DOWN)
TempKeyV = 2;
if (!Key_SET)
TempKeyV = 3;
if (KeyV == TempKeyV) //兩次值相等為確定接下了鍵
{
if (KeyV == 3) //按下SET鍵,如在SET狀態(tài)就退出,否則進(jìn)入
{
//KeySETDowning = 1; //表明SET正在按下
PowTF = 0; //電源標(biāo)識(shí)開
if (!KeyTF)
if (SetTF)
{
SetTF = 0; //標(biāo)識(shí)位標(biāo)識(shí)退出設(shè)定
InitDS1820(); //初始化
ROMDS1820(); //跳過ROM
TMWDS1820(); //寫溫度上限指令
WriteDS1820(); //寫溫度上限到DS18B20ROM
WriteDS1820(); //寫溫度上限到DS18B20ROM
WriteDS1820(); //寫溫度上限到DS18B20ROM
InitDS1820(); //初始化
ROMDS1820(); //跳過ROM
TMREDS1820(); //溫度上限值COPY回E2PRAM
}
else
SetTF = 1;
if (!KeySETDown) //沒有第一次按下SET時(shí),KeySETDown標(biāo)識(shí)置1
KeySETDown = 1;
else
KSDNum = KSDNum + 1; //前一秒內(nèi)有按過SET則開始計(jì)數(shù)
}
if (SetTF) //在SET狀態(tài)下
{
if ((KeyV == 1) (!KeyTF))
TMSetV = TMSetV + 1; //上調(diào)溫度
if ((KeyV == 2) (!KeyTF))
TMSetV = TMSetV - 1; //下調(diào)溫度
if (TMSetV = -55) //限制溫度上下限
TMSetV = -55;
if (TMSetV >= 125)
TMSetV = 125;
}
if ((!KeyTF) (IntNum3 == 0)) KeyTF = 1; //當(dāng)鍵盤處于可用時(shí),鎖定
}
if (KeySETDown) //在2秒內(nèi)按下了SET則計(jì)中斷發(fā)生次數(shù)用于長(zhǎng)按SET時(shí)計(jì)時(shí)用
IntNum = IntNum + 1;
if (IntNum > 55) //中斷發(fā)生了55次時(shí)(大約1.2秒)75為1.5秒左右
{
IntNum = 0;
KeySETDown = 0;
if (KSDNum == 55) //如一直長(zhǎng)按了SET1.2秒左右
{
RelayOutPort = 1; //關(guān)閉繼電器輸出
PowTF = 1; //電源標(biāo)識(shí)關(guān)
LEDOneC = 0;
LEDTwoC = 0;
LEDThreeC = 0;
LEDPort = 0xBF; //顯示--
Delay_4s(); //延時(shí)
LEDOneC = 1;
LEDTwoC = 1; //關(guān)顯示
LEDThreeC = 1;
Delay_4s();
IntNum = 0;
IntNum2 = 0;
IntNum3 = 0;
}
KSDNum = 0;
}
}
KeyV = 0;
TempKeyV = 0; //清空變量準(zhǔn)備下次鍵掃描
if (!PowTF)
{
InitDS1820(); //初始化
ROMDS1820(); //跳過ROM
TMVDS1820(); //溫度轉(zhuǎn)換指令
Delay_510();
Delay_510(); //延時(shí)等待轉(zhuǎn)換完成
InitDS1820(); //初始化
ROMDS1820(); //跳過ROM
TMRDS1820(); //讀出溫度指令
ReadDS1820(); //讀出溫度值
V2ToV(); //轉(zhuǎn)換顯示值
if (TMV > TMSetV) //根據(jù)采集到的溫度值控制繼電器
{
RelayOutPort = 0;
}
else
{
RelayOutPort = 1;
}
評(píng)論