1、单片机多机多点温控系统从机模块/DS从机程序#include #include #define uchar unsigned char#define uint unsigned int#define SLAVE 0x01#define BN 6sbit LCD_RS = P25; /*定义LCD控制端口*/ sbit LCD_RW = P26;sbit LCD_EN = P27;sbit DS1=P10;sbit DS2=P11;sbit key3=P35;sbit tem=P33;sbit win=P34;sbit key1=P36;sbit key2=P37;uint temp1,temp
2、2,tempH,tempL; / variable of temperatureuchar flag1,aa,we;uchar A1,A2,A3,A4,B1,B2,B3,B4; / sign of the result positive or negativeuchar dis116=76,45,84,0,0,.,0,32,32,72,45,84,0,0,.,0;uchar dis216=48,49,32,0,0,.,0,32,32,48,50,32,0,0,.,0;uchar code tab = 0,1,2,3,4,5,6,7,8,9;uchar trbuf6;bit tready;bit
3、 rready;void str(void);void sre(void); void delay(int ms) int i; while(ms-) for(i = 0; i0) i-; count-; */功能:串口初始化,波特率9600,方式1/void Init(void)TMOD=0x20; TL1=0xfd; TH1=0xfd; PCON=0x00; TR1=1; SCON=0xf0; ES=1; EA=1;void dsreset(uchar DS) /send reset and initialization command 18B20复位,初始化函数 uint i; if(D
4、S=1) DS1=0; i=103; while(i0)i-; DS1=1; i=4; while(i0)i-; if(DS=2) DS2=0; i=103; while(i0)i-; DS2=1; i=4; while(i0)i-; bit tmpreadbit(uchar DS) /read a bit 读DS2 1位数据函数 uint i; bit dat; if(DS=1) DS1=0;i+; /i+ for delay DS1=1;i+;i+; dat=DS1; i=8;while(i0)i-; if(DS=2) DS2=0;i+; /i+ for delay DS2=1;i+;i+
5、; dat=DS2; i=8;while(i0)i-; return (dat);uchar tmpread(uchar DS) /read a byte date 读1字节函数 uchar i,j,dat; dat=0; if(DS=1) for(i=1;i=8;i+) j=tmpreadbit(1); dat=(j1); /读出的数据最低位在最前面,这样刚好一个字节在DAT里 if(DS=2) for(i=1;i=8;i+) j=tmpreadbit(2); dat=(j1); /读出的数据最低位在最前面,这样刚好一个字节在DAT里 return(dat);void tmpwritebyt
6、e(uchar dat,uchar DS) /write a byte to ds18b20 向1820写一个字节数据函数 uint i; uchar j; bit testb; for(j=1;j1; if(DS=1) if(testb) /write 1 DS1=0; i+;i+; DS1=1; i=8;while(i0)i-; else DS1=0; /write 0 i=8;while(i0)i-; DS1=1; i+;i+; if(DS=2) if(testb) /write 1 DS2=0; i+;i+; DS2=1; i=8;while(i0)i-; else DS2=0; /w
7、rite 0 i=8;while(i0)i-; DS2=1; i+;i+; void tmpchange(uchar DS) /DS18B20 begin change 开始获取数据并转换 if(DS=1) dsreset(1); delay(1); tmpwritebyte(0xcc,1); / address all drivers on bus 写跳过读ROM指令 tmpwritebyte(0x44,1); / initiates a single temperature conversion 写温度转换指令 if(DS=2) dsreset(2); delay(1); tmpwrite
8、byte(0xcc,2); / address all drivers on bus 写跳过读ROM指令 tmpwritebyte(0x44,2); / initiates a single temperature conversion 写温度转换指令 uint tmp1(void) /get the temperature 读取寄存器中存储的温度数据 float tt; uchar a,b; dsreset(1); delay(1); tmpwritebyte(0xcc,1); tmpwritebyte(0xbe,1); a=tmpread(1); /读低8位 b=tmpread(1); /
9、读高8位 temp1=b; temp1=8; /two byte compose a int variable 两个字节组合为1个字 temp1=temp1|a; tt=temp1*0.0625; /温度在寄存器中是12位,分辨率是0.0625 temp1=tt*10+0.5; /乘10表示小数点后只取1位,加0.5是四折五入 temp1=temp1; /误差补偿 return temp1;uint tmp2( void) /get the temperature 读取寄存器中存储的温度数据 float tt; uchar a,b; dsreset(2); delay(1); tmpwrite
10、byte(0xcc,2); tmpwritebyte(0xbe,2); a=tmpread(2); /读低8位 b=tmpread(2); /读高8位 temp2=b; temp20;a-) for(b=60;b0;b-); uchar display(uint temp1,uint temp2) /显示程序 uchar i; A1=temp1/100; A2=temp1%100/10; A3=temp1%100%10; B1=temp2/100; B2=temp2%100/10; B3=temp2%100%10; trbuf0=A1; trbuf1=A2; trbuf2=A3; trbuf3
11、=B1; trbuf4=B2; trbuf5=B3; dis13=tabtempL*10/100; dis14=tabtempL*10%100/10; dis16=tabtempL*10%100%10; dis112=tabtempH*10/100; dis113=tabtempH*10%100/10; dis115=tabtempH*10%100%10; dis23=tabA1; dis24=tabA2; dis26=tabA3; dis212=tabB1; dis213=tabB2; dis215=tabB3; delay(1); lcd_pos(0); /设置显示位置为 delay(2)
12、;/ 第一行的第1个字符 for(i=0;i16;i+) lcd_wdat(dis1i); delay(2); lcd_pos(0x40); delay(2); for(i=0;i16;i+) lcd_wdat(dis2i); return (A1,A2,A3,B1,B2,B3); void TemC() if(temp1/10=tempL|temp2/10=tempH|temp2/10=tempH) win=0; else win=1; uchar chan() if(key1=0) tempL+; while(key1=0); if(tempL35) tempL=0; if(key2=0)
13、 tempH+; while(key2=0); if(tempH35) tempH=0; return (tempH,tempL); /*/void main() /uchar a; /uchar i; 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; void ssio(void)interrupt 4 uchar a;
14、RI=0; ES=0; if(SBUF!=SLAVE) ES=1; goto reti; SM2=0; SBUF=SLAVE; while(TI!=1); TI=0; while(RI!=1); RI=0; if(RB8=1) SM2=1; ES=1; goto reti; a=SBUF; if(a=0x02) if(tready=1) SBUF=0x02; else SBUF=0x00; while(TI!=1); TI=0; str(); goto reti; else SBUF=0x08; while(TI!=1); TI=0; SM2=1; ES=1; reti:RI=0; SM2=1; ES=1; void str(void) uchar i; tready=0; while(1) tmpchange(1); tmpchange(2); tmp1(); tmp2(); chan(); TemC(); display(temp1,temp2); for(i=0;iBN;i+) SBUF=trbufi; while(TI!=1); TI=0; while(RI!=1); RI=0; if(RB8=1)goto loop; loop:SM2=1; ES=1;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1