专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
51单片机
小弟 学习18b20 不会读数
2020-02-04 09:10
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
51单片机
18270
17
17
小弟 想用12864显示18b20温度 可是对 18b20读数不了解 在网上找的程序 没有解释 看不太懂 求 哪位前辈 传个以前做过的程序 或指点一下思路 小弟 万分感谢
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
17条回答
INT0
2020-02-06 15:49
贴一个
/*DS18B20 正常显示 by whj 11 11 18*/
#include<reg52.h>
//#include<intrins.h>
//#include<absacc.h>
//#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
sbit DS = P2^0; //DS18B20的数据输入/输出脚DQ,根据情况设定
uchar flag1; //正负标志
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
uchar code wei[]={0x00,0x01,0x02,0x04,0x08};
uint temp,t;
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void display(uint temp)
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
t=400;
P1= wei[1];;
if (flag1==1)
{
P0=0x40;
}
else
{
P0=0x00;
}
while(t--);
t=400;
P1= wei[2];
P0=table[A1];
while(t--);
t=400;
P1= wei[3];
P0=table1[A2];
while(t--);
t=400;
P1= wei[4];
P0=table[A3];
while(t--);
P1=wei[0];
}
void main()
{
uchar a;
do
{
tmpchange();
for(a=10;a>0;a--)
{
display(tmp());
}
} while(1);
}
加载中...
查看其它17个回答
一周热门
更多
>
相关问题
【东软载波ESF0654 PDS开发板活动】开箱
1 个回答
东软载波ESF0654 PDS开发板外部中断
1 个回答
东软载波ESF0654 PDS开发板高级控制定时器AD16C4T
1 个回答
用串口调试助手为什么只能在hex模式接收发送而在文本模式不行
9 个回答
触摸芯片SC02B/SC04B在地砖灯的设计方案
1 个回答
相关文章
51单片机与蓝牙模块连接
0个评论
51单片机的硬件结构
0个评论
基于51单片机的无线遥控器制作
0个评论
51单片机 AD转换
0个评论
51单片机数码管递增显示
0个评论
如何实现对单片机寄存器的访问
0个评论
基于51单片机的指纹密码锁
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
51单片机
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
/*DS18B20 正常显示 by whj 11 11 18*/
#include<reg52.h>
//#include<intrins.h>
//#include<absacc.h>
//#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
sbit DS = P2^0; //DS18B20的数据输入/输出脚DQ,根据情况设定
uchar flag1; //正负标志
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
uchar code wei[]={0x00,0x01,0x02,0x04,0x08};
uint temp,t;
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void display(uint temp)
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
t=400;
P1= wei[1];;
if (flag1==1)
{
P0=0x40;
}
else
{
P0=0x00;
}
while(t--);
t=400;
P1= wei[2];
P0=table[A1];
while(t--);
t=400;
P1= wei[3];
P0=table1[A2];
while(t--);
t=400;
P1= wei[4];
P0=table[A3];
while(t--);
P1=wei[0];
}
void main()
{
uchar a;
do
{
tmpchange();
for(a=10;a>0;a--)
{
display(tmp());
}
} while(1);
}
一周热门 更多>