51 单片机复位一直不成功。
本帖最后由 Victorsixy 于 2014-7-7 17:42 编辑完全按照规格书时序写的,示波器测试都正常,就是读取温度值不对。只能求助大神了,这个太蛋疼了。以下的延时函数均用示波器测试过。
#include"DS18B20.h"
#include"UartPrintf.h"
void DS18B20_Init(void)
{
DS18B20_Reset();
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0x44);
DelayNms(500);
DelayNms(500);
//while(!DQ);
}
void Read_Value(unsigned char *p, unsigned char idata j)
{
int i;
for(i=0;i<j; i++)
{
*p = DS18B20_ReadByte();
p++;
DelayUS(20);
}
}
int Get_Temp(unsigned char *p)
{
intTemp;
Temp = p*0x100 + p;
Temp /=16;
return Temp;
}
/**************************************
1@2.4us
2@3.0us
3@4.4us
4@5.4us
5@6.4us
10@11.6us
20@20.8us
50@51.2us
200@200us
*************************************/
void DelayUS(unsigned char n)//256
{
while (--n)
{
_nop_();
}
}
/********************************
1@1ms
2@2ms
3@3ms
10@10ms
800@800ms
********************************/
void DelayNms(unsigned int n)
{
unsigned int i;
while(n--)
{
for(i=0;i<550;i++);
}
}
void DS18B20_Reset(void)
{
while(1)
{
DQ=0;
DelayUS(200);
DelayUS(200);
DelayUS(80);
DQ=1;
DelayUS(60);
if(!DQ)
{
DQ=1;
DelayUS(200);
DelayUS(40);
DelayUS(200);
DelayUS(40);
break;
}
}
}
/**************************************
´ÓDS18B20ÖжÁÈ¡1×Ö½ÚÊý¾Ý
**************************************/
unsigned char DS18B20_ReadByte(void )
{
char i;
unsigned char dat;
for (i=0; i<8; i++)
{
dat >>= 1;
DQ=0;
_nop_();
_nop_();
DQ=1; //ÊÍ·Å×ÜÏß
DelayUS(16);
if (DQ)
{
dat |= 0x80;
}
DelayUS(60);
}
return dat;
}
/**************************************
ÏòDS18B20д1×Ö½ÚµÄÊý¾Ý
**************************************/
void DS18B20_WriteByte(char dat)
{
char i, datebuf;
for (i=0; i<8; i++)
{
datebuf = dat>>i;
if(datebuf & 0x01)
{
DQ=0;
_nop_();
_nop_();
DQ=1;
DelayUS(60);
}
else
{
DQ=0;
DelayUS(60);
DQ=1;
}
}
}
这都老黄历了,自己搜索一下看看吧,另外注意不能用推挽方式的 读写字节有问题,位起始低电平时间不够 标题不合格哦~明显是18b20的问题,不是51的问题~ 很抱歉的确是标题错了,单最终不是读写数据的时间问题,而是DS18B20复位时存在问题,抱歉抱歉
页:
[1]