📄 1wire.c
字号:
#include "w77e532.h"
#include "1wire.h"
#include <intrins.h>
/************************************************************
*Function:延时处理
*parameter:
*Return:
*Modify:
*************************************************************/
void TempDelay (unsigned char us)
{
while(us--);
}
/****************************************
*1-wire reset function
*call this function before rom command and function command
*return value:1 :have 1-wire device;0: no 1-wire device
******************************************/
unsigned char wire1_reset()
{
bit presence;
WIRE1_DQ = 1;
_nop_();
WIRE1_DQ = 0;
TempDelay(86); //delay 530 uS//80
_nop_();
WIRE1_DQ = 1;
TempDelay(14); //delay 100 uS//14
_nop_();
_nop_();
_nop_();
presence = WIRE1_DQ;
TempDelay(40); //20//mod to 40//09-19
_nop_();
_nop_();
WIRE1_DQ = 1;
if (0 == presence)
return 1;
else
return 0;
}
/******************************************
*read a bit
*return value: the bit readed
*******************************************/
unsigned char wire1_readbit()
{
unsigned char retVal, i;
WIRE1_DQ = 0;
i++;
WIRE1_DQ = 1;
i++;
i++;
i++;
retVal = WIRE1_DQ;
for(i=0; i<19; i++);
return retVal;
}
/*******************************************
*write a bit
*bitval:write value
*******************************************/
void wire1_writebit(unsigned char bitval)
{
unsigned char i;
WIRE1_DQ = 0;
i++;
if (bitval == 1)
WIRE1_DQ = 1;
for(i=0; i<18; i++);
WIRE1_DQ = 1;
}
/********************************************
*
*********************************************/
unsigned char wire1_readbyte()
{
unsigned char i,u=0;
unsigned char temp;
for(i=0;i<8;i++)
{
WIRE1_DQ = 0;
u >>= 1;
WIRE1_DQ = 1;
temp = WIRE1_DQ;
if(temp==1)
u |= 0x80;
TempDelay(12);//25);//4);//mod to 12 09-19
/**add09-19**/
WIRE1_DQ = 1;
TempDelay(1);
/*end of add*/
_nop_();
}
return(u);
}
/*******************************************
*
*******************************************/
void wire1_writebyte(unsigned char value)
{
unsigned char i;
for (i=0;i<8;i++)
{
WIRE1_DQ = 0;
_nop_();
WIRE1_DQ = value & 0x01;
TempDelay(13);//25);//5); //delay 45 uS //5//mod to 13 09-19
_nop_();
_nop_();
WIRE1_DQ=1;
value >>= 1;
//add
TempDelay(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -