STC89C52 dallas单总线读写头文件

/*************************
	适合晶振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    点击打开链接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值