/*************************
适合晶振12MHz左右(以下)
*************************/
#ifndef __DS1WB_H__
#define __DS1WB_H__
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define nop20us() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
#define nop3us() _nop_();_nop_();_nop_();
sbit ds1wbPin=P2^2;// 单总线数据引脚, 1wb for one wire bus
void nop80us()
{
static uchar j;
j=4;
while(j--)
nop20us();
}
/*
returns 1 if there is any device on the bus and is ready to operate,
returns 0 otherwise.
*/
bit ds1wbInit()
{
uchar i;
bit b=0;
ds1wbPin=0;
i=25;
while(i--)
nop20us();// reset pulse by master
ds1wbPin=1;
i=25;
while(i--)
{
nop20us();
if(!b && !ds1wbPin)// presence pulse by slaves
b=1;
}
return b;
}
uchar ds1wbReadByte()
{
uchar i,d;
d=0;
for(i=0;i<8;i++)
{
d>>=1;
ds1wbPin=0;
nop3us();
ds1wbPin=1;
nop3us();
if(ds1wbPin)
d|=0x80;
nop80us();
}
return d;
}
void ds1wbWriteByte(uchar b)
{
uchar i;
for(i=0;i<8;i++)
{
if(b&0x01)
{
ds1wbPin=0;
nop3us();
ds1wbPin=1;
nop80us();
}
else
{
ds1wbPin=0;
nop80us();
ds1wbPin=1;
nop3us();
}
b>>=1;
}
}
#endif
文章来之 源码世界 http://www.ymsky.net/views/68642.shtml 点击打开链接