matlab ds18b20 单片机,51单片机串口读取DS18B20序列号的程序 显示出来的只有******,求指导!...

刚写了一个串口读取DS18B20序列号的程序,显示出来的只有******,求指导!谢谢了#include

#include      //nop函数

#define uchar unsigned char

#define uint unsigned int

sbit DQ=P3^7;

uchar Rom_18B20[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void  delay(uint z);                                //延时函数

bit   DS18B20_Readbit(void);                //读1位数据函数

uchar DS18B20_ReadByte(void);                //读1个字节数据函数

void  DS18B20_WriteByte(uchar dat);        //向DS18B20写一个字节数据函数

void get_ser(void);//读取序列号

/***************************************************/

/*         延时函数:void delay()                                                             */

/*         功能:延时函数                                                                           */

/***************************************************/

void delay(uint z)//延时函数

{

uint x,y;

for( x = z; x > 0; x-- )

for(y = 110;y>0;y--);

}

uchar DS18B20_Reset()

{

uchar i;

DQ = 0;                         //将总线拉低480us~960us

i = 70;

while(i--);         //延时642us

DQ= 1;                        //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低

i = 0;

while(DQ)        //等待DS18B20拉低总线

{

i++;

if(i>5)//等待>5MS

{

return 0;//初始化失败

}

delay(1);

}

return 1;//初始化成功

}

/***************************************************/

/*         DS18B20函数:void DS18B20_Readbit()                                  */

/*         功能:读1个字节数据函数                                                              */

/***************************************************/

bit DS18B20_Readbit(void)   //读1位数据函数

{

uint i;

bit dat;

DQ = 0;

i++;                 //i++起延时作用

DQ = 1;

i++;

i++;

dat = DQ;

i = 8;

while( i > 0 )i--;

return(dat);

}

/***************************************************/

/*         DS18B20函数:void DS18B20_ReadByte()                               */

/*         功能:读1个字节数据函数                                                              */

/***************************************************/

uchar DS18B20_ReadByte(void)        //读1个字节数据函数

{

uchar i,j,dat;

dat = 0;

for( i = 1; i <= 8; i++ )

{

j = DS18B20_Readbit();

dat = ( j << 7 ) | ( dat >> 1 );

}

return(dat);

}

/***************************************************/

/*         DS18B20函数:void DS18B20_WriteByte()                             */

/*         功能:向DQ18B20写一个字节数据函数                                            */

/***************************************************/

void DS18B20_WriteByte(uchar dat)        //向DQ18B20写一个字节数据函数

{

uint i;

uchar j;

bit testb;

for( j=1; j<=8; j++)

{

testb = dat&0x01;

dat= dat>>1;

if(testb)        //写1

{

DQ = 0;

i++;i++;

DQ = 1;

i = 8;while(i>0)i--;

}

else

{

DQ = 0;        //写0

i = 8;while(i>0)i--;

DQ = 1;

i++;i++;

}

}

}

/*****************************************************

函数:void get_ser(void)

功能:显示DS18B20的序列号

*******************************************************/

/*void get_ser(void)//读取温度

{

uchar i,temp;

while(DS18B20_Reset());

DS18B20_WriteByte(0x33);//读取光刻ROM中编码命令

delay(1);

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

{

Rom_18B20=DS18B20_ReadByte();

}

}  */

//串口初始化函数 //

//9600,N,8,1 //

//T1做波特率发生器//

void InitUart(void)

{

TMOD=0x20;

TH1=0xfd;

TL1=0xfd;

SCON=0x50;

PCON=0;

EA=1;

ES=1;

TR1 = 1;

}

void main()

{

uchar i;

InitUart();

while(1)

{

ES=0;

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

{

SBUF=Rom_18B20;

while(!TI);

TI=0;

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值