单片机课程设计报告-基于单片机的数字温度计设计内容摘要:

DS18B20 读 字节 /* 读取一位数据 */ bit RdBit() { uchar i。 bit b。 DQ = 0。 //读开始 1us i++。 DQ = 1。 //产生读时间隙 15us i++。 i++。 b = DQ。 //读位 i = 8。 while(i0) i。 //等待 60us DQ = 1。 //释放总线 return b。 } /* 读取 字节数据 */ uchar RdByte() { uchar i,j,d。 d=0。 for(i=0。 i8。 i++) //各位由低向高读出 DS18B20 { j=RdBit()。 d=(j7)|(d1)。 } return d。 } DS18B20 写字节 /* 写入字节数据 */ void WrByte(uchar d) { uint i。 uchar j。 bit btmp。 for(j=0。 j8。 j++) //各位由低向高写入 DS18B20 { btmp=damp。 0x01。 d=d1。 if(btmp) //写 1 { DQ=0。 //延时 15us i++。 i++。 DQ=1。 //写 1 时隙不低于 60u i=8。 while(i0) i。 } else //写 0 { DQ=0。 i=8。 while(i0) i。 //保持低电平 60us 到 120us DQ=1。 //释放总线 i++。 i++。 } } } 启动温度测量 /* 启动温度测量 */ void Convert() { uint i。 Delay(125)。 //延时 1ms TxReset()。 //初始化 DS18B20 RxWait()。 //等待 DS18B20 答应 Delay(125)。 //延时 WrByte(0xcc)。 //跳过 ROM 命令 WrByte(0x44)。 //温度转换命令 for(i=0。 ii++) //延时 1s Display()。 } 读取测量结果 /* 读取温度值 */ void RdTemp(void) { short int temp。 uint x。 bit flag = 1。 TxReset()。 //初始化 DS18B20 RxWait()。 //等待 DS18B20 应答 Delay(125)。 //延时 WrByte(0xcc)。 //跳过 ROM 命令 WrByte(0xbe)。 //读暂存存储器命令 templow = RdByte()。 //温度值低字节,低 4 位为小数 temphigh = RdByte()。 //温度值高字节 temp = (temphigh 8) | templow。 //温度为 16 位补码 if(temp0) //负温度 { flag = 0。 temp = ~temp + 1。 } tempzheng = temp 4。 //温度值整数 x = temp amp。 0x000f。 tempxiao = (x*10)/16。 //温度值小数 if(flag) //正温度 disp[0] = tempzheng/100。 //百位 else //负温度 disp[0] = 16。 //负号 disp[1] = (tempzheng%100)/10。 //十位 disp[2] = tempzheng%10。 //个位 disp[3] = tempxiao。 //小数 } 各算法流程图 图 图 发 DS18B20 复位命令 发跳过 ROM 命令 发温度转换开始命令 结束 图 温度转换流程图 图 计算温度流程图 图 显示数据刷新流程 图 开始 温度零下 ? 温度值取补码置“ — ”标志 计算小数位温度 BCD 值。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。