作业帮 > 综合 > 作业

求一个ds18b20数字温度测量的C程序

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/08/04 11:36:03
求一个ds18b20数字温度测量的C程序
程序比较简单,电路图已上传.P0作为段选,P2位选,希望能得到完整的程序,包括:ds18b20初始化,读字节,写字节,读温度,温度处理,和温度的4位数码管显示,
求一个ds18b20数字温度测量的C程序
#include"reg51.h"
#include"intrins.h"
unsigned char templ=0;
unsigned char temph=0;
unsigned char tempdot=0;
bit fuhao_flag=0;
float temperature=0;
unsigned char t=0;
unsigned char tab[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
unsigned char ledbitpos=0;
sbit dq=P1^0;
sbit num_shiwei=P2^5;
sbit num_gewei=P2^4;
sbit fuhaowei=P2^3;
sbit shiwei=P2^2;
sbit gewei=P2^1;
sbit dot=P2^0;
void Delay5us(unsigned char n)
{
unsigned char i;
i=n;
do
{
_nop_();_nop_();_nop_();
i--;
}
while(i);
return;
}
void Delay5ms(unsigned char n)
{
unsigned char i;
i=n;
do
{
Delay5us(200);
Delay5us(200);
Delay5us(200);
Delay5us(200);
Delay5us(200);
i--;
}
while(i);
}
bit init_ds18b20(void)
{
bit bresult=0;
EA=0;
dq=1;
Delay5us(8);
dq=0;
Delay5us(90);
dq=1;
Delay5us(6);
bresult=dq;
Delay5us(20);
EA=1;
return bresult;
}
bit readbit()
{
bit b;
dq=0;
_nop_();
_nop_();
_nop_();
_nop_();
dq=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
b=dq;
Delay5us(4);
return b;
}
unsigned char readonechar(void)
{
unsigned char i=0;
unsigned char dat=0;
EA=0;
for(i=8;i>0;i--)
{
dat=_cror_(dat,1);
if(readbit())
dat|=0x80;
else
dat|=0x00;
}
EA=1;
return dat;
}
void writebit(bit dq1)
{
unsigned char j;
if(!dq1)
{
dq=0;
for(j=35;j>0;j--);
dq=1;
}
else
{
dq=0;
for(j=2;j>0;j--);
dq=1;
for(j=33;j>0;j--);
}
}
void writeonechar(unsigned char command)
{
unsigned char i;
EA=0;
for(i=0;i>4;
tempdot=(10*(templ & 0x0f))/16;
templ=templ>>4;
t=temph