/*
*******************************************************************************
** Include files
*******************************************************************************
*/
#include <iom16.h>
#include <comp_a90.h>
#include <avr_macros.h>
#include <intrinsics.h>
#include "onewire.h"
//-----------------------------------------------------------------------------
//Generate a 1-wire reset, return 1 of no presence detect was found,
//return 0 otherwise.
//(NOTE: Does not handle alarm presence for DS2404/DS1994)
//
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is OneWire Reset.
**
** Parameters:
** None
**
** Returns:
** Return 1 of no presence detect was found, return 0 otherwise.
**
**-----------------------------------------------------------------------------
*/
unsigned char OWTouchReset(void)
{
unsigned char result = 0;
unsigned char i;
DQOut_Low(); //Drives DQ_T low
__delay_cycles(480*8);
DQOutData = High;
DQChangeInput(); //Releases the bus
__delay_cycles(40*8);
// result = DQ_T ^ 0x01; //Sample for presence pulse from slave
i = 250;
while(!DQ_T)
{
if(--i)
result = 1;
else
{
result = 0;
break;
}
_NOP();
}
__delay_cycles(300*8);
return result; //Return sample presence pulse result
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is Send a 1-wire bit.
**
** Parameters:
** provide 10us recovery time
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void OWWriteBit(unsigned char bit)
{
//write '1' bit
DQOut_Low(); //Drives DQ_T low
// delay1us();
__delay_cycles(8);
DQOutData = bit;
__delay_cycles(90*8); //Complete the time slot and 10us recovery
DQChangeInput(); //Releases the bus
__delay_cycles(30*8);
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is Read a bit from the 1-wire bus return it.
** Provide 10us recovery time.
** Parameters:
** None
**
** Returns:
** Return a bit from the 1-wire bus.
**
**-----------------------------------------------------------------------------
*/
unsigned char OWReadBit(void)
{
unsigned char result;
DQOut_Low(); //Drives DQ_T low
// delay1us();
DQChangeInput(); //Releases the bus
__delay_cycles(10*8);
_NOP();_NOP();_NOP();
result = DQ_T & 0x01; //Sample the bit value from the slave
__delay_cycles(60*8); //Complete the time slot and 10us recovery
return result;
}
/*
*******************************************************************************
** This is all for bit-wise manipulation of the 1-Wire bus.The above routines
** can be built upon to create byte-wise manipulator functions as seen in
** Example 3.
** Example 3.Derived 1-Wire Functions
*******************************************************************************
*/
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is Wirte 1-Wire data byte.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void OWWriteByte(unsigned char data)
{
unsigned char loop;
//Loop to write each bit in the byte, LS-bit first
for(loop = 8; loop != 0 ; loop--)
{
OWWriteBit(data & 0x01);
data >>= 1;
}
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is Read 1-wire data byte and retrurn it.
**
** Parameters:
** None
**
** Returns:
** Return read 1-wire
**
**-----------------------------------------------------------------------------
*/
unsigned char OWReadByte(void)
{
unsigned char loop,result = 0;
for(loop = 8;loop != 0;loop --)
{
//Shift the result to get it ready for the nexit bit
result >>= 1;
//if result is one,then set MS bit
if(OWReadBit())
result |= 0x80;
}
return result;
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is Write a 1-wire data byte and return the
** sampled result.
**
** Parameters:
** Write value.
**
** Returns:
** return the sampled result.
**
**-----------------------------------------------------------------------------
*/
unsigned char OWTouchByte(unsigned char data)
{
unsigned char loop,result = 0;
for(loop = 8;loop != 0;loop --)
{
//Shift the result to get it ready for the next bit
result >>= 1;
//if sending a '1' then read a bit else write a '0'
if(data & 0x01)
{
if(OWReadBit())
result |= 0x80;
}
else
OWWriteBit(0);
//Sfhift the data byte for the next bit
data >>= 1;
}
return result;
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function is Wirte a block 1-wire data bytes and return
** the sampled result in the same buffer.
**
** Parameters:
** None
**
** Returns:
** Return the sampled result.
**
**-----------------------------------------------------------------------------
*/
void OWOverdriveSkip(unsigned char *data,unsigned char data_len)
{
unsigned char loop;
for( loop = 8; loop != 0; loop --)
{
data[loop] = OWTouchByte(data[loop]);
}
}
熊已出没
- 粉丝: 60
- 资源: 29
最新资源
- C语言实例-毕业设计项目:迷宫生成与路径搜索程序-开题报告,论文,答辩PPT参考
- springboot艺术摄影预约.zip
- springboot医疗用品销售网站.zip
- springboot养老院管理系统.zip
- springboot研究生双选信息发布系统.zip
- springboot研究生科研文档资料管理系统.zip
- C语言实例-毕业设计项目:角色扮演游戏开发-开题报告,论文,答辩PPT参考
- springboot学生宿舍管理系统.zip
- springboot学生就业信息管理系统.zip
- springboot学生操行评分系统.zip
- springboot校园闲置物品.zip
- Stata 软件介绍与使用指南+案例操作(小白可上手).doc
- springboot校园外卖配送系统.zip
- springboot校园失物招领平台.zip
- C语言实例-毕业设计项目:KTV歌曲管理系统开发-开题报告,论文,答辩PPT参考
- springboot小区物业管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈