




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/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,temp2,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 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(DS=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+; 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 tmpwritebyte(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; /write 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); tmpwritebyte(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); /读高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); tmpwritebyte(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=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);/ 第一行的第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)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;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;elseSBUF=0x08;while(TI!=1);TI=0;SM2=1;ES=1;reti:RI=0; SM2=1;ES=1
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论