单片机多机多点温控系统从机模块.docx
《单片机多机多点温控系统从机模块.docx》由会员分享,可在线阅读,更多相关《单片机多机多点温控系统从机模块.docx(19页珍藏版)》请在冰豆网上搜索。
单片机多机多点温控系统从机模块
//DS从机程序
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
#defineSLAVE0x01
#defineBN6
sbitLCD_RS=P2^5;/*定义LCD控制端口*/
sbitLCD_RW=P2^6;
sbitLCD_EN=P2^7;
sbitDS1=P1^0;
sbitDS2=P1^1;
sbitkey3=P3^5;
sbittem=P3^3;
sbitwin=P3^4;
sbitkey1=P3^6;
sbitkey2=P3^7;
uinttemp1,temp2,tempH,tempL;//variableoftemperature
ucharflag1,aa,we;
ucharA1,A2,A3,A4,B1,B2,B3,B4;//signoftheresultpositiveornegative
uchardis1[16]={76,45,84,'0','0','.','0',32,32,72,45,84,'0','0','.','0'};
uchardis2[16]={48,49,32,'0','0','.','0',32,32,48,50,32,'0','0','.','0'};
ucharcodetab[]={'0','1','2','3','4','5','6','7','8','9'};
uchartrbuf[6];
bittready;
bitrready;
voidstr(void);
voidsre(void);
voiddelay(intms)
{
inti;
while(ms--)
{
for(i=0;i<250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/*******************************************************************/
/**/
/*检查LCD忙状态*/
/*lcd_busy为1时,忙,等待。
lcd-busy为0时,闲,可写指令与数据。
*/
/**/
/*******************************************************************/
bitlcd_busy()
{
bitresult;
LCD_RS=0;
LCD_RW=1;
LCD_EN=1;
_nop_();
_nop_();
_nop_();
_nop_();
result=(bit)(P0&0x80);
LCD_EN=0;
returnresult;
}
/*******************************************************************/
/**/
/*写指令数据到LCD*/
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。
*/
/**/
/*******************************************************************/
voidlcd_wcmd(ucharcmd)
{
while(lcd_busy());
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
_nop_();
_nop_();
P0=cmd;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN=1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN=0;
}
/*******************************************************************/
/**/
/*写显示数据到LCD*/
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。
*/
/**/
/*******************************************************************/
voidlcd_wdat(uchardat)
{
while(lcd_busy());
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN=1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN=0;
}
/*******************************************************************/
/**/
/*设定显示位置*/
/**/
/*******************************************************************/
voidlcd_pos(ucharpos)
{
lcd_wcmd(pos|0x80);//数据指针=80+地址变量
}
/*******************************************************************/
/**/
/*LCD初始化设定*/
/**/
/*******************************************************************/
voidlcd_init()
{
lcd_wcmd(0x38);//16*2显示,5*7点阵,8位数据
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x0c);//显示开,关光标
delay(5);
lcd_wcmd(0x06);//移动光标
delay(5);
lcd_wcmd(0x01);//清除LCD的显示内容
delay(5);
lcd_wcmd(0x06);//向右移动光标
delay(5);
}
/*******************************************************************/
/**/
/*闪动子程序*/
/**/
/*******************************************************************/
voidflash()
{
delay(600);//控制停留时间
lcd_wcmd(0x08);//关闭显示
delay(200);//延时
lcd_wcmd(0x0c);//开显示
delay(200);//延时
lcd_wcmd(0x08);//关闭显示
delay(200);//延时
lcd_wcmd(0x0c);//开显示
delay(200);
}
/*voiddelay(uintcount)//delay
{
uinti;
while(count)
{
i=100;
while(i>0)
i--;
count--;
}
}*/
///////功能:
串口初始化,波特率9600,方式1///////
voidInit(void)
{
TMOD=0x20;
TL1=0xfd;
TH1=0xfd;
PCON=0x00;
TR1=1;
SCON=0xf0;
ES=1;
EA=1;
}
voiddsreset(ucharDS)//sendresetandinitializationcommand18B20复位,初始化函数
{
uinti;
if(DS==1)
{
DS1=0;
i=103;
while(i>0)i--;
DS1=1;
i=4;
while(i>0)i--;
}
if(DS==2)
{
DS2=0;
i=103;
while(i>0)i--;
DS2=1;
i=4;
while(i>0)i--;
}
}
bittmpreadbit(ucharDS)//readabit读DS21位数据函数
{
uinti;
bitdat;
if(DS==1)
{
DS1=0;i++;//i++fordelay
DS1=1;i++;i++;
dat=DS1;
i=8;while(i>0)i--;
}
if(DS==2)
{
DS2=0;i++;//i++fordelay
DS2=1;i++;i++;
dat=DS2;
i=8;while(i>0)i--;
}
return(dat);
}
uchartmpread(ucharDS)//readabytedate读1字节函数
{
uchari,j,dat;
dat=0;
if(DS==1)
{
for(i=1;i<=8;i++)
{
j=tmpreadbit
(1);
dat=(j<<7)|(dat>>1);//读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
}
if(DS==2)
{
for(i=1;i<=8;i++)
{
j=tmpreadbit
(2);
dat=(j<<7)|(dat>>1);//读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
}
return(dat);
}
voidtmpwritebyte(uchardat,ucharDS)//writeabytetods18b20向1820写一个字节数据函数
{
uinti;
ucharj;
bittestb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(DS==1)
{
if(testb)//write1
{
DS1=0;
i++;i++;
DS1=1;
i=8;while(i>0)i--;
}
else
{
DS1=0;//write0
i=8;while(i>0)i--;
DS1=1;
i++;i++;
}
}
if(DS==2)
{
if(testb)//write1
{
DS2=0;
i++;i++;
DS2=1;
i=8;while(i>0)i--;
}
else
{
DS2=0;//write0
i=8;while(i>0)i--;
DS2=1;
i++;i++;
}
}
}
}
voidtmpchange(ucharDS)//DS18B20beginchange开始获取数据并转换
{
if(DS==1)
{
dsreset
(1);
delay
(1);
tmpwritebyte(0xcc,1);//addressalldriversonbus写跳过读ROM指令
tmpwritebyte(0x44,1);
}//initiatesasingletemperatureconversion写温度转换指令
if(DS==2)
{
dsreset
(2);
delay
(1);
tmpwritebyte(0xcc,2);//addressalldriversonbus写跳过读ROM指令
tmpwritebyte(0x44,2);
}//initiatesasingletemperatureconversion写温度转换指令
}
uinttmp1(void)//getthetemperature读取寄存器中存储的温度数据
{
floattt;
uchara,b;
dsreset
(1);
delay
(1);
tmpwritebyte(0xcc,1);
tmpwritebyte(0xbe,1);
a=tmpread
(1);//读低8位
b=tmpread
(1);//读高8位
temp1=b;
temp1<<=8;//twobytecomposeaintvariable两个字节组合为1个字
temp1=temp1|a;
tt=temp1*0.0625;//温度在寄存器中是12位,分辨率是0.0625
temp1=tt*10+0.5;//乘10表示小数点后只取1位,加0.5是四折五入
temp1=temp1;//误差补偿
returntemp1;
}
uinttmp2(void)//getthetemperature读取寄存器中存储的温度数据
{
floattt;
uchara,b;
dsreset
(2);
delay
(1);
tmpwritebyte(0xcc,2);
tmpwritebyte(0xbe,2);
a=tmpread
(2);//读低8位
b=tmpread
(2);//读高8位
temp2=b;
temp2<<=8;//twobytecomposeaintvariable两个字节组合为1个字
temp2=temp2|a;
tt=temp2*0.0625;//温度在寄存器中是12位,分辨率是0.0625
temp2=tt*10+0.5;//乘10表示小数点后只取1位,加0.5是四折五入
temp2=temp2+5;//误差补偿
returntemp2;
}
voiddelay10ms()//delay
{
uchara,b;
for(a=10;a>0;a--)
for(b=60;b>0;b--);
}
uchardisplay(uinttemp1,uinttemp2)//显示程序
{
uchari;
A1=temp1/100;
A2=temp1%100/10;
A3=temp1%100%10;
B1=temp2/100;
B2=temp2%100/10;
B3=temp2%100%10;
trbuf[0]=A1;
trbuf[1]=A2;
trbuf[2]=A3;
trbuf[3]=B1;
trbuf[4]=B2;
trbuf[5]=B3;
dis1[3]=tab[tempL*10/100];
dis1[4]=tab[tempL*10%100/10];
dis1[6]=tab[tempL*10%100%10];
dis1[12]=tab[tempH*10/100];
dis1[13]=tab[tempH*10%100/10];
dis1[15]=tab[tempH*10%100%10];
dis2[3]=tab[A1];
dis2[4]=tab[A2];
dis2[6]=tab[A3];
dis2[12]=tab[B1];
dis2[13]=tab[B2];
dis2[15]=tab[B3];
delay
(1);
lcd_pos(0);//设置显示位置为
delay
(2);//第一行的第1个字符
for(i=0;i<16;i++)
{
lcd_wdat(dis1[i]);
}
delay
(2);
lcd_pos(0x40);
delay
(2);
for(i=0;i<16;i++)
{
lcd_wdat(dis2[i]);
}
return(A1,A2,A3,B1,B2,B3);
}
voidTemC()
{
if(temp1/10<=tempL||temp2/10<=tempL)tem=0;
elsetem=1;
if(temp1/10>=tempH||temp2/10>=tempH)win=0;
elsewin=1;
}
ucharchan()
{
if(key1==0)
{
tempL++;
while(key1==0);
if(tempL>35)
tempL=0;
}
if(key2==0)
{
tempH++;
while(key2==0);
if(tempH>35)
tempH=0;
}
return(tempH,tempL);
}
/******************************************************************************/
voidmain()
{//uchara;
//uchari;
delay(10);
lcd_init();
Init();
tempH=35;
tempL=15;
while
(1)
{
tmpchange
(1);
tmpchange
(2);
tmp1();
tmp2();
chan();
TemC();
display(temp1,temp2);
tready=1;
rready=1;
}
}
voidssio(void)interrupt4
{
uchara;
RI=0;
ES=0;
if(SBUF!
=SLAVE)
{
ES=1;
gotoreti;
}
SM2=0;
SBUF=SLAVE;
while(TI!
=1);
TI=0;
while(RI!
=1);
RI=0;
if(RB8==1)
{
SM2=1;
ES=1;
gotoreti;
}
a=SBUF;
if(a==0x02)
{
if(tready==1)
SBUF=0x02;
else
SBUF=0x00;
while(TI!
=1);
TI=0;
str();
gotoreti;
}
else
{
SBUF=0x08;
while(TI!
=1);
TI=0;
SM2=1;
ES=1;
}
reti:
RI=0;
SM2=1;
ES=1;
}
voidstr(void)
{
uchari;
tready=0;
while
(1)
{
tmpchange
(1);
tmpchange
(2);
tmp1();
tmp2();
chan();
TemC();
display(temp1,temp2);
for(i=0;i{
SBUF=trbuf[i];
while(TI!
=1);
TI=0;
}
while(RI!
=1);
RI=0;
if(RB8==1)gotoloop;
}
loop:
SM2=1;
ES=1;
}