《多级通信测温系统程序单片机.doc》由会员分享,可在线阅读,更多相关《多级通信测温系统程序单片机.doc(20页珍藏版)》请在三一办公上搜索。
1、附1:源程序代码一、主机#include#define uchar unsigned char#define uint unsigned intsbit beep=P35;unsigned char code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;uchar RecData;uchar tmp;uint wei;sbit H1=P36;sbit L1=P05;sbit L2=P06;sbit L3=P07;void UART_int() TMOD=0X21;
2、TH1=0XFD; TL1=0XFD; SCON=0XD0; TH0=(65535-45872)/256; TL0=(65536-45872)%256; EA=1; TR1=1; TR0=1; ET0=1; ES=1; EA=1; TI=0; void delay(uint num) while(num-); void key() H1=0; if(L1=0) delay(10); if(L1=0) RecData=b; wei=11; while(!L1); if(L2=0) delay(10); if(L2=0) RecData=c; wei=12; while(!L2); void di
3、splay(uint temp)/显示程序 uchar A1,A2,A2t,A3; A1=temp/100; A2t=temp%100; A2=A2t/10; A3=A2t%10; P1=tablewei; P0=0XFE; delay(50); P1=tableA1;/显示十位 P0=0XFD; delay(50); P1=tableA2|0x80;/显示个位 P0=0XFB; delay(50); P1=tableA3;/显示分位 P0=0XF7; delay(50); void warn(uint s) uchar i; i=s; beep=0; P2=0x00; while(i-) d
4、isplay(tmp); beep=1; P2=0xff; i=s; while(i-) display(tmp); void deal(uint t) if(t50&t300) warn(10); void putc_to(uchar c) SBUF=c; while(TI=0) TI=0; void main() P1M0=0xff; P1M1=0x00; UART_int(); while(1) key(); display(tmp);TB8=1; putc_to(RecData);deal(tmp); com_int(void) interrupt 4 if(RI) tmp=SBUF;
5、 RI=0; void intt0() interrupt 1 uchar miao,a,num; TH0=(65535-45872)/256; TL0=(65536-45872)%256; num+; if(num=20) num=0; miao+;if(miao=5) miao=0; a+; if(a=1) RecData=b; wei=11; if(a=2) RecData=c; wei=12; a=0; 二、 B从机#include #define uchar unsigned char#define uint unsigned intsbit DQ=P22;sbit beep=P23
6、;sbit led=P20;uchar RecData; unsigned char code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;void delay(uint num) while(num-);/*函数名称:void Init_DS18B20(void)返回值:无参数:无作用:初始化18B20*/ void Init_DS18B20(void) char x=0; DQ=1; delay(10);/稍作延时 DQ=0; delay(800);/延时480
7、us 540us DQ=1; /拉高总线 15-60us delay(50); x=DQ;/读总线状态 为0复位成功,为1则不成功 delay(30); DQ=1;/释放总线/*函数名称:uchar ReadOneChar(void)返回值:uchar dat参数: 无作用:读1820一个字节*/uchar ReadOneChar(void) uchar i; uchar dat=0; for(i=0;i=1; DQ=1;/给脉冲 if(DQ) dat|=0x80;/读1 / 读0右移处理 delay(20);/15us内读完一个数 return(dat); /*函数名称:void Write
8、OneChar(uchar dat)返回值:无参数: uchar dat作用:写1820一个字节*/*写DS18B20*/写0 60us读完,写1 30us 内读完void WriteOneChar(uchar dat) uint i; uchar j; bit testb; for(j=1;j1; if(testb) /write 1 DQ=0; i+;i+;i+; DQ=1; i=15;while(i0)i-; else DQ=0; /write 0 i=15;while(i0)i-; DQ=1; i+;i+;i+; /*函数名称:int ReadOneTemperature(void)返
9、回值:int t参数: 无作用:读温度值*/void tmpchange(void) /DS18B20 begin change Init_DS18B20(); delay(1); WriteOneChar(0xcc); / address all drivers on bus WriteOneChar(0x44); / initiates a single temperature conversionint ReadOneTemperature(void) /*读取温度值*/ 每次读写均要先复位 uint a,b,t; float tep; Init_DS18B20(); WriteOneC
10、har(0xcc);/发跳过ROM命令 WriteOneChar(0xbe);/读寄存器,共九字节,前两字节为转换值 a=ReadOneChar(); /a存低字节 b=ReadOneChar(); /b存高字节 t=b; t=8;/高字节转换为10进制 t=t|a; tep=t*0.0625;/转换精度为0.0625/LSB t=tep*10+0.5;/保留1位小数并四舍五入*后面除10还原正确温度值) return(t);void display(uint temp)/显示程序 uchar A1,A2,A2t,A3; A1=temp/100; A2t=temp%100; A2=A2t/10
11、; A3=A2t%10; P1=table11; P0=0XFE; delay(800); P1=tableA1;/显示十位 P0=0XFD; delay(800); P1=tableA2|0X80;/显示个位 P0=0XFB; delay(800); P1=tableA3;/显示分位 P0=0XF7; delay(1);void warn(uint s) uchar i; i=s; beep=0; led=0; while(i-) display(ReadOneTemperature(); beep=1; led=1; i=s; while(i-) display(ReadOneTemper
12、ature(); void deal(uint t) if(t50&t300) warn(10); void init_com(void) TMOD=0X20; PCON=0X00; SCON=0XF0; TH1=0XFD; TL1=0XFD; TR1=1; ES=1; EA=1; PS=1; void comm(uint a) SBUF=a; while(!TI); TI=0;void main() P1M1=0x00; P1M0=0xff; init_com(); while(1) tmpchange(); display(ReadOneTemperature(); deal(ReadOn
13、eTemperature(); com_int(void) interrupt 4 if(RI) RecData=SBUF; RI=0; if(RB8=1) if(RecData=b) SM2=0; tmpchange(); comm(ReadOneTemperature(); display(ReadOneTemperature(); deal(ReadOneTemperature(); SM2=1; else SM2=1; 三、 C从机#include #define uchar unsigned char#define uint unsigned intsbit DQ=P22;sbit
14、beep=P23;sbit led=P20;uchar RecData; unsigned char code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;void delay(uint num) while(num-);/*函数名称:void Init_DS18B20(void)返回值:无参数:无作用:初始化18B20*/ void Init_DS18B20(void) char x=0; DQ=1; delay(10);/稍作延时 DQ=0; delay(800
15、);/延时480us 540us DQ=1; /拉高总线 15-60us delay(50); x=DQ;/读总线状态 为0复位成功,为1则不成功 delay(30); DQ=1;/释放总线/*函数名称:uchar ReadOneChar(void)返回值:uchar dat参数: 无作用:读1820一个字节*/uchar ReadOneChar(void) uchar i; uchar dat=0; for(i=0;i=1; DQ=1;/给脉冲 if(DQ) dat|=0x80;/读1 / 读0右移处理 delay(20);/15us内读完一个数 return(dat); /*函数名称:vo
16、id WriteOneChar(uchar dat)返回值:无参数: uchar dat作用:写1820一个字节*/*写DS18B20*/写0 60us读完,写1 30us 内读完void WriteOneChar(uchar dat) uint i; uchar j; bit testb; for(j=1;j1; if(testb) /write 1 DQ=0; i+;i+;i+; DQ=1; i=15;while(i0)i-; else DQ=0; /write 0 i=15;while(i0)i-; DQ=1; i+;i+;i+; /*函数名称:int ReadOneTemperatur
17、e(void)返回值:int t参数: 无作用:读温度值*/void tmpchange(void) /DS18B20 begin change Init_DS18B20(); delay(1); WriteOneChar(0xcc); / address all drivers on bus WriteOneChar(0x44); / initiates a single temperature conversionint ReadOneTemperature(void) /*读取温度值*/ 每次读写均要先复位 uint a,b,t; float tep; Init_DS18B20(); W
18、riteOneChar(0xcc);/发跳过ROM命令 WriteOneChar(0xbe);/读寄存器,共九字节,前两字节为转换值 a=ReadOneChar(); /a存低字节 b=ReadOneChar(); /b存高字节 t=b; t=8;/高字节转换为10进制 t=t|a; tep=t*0.0625;/转换精度为0.0625/LSB t=tep*10+0.5;/保留1位小数并四舍五入*后面除10还原正确温度值) return(t);void display(uint temp)/显示程序 uchar A1,A2,A2t,A3; A1=temp/100; A2t=temp%100; A
19、2=A2t/10; A3=A2t%10; P1=table11; P0=0XFE; delay(800); P1=tableA1;/显示十位 P0=0XFD; delay(800); P1=tableA2|0X80;/显示个位 P0=0XFB; delay(800); P1=tableA3;/显示分位 P0=0XF7; delay(1);void warn(uint s) uchar i; i=s; beep=0; led=0; while(i-) display(ReadOneTemperature(); beep=1; led=1; i=s; while(i-) display(ReadO
20、neTemperature(); void deal(uint t) if(t50&t300) warn(10); void init_com(void) TMOD=0X20; PCON=0X00; SCON=0XF0; TH1=0XFD; TL1=0XFD; TR1=1; ES=1; EA=1; PS=1; void comm(uint a) SBUF=a; while(!TI); TI=0;void main() P1M1=0x00; P1M0=0xff; init_com(); while(1) tmpchange(); display(ReadOneTemperature(); deal(ReadOneTemperature(); com_int(void) interrupt 4 if(RI) RecData=SBUF; RI=0; if(RB8=1) if(RecData=c) SM2=0; tmpchange(); comm(ReadOneTemperature(); display(ReadOneTemperature(); deal(ReadOneTemperature(); SM2=1; else SM2=1;