ds18b20采集c语言,为何 我做的DS18B20 采集温度系统 反应那么慢!! 附上c语音源码 求大神分析一下...

本帖最后由 lamp波波 于 2016-8-7 13:08 编辑

#include

#define uchar unsigned char

#define uint unsigned int

sbit DS=P2^4;

sbit dot=P0^7;     //小数点

uint temp;             // variable of temperature

uchar flag1;            // sign of the result positive or negative

code unsigned char tab[]={0x40, 0x79, 0x24, 0x30,

0x19, 0x12, 0x02, 0x78,

0x00, 0x10};

uint  ad=0, ads;//数字信号变量

uchar StrTab[3];

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)

{

uint i;

ad = temp;

ads=ad/100;

StrTab[2]=tab[ads];

ad=ad%100;

ads=ad/10;

StrTab[1]=tab[ads];

ad=ad%10;

ads=ad;

StrTab[0]=tab[ads];

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

{

P2=0x01;

P0=StrTab[2];

dot=1;

delay(5);

P2=0x00;

P2=0x02;

P0=StrTab[1];

delay(5);

P2=0x00;

P2=0x04;

P0=StrTab[0];

dot=1;

delay(5);

P2=0x00;

}

}

void main()

{

uchar a;

while(1)

{

tmpchange();

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

{

display(tmp());

}

}

}

699ba7046c51816a17b33a7caa85f179.png

0

1470546013695.jpg

(950.81 KB, 下载次数: 1)

993d5be4b93fad92a5cc6b25a09e80d9.gif

1470546031088.jpg

(738.6 KB, 下载次数: 5)

993d5be4b93fad92a5cc6b25a09e80d9.gif

97b4b3417991aabde46fdac613e34292.png

已退回5积分

2016-8-7 13:05:04

d6e83965612547db3ff693951622eb97.png

评论

提交评论

2016-8-7 13:07:21

d6e83965612547db3ff693951622eb97.png

评论

提交评论

2016-8-7 13:07:53

d6e83965612547db3ff693951622eb97.png

评论

提交评论

2018-5-8 18:29:38

d6e83965612547db3ff693951622eb97.png

评论

提交评论

2018-5-8 20:14:51

d6e83965612547db3ff693951622eb97.png

评论

提交评论

撰写答案

你正在撰写答案

如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值