基于51单片机

#include

#define uchar unsigned char

#define uint unsigned int

sbit ds=P2^2; //温度传感器信号线

sbit sda=P1^3;

sbit scl=P1^4;

uint temp;

float f_temp;

uchar code duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

uchar code wei[]={0xf7,0xfb,0xfd,0xfe}; //不带小数点的编码

void delay(){;;} //等待写入_短延时

void delms(uint z) //延时函数

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

void dsreset(void) //DS18B20复位,初始化函数

{

uint i;

ds=0;

i=103;

while(i>0)i--;

ds=1;

i=4;

while(i>0)i--;

}

bit tempreadbit(void) //读1位函数

{

uint i;

bit dat;

ds=0;i++; //i++ 起延时作用

ds=1;i++;i++;

dat=ds;

i=8;while(i>0)i--;

return (dat);

}

uchar tempread(void) //读1个字节

{

uchar i,j,dat;

dat=0;

for(i=1;i<=8;i++)

{

j=tempreadbit();

dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里

}

return(dat);

}

void tempwritebyte(uchar dat) //向18B20写一个字节数据

{

uint i;

uchar j;

bit testb;

for(j=1;j<=8;j++)

{

testb=dat&0x01;

dat=dat>>1;

if(testb) //写 1

{

ds=0;

i++;i++;

ds=1;

i=8;while(i>0)i--;

}

else

{

ds=0; //写 0

i=8;while(i>0)i--;

ds=1;

i++;i++;

}

}

}

void tempchange(void) //DS18B20 开始获取温度并转换

{

dsreset();

delms(1);

tempwritebyte(0xcc); // 写跳过读ROM指令

tempwritebyte(0x44); // 写温度转换指令

}

uint get_temp() //读取寄存器中存储的温度数据

{

uchar a,b;

dsreset();

delms(1);

tempwritebyte(0xcc);

tempwritebyte(0xbe);

a=tempread(); //读低8位

b=tempread(); //读高8位

temp=b;

temp<<=8; //两个字节组合为1个字

temp=temp|a;

f_temp=temp*0.0625; //温度在寄存器中为12位 分辨率位0.0625°

temp=f_temp*10+0.5; //乘以10表示小数点后面只取1位,加0.5是四舍五入

f_temp=f_temp+0.05;

return temp; //temp是整型

}

显示程序//

void wbyte_164(uchar date)

{

uchar i,temp;

temp=date;

for(i=0;i<8;i++)

{

temp=temp<<1;

scl=0;

delay();

sda=CY;

delay();

scl=1;

delay();

}

scl=0;

delay();

sda=1;

delay();

}

void display(uint t)

{

wbyte_164(wei[3]);

wbyte_164(duan[t/100]);

delms(6);

wbyte_164(wei[2]);

wbyte_164(duan[t%100/10]);

delms(6);

wbyte_164(wei[1]);

wbyte_164(duan[t%100%10]);

delms(6);

wbyte_164(wei[0]);

wbyte_164(0xff);

delms(6);

}

//

void main()

{

uchar i;

while(1)

{

tempchange();

for(i=10;i>0;i--)

{

display(get_temp());}

for(i=10;i>0;i

c语言b20等于多少,C语言程序:温度DS19B20显示相关推荐

  1. c语言竖等于意思,C语言竖式问题

    #include #include int main(){ char str[30];//键盘输入数组 scanf("%s",str); int i,j; char sta[50] ...

  2. 背口诀14天精通c语言pdf下载,C语言必背18个经典程序

    C语言必背18个经典程序 1./*输出9*9口诀.共9行9列,i控制行,j控制列.*/ #include "stdio.h" main() {int i,j,result; for ...

  3. C语言用字符串sex储存,C语言必须要记住的经典程序

    原标题:C语言必须要记住的经典程序 1./*判断101-200之间有多少个素数,并输出所有素数及素数的个数. 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此 ...

  4. C语言编写的判断素数的程序

    下面是一个用C语言编写的判断素数的程序: 复制#include <stdio.h>int main() {int n;printf("请输入一个数字:");scanf( ...

  5. C语言程序设计:one two=3,程序设计基础(C语言)

    <程序设计基础(C语言)>由会员分享,可在线阅读,更多相关<程序设计基础(C语言)(102页珍藏版)>请在装配图网上搜索. 1.程序设计基础( C语言) 东北大学高级语言程序设 ...

  6. 宏程序c语言哪个复杂,什么是宏程序?

    什么是宏程序,宏程序在数控编程中的作用: 数控编程是数控加工准备阶段的主要内容,通常包括分析零件图样,确定加工工艺过程:计算走刀轨迹,得出刀位数据:编写数控加工程序:制作控制介质:校对程序及首件试切. ...

  7. python语言程序设计嵩天-Python语言程序设计基础(第2版)嵩天课后答案

    嵩天.礼欣.黄天羽Python语言程序设计基础(第2版)习题答案本书提出了以理解和运用计算生态为目标的Python语言教学思想,在系统讲解Python语言语法的同时介绍了从数据理解到图像处理的14个P ...

  8. python语言程序设计基础网课-Python语言程序设计基础答案

    [填空题]逻辑型数据在参与算术运算的过程中可以被当作整数进行操作,通常True的值对应整数1,False的值对应整数____________. [单选题]在Python中,不可以用来表示字符串的符号是 ...

  9. c语言吗 程序语言,编程语言为什么从c语言开始,那有没有a语言b语言呢?

    实际上没有A语言叫法. B语言 B语言是贝尔实验室的计算机先驱Ken Thompson 和 Dennis Ritchied与1969年设计开发.他们的主要其他功绩还有Unix操作系统和C语言.可以说C ...

最新文章

  1. CVPR2020:点云三维目标跟踪的点对盒网络(P2B)
  2. Web漏洞扫描(三:Burp Suite的基本操作)
  3. 不再颓废,重新开始,牛客第一题1016. 部分A+B (15)
  4. MySQL / 自带的四个数据库介绍
  5. exception:Deleted row information cannot be accessed through the row处理
  6. aop面向切面编程到底是干嘛的?
  7. T-sql检测文件夹是否存在
  8. 为什么微型芯片是机器学习的命门?
  9. 不是纸上谈兵,VR原来真的可以缓解抑郁症
  10. SharePoint:扩展DVWP - 第34部分:使用图标形式的表单操作链接
  11. 包容普通的父亲和母亲
  12. Android Studio生成.jks文件
  13. 日语语法准备一:日语词性的分类
  14. CentOS的DNS服务器配置文件/etc/resolv.conf重置问题
  15. 哪些东西不宜和羊肉一起吃啊/吃香蕉对胃有坏处吗
  16. (三)编辑序列帧动画
  17. 魔兽世界8.0服务器不稳定老掉线,Win7系统玩魔兽世界老是掉线怎么办?
  18. 2021 年 3 月程序员工资统计新出炉
  19. 中文乱码解决文章摘录
  20. 游戏《纪念碑谷》介绍

热门文章

  1. JZOJ 6290. 倾斜的线
  2. php课程 10-35 php实现文件上传的注意事项是什么
  3. iOS tableView刷新
  4. TensorFlow | ReluGrad input is not finite. Tensor had NaN values
  5. 寫程式不需要天份,也不需要熱情
  6. haproxy +keepalived 原创
  7. jsp数据交互(一),九大内置对象
  8. The last time...
  9. sudo -u hdfs hdfs balancer出现异常 No lease on /system/balancer.id
  10. MySQL命令窗口出现中文乱码的解决方法