单片机温度控制系统Word下载.docx
《单片机温度控制系统Word下载.docx》由会员分享,可在线阅读,更多相关《单片机温度控制系统Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
voidConfig18b20(void);
//重新配置报警限定值和分辨率
voidReadID(void);
//读取器件
voidTemperatuerResult(void);
//18B20ID全处理
//*************变量及I/O口定义******************
unsignedchardigit[5];
unsignedcharkey_number,j,i,k,t;
//flag做键标记
unsignedinttmr,flag1,flag[8];
//fflag1数码管位标记
unsignedlongwait_cnter;
sbitcs=P0^3;
//csatP0.3
sbitclk=P0^2;
//clk连接于P0.2
sbitdat=P0^0;
//dat连接于P0.0
sbitkey=P0^1;
//key连接于P0.1
sbitzdz=P1^0;
sbitdq=P1^7;
bitflag2;
unsignedintTemperature;
unsignedintzhidz;
unsignedintTP[5];
unsignedchartemp_buff[9];
//存储读取的字节,readscratchpad为9字节,readromID为8字节
unsignedcharid_buff[8];
unsignedchar*p;
unsignedcharcrc_data;
unsignedcharcodeCrcTable[256]={0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,140,210,48,110,237,179,81,15,78,16,242,172,47,113,147,205,17,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53};
//****************HD7279A指令************
#defineCMD_RESET0xa4
#defineCMD_TEST0xbf
#defineDECODE00x80
#defineDECODE10xc8
#defineCMD_READ0x15
#defineUNDECODE0x90
#defineRTL_CYCLE0xa3
#defineRTR_CYCLE0xa2
#defineRTL_UNCYL0xa1
#defineRTR_UNCYL0xa0
#defineACTCTL0x98
#defineSEGON0xe0
#defineSEGOFF0xc0
#defineBLINKCTL0x88
//****************按键宏定义************
#definekey10x3a
#definekey20x39
#definekey30x38
#definekey40x32
#definekey50x31
#definekey60x30
#definekey70x2a
#definekey80x29
#definekey90x28
#definekey00x21
#definekey_left0x37
#definekey_right0x2f
#definekey_up0x3f
#definekey_down0x27
#definekey_esc0x22
#definekey_enter0x20
//***************主程序*****************
main()
{
for(tmr=0;
tmr<
0x2000;
tmr++);
//上电延时
send_byte(CMD_RESET);
//复位HD7279A
cs=1;
flag1=7;
write7279(DECODE0+7,0x0a);
write7279(DECODE0+6,0x0a);
while
(1)
zhidingzhi();
for(t=0;
t<
5;
t++)
{
TemperatuerResult();
TP[t]=Temperature;
}
Temperature=(TP[0]+TP[1]+TP[2]+TP[3]+TP[4])/5;
write7279(DECODE1,Temperature%10);
write7279(DECODE1+1,0x80+(Temperature%100)/10);
write7279(DECODE1+2,Temperature/100);
zhidz=flag[7]*100+flag[6]*10;
if(zhidz>
=Temperature){zdz=1;
}
else{zdz=0;
write7279(BLINKCTL,0xff);
//清除闪烁设置
delay10ms(10);
voidwrite7279(unsignedcharcmd,unsignedchardta)
{send_byte(cmd);
send_byte(dta);
unsignedcharread7279(unsignedcharcommand)
{send_byte(command);
return(receive_byte());
voidsend_byte(unsignedcharout_byte)//发送一个命令字
{unsignedchari;
clk=0;
cs=0;
short_delay();
for(i=0;
i<
8;
i++)
{clk=0;
if(out_byte&
0x80){dat=1;
else{dat=0;
clk=1;
out_byte=out_byte*2;
dat=1;
unsignedcharreceive_byte(void)//接收一个命令字
{
unsignedchari,in_byte;
//设为输入状态
for(i=0;
in_byte=in_byte*2;
if(dat==1)
{in_byte=in_byte|0x01;
//cs=1;
return(in_byte);
voidlong_delay(void)//长延时
0x30;
i++);
voidshort_delay(void)//短延时
//*********************n*10ms**********************
voiddelay10ms(unsignedchartime)
{unsignedchari;
unsignedintj;
for(i=0;
time;
{for(j=0;
j<
0x390;
j++);
//{if(!
key)
//{key_int();
voidzhidingzhi(void)
do
if(!
key)//如果有键按下
key_number=read7279(CMD_READ);
//读出键码
tmr=key_number;
//*******按键转换****************
if(key_number==key1)
{write7279(DECODE1+flag1,1);
flag[flag1]=1;
}//按key1显示1
elseif(key_number==key2)
{//send_byte(CMD_RESET);
//清除显示
//cs=1;
write7279(DECODE1+flag1,2);
flag[flag1]=2;
}//按key2显示2
elseif(key_number==key3)
write7279(DECODE1+flag1,3);
flag[flag1]=3;
}//按key3显示3
elseif(key_number==key4)
write7279(DECODE1+flag1,4);
flag[flag1]=4;
}//按key4显示4
elseif(key_number==key5)
write7279(DECODE1+flag1,5);
flag[flag1]=5;
}//按key5显示5
elseif(key_number==key6)
//cs=1;
//按key6显示6
write7279(DECODE1+flag1,6);
flag[flag1]=6;
elseif(key_number==key7)
write7279(DECODE1+flag1,7);
flag[flag1]=7;
}//按key7显示7
elseif(key_number==key8)
write7279(DECODE1+flag1,8);
flag[flag1]=8;
}//按key8显示8
elseif(key_number==key9)
{//send_byte(CMD_RESET);
write7279(DECODE1+flag1,9);
flag[flag1]=9;
}//按key9显示9
elseif(key_number==key0)
write7279(DECODE1+flag1,0);
flag[flag1]=0;
}//按键0
else
{send_byte(CMD_RESET);
cs=1;
write7279(DECODE0+flag1,0X0A);
while(!
key);
//等待按键放开
wait_cnter=0;
flag1--;
wait_cnter++;
key=0;
while(key_number!
=0&
&
wait_cnter<
0x30000);
//如果按键为0和超时则进入下一步演示
/************************************************************
*Function:
延时处理
*parameter:
*Return:
*Modify:
*************************************************************/
voidTempDelay(unsignedcharus)
while(us--);
/************************************************************
18B20初始化
voidInit18b20(void)
unsignedintdq;
dq=1;
dq=0;
TempDelay(86);
//delay530uS//80
_nop_();
TempDelay(14);
//delay100uS//14
if(dq==0)
flag2=1;
//detect1820success!
flag2=0;
//detect1820fail!
TempDelay(20);
//20
dq=1;
向18B20写入一个字节
voidWriteByte(unsignedcharwr)//单字节写入
unsignedchari;
dq=0;
dq=wr&
0x01;
TempDelay(5);
//delay45uS//5
wr>
>
=1;
读18B20的一个字节
unsignedcharReadByte(void)//读取单字节
unsignedchari,u=0;
for(i=0;
u>
if(dq==1)
u|=0x80;
TempDelay(4);
return(u);
读18B20
*************************************************************/
voidread_bytes(unsignedcharj)
j;
{*p=ReadByte();
p++;
*Function:
CRC校验
*parameter:
*Return:
*Modify:
unsignedcharCRC(unsignedcharj)
unsignedchari,crc_data=0;
i++)crc_data=CrcTable[crc_data^temp_buff[i]];
return(crc_data);
读取温度
voidGemTemp(void)
read_bytes(9);
if(CRC(9)==0)//校验正确
Temperature=temp_buff[1]*0x100+temp_buff[0];
Temperature*=6.25;
Temperature/=16;
Temperature+=100;
TempDelay
(1);
内部配置
voidConfig18b20(void)//重新配置报警限定值和分辨率
Init18b20();
WriteByte(0xcc);
//skiprom
WriteByte(0x4e);
//writescratchpad
WriteByte(0x19);
//上限
WriteByte(0x1a);
//下限
WriteByte(0x7f);
//set11bit(0.125)
WriteByte(0x48);
//保存设定值
WriteByte(0xb8);
//回调设定值
读18B20ID
voidReadID(void)//读取器件id
WriteByte(0x33);
//readrom
read_bytes(8);
18B20ID全处理
voidTemperatu