标题:
单片机+ds18b20温度显示不稳定,会闪烁
[打印本页]
作者:
zzh123123
时间:
2020-3-12 16:41
标题:
单片机+ds18b20温度显示不稳定,会闪烁
这是温度程序,正常运行没问题,但是插进别的程序就会闪烁而且不稳定。
#include"temp.h"
void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
uchar Ds18b20Init()
{
uchar i;
DSPORT = 0; //将总线拉低480us~960us
i = 70;
while(i--);//延时642us
DSPORT = 1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
i = 0;
while(DSPORT) //等待DS18B20拉低总线
{
Delay1ms(1);
i++;
if(i>5)//等待>5MS
{
return 0;//初始化失败
}
}
return 1;//初始化成功
}
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0;
i++;
DSPORT = dat & 0x01;
i=6;
while(i--);
DSPORT = 1;
dat >>= 1;
}
}
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=0;j<8;j++)
{
DSPORT = 0;
i++;
DSPORT = 1;
i++;
i++;
bi = DSPORT;
byte = (byte >> 1) | (bi << 7);
i = 4;
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳过ROM操作命令
Ds18b20WriteByte(0x44); //温度转换命令
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0xbe);
}
int Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp(); //先写入转换命令
Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
tml = Ds18b20ReadByte(); //读取温度值共16位,先读低字节
tmh = Ds18b20ReadByte(); //再读高字节
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
作者:
wulin
时间:
2020-3-12 17:39
ds18b20是单总线器件,读写一次数据耗时较长,按你这程序大于4ms。与其它程序共同运行容易互相干扰。所以执行多任务要学会统筹安排时间。
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1