📄 ibutten_c.txt
字号:
//===========================================================================
// iButton DS1991 Functions
//***************************************************************************
#i nclude "ibutton.h"
#i nclude "typedef.h"
#i nclude "resource.h"
#define bDataBus pinIButtonData
typedef struct _iButton
{
UCHAR *PassWord;
UCHAR Identify[8];
UCHAR RomFuncCmd; //33h,55h,F0h,CCh-Skip ROM
}IBUTTON;
//--------------------------------------------------------------------------
IBUTTON iButton;
extern void DelayCycle(unsigned char nCycle);
//===========================================================================
UCHAR _iButtonReset(void) //return 0 if exist device
{
UCHAR i;
EA=0;
//发复位低脉冲tRSTL>80us
bDataBus=0;
DelayCycle(200); //about 439us
//释放总线,等待上拉电阻将总线恢复高电平tPDH=5us~60us
bDataBus=1;
DelayCycle(12); //跳过上拉电阻上拉总线时间, about 29us
//等待对方返回低脉冲, tPDL=60us~240us
i=0;
while(bDataBus==1)
{
i++;
if(i>27) {EA=1; return 0x0E;}
}
//等待对方恢复高电平
DelayCycle(75);
i=0;
while(bDataBus==0)
{
if(i>54) break;
i++;
}
DelayCycle(180);
EA=1;
return 0;
}
//===========================================================================
void _iButtonWriteBit(bit bBit) //读或写周期60us~120us
{
EA=0;
//总线值低电平tLow1=1us~15us
bDataBus=0;
DelayCycle(0);
//写周期TH=60us-tLow1, 对方在检测到总线=0开始15us后,将总线采样写入
bDataBus=bBit;
DelayCycle(14);
bDataBus=1;
EA=1;
DelayCycle(15);
}
//===========================================================================
bit _iButtonReadBit(void) //读周期60us~120us
{
bit bRet;
EA=0;
//总线值低电平tSu<1us
bDataBus=0;
DelayCycle(0);
//等待总线数据到达tLOW=1us~15us
bDataBus=1;
DelayCycle(2);
//采样周期,对方在检测到总线=0开始15us后,将数据放在总线上,持续时间0~45us
//然后释放总线,总时间共60us-TL.
bRet=bDataBus;
DelayCycle(14);
EA=1;
DelayCycle(15);
return bRet;
}
//===========================================================================
void _iButtonWriteByte(UCHAR TxByte)
{
UCHAR i=8;
while(i>0)
{
_iButtonWriteBit(TxByte&0x01);
TxByte>>=1;
i--;
}
}
//===========================================================================
UCHAR _iButtonReadByte(void)
{
UCHAR RxByte;
bit bRx;
UCHAR i=8;
while(i>0)
{
RxByte>>=1;
bRx=_iButtonReadBit();
if(bRx) RxByte|=0x80;
else RxByte&=0x7F;
i--;
}
return RxByte;
}
//===========================================================================
//pRxBuffer: 如果pRxBuffer==NULL, 将不对其所指向的地址写入系统码
//Return: 返回系统码(FamillyCode(1byte)+SerialCode(6Byte)+CRC(1byte
//---------------------------------------------------------------------------
UCHAR _iButtonSendRomFuncCmd(UCHAR *pRxBuffer)
{
UCHAR nBytes=0;
_iButtonWriteByte(iButton.RomFuncCmd);
if(iButton.RomFuncCmd==0xCC) return 0; //0xCC命令不不返回系统码
//如果RomFuncCmd!=0xCC,则接收系统码
if(pRxBuffer)
{
while(nBytes<8)
{
*pRxBuffer=_iButtonReadByte();
pRxBuffer++;
nBytes++;
}
}
return nBytes;
}
//===========================================================================
//iButtonAddr: SubKey0: 10h-3Fh; SubKey1: 50h-7Fh, Subkey2: 90h-BFh
// ScratchPad: D0h-FFh
//OperateId: 0-Read, 1-Write, 2-Copy ScratchPad or Set Subkey Password
//---------------------------------------------------------------------------
void _iButtonWriteAddress(UCHAR iButtonAddr, UCHAR OperateId)
{
UCHAR MemOpCmd;
MemOpCmd=iButtonAddr&0xC0;
if(MemOpCmd==0xC0)
{
if(OperateId==0) MemOpCmd=0x69; //读ScratchPad
if(OperateId==1) MemOpCmd=0x96; //写ScratchPad
if(OperateId==2) MemOpCmd=0x3C; //拷贝ScratchPad
}
else
{
if(OperateId==0) MemOpCmd=0x66; //读Subkey
if(OperateId==1) MemOpCmd=0x99; //写Subkey
if(OperateId==2) MemOpCmd=0x5A; //写Subkey密码
}
_iButtonWriteByte(MemOpCmd);
_iButtonWriteByte(iButtonAddr);
_iButtonWriteByte(~iButtonAddr);
//如果是对Subkey操作的ROM命令,需要密码读取Identify标识码,并写入密码进行验证
if((MemOpCmd==0x66)||(MemOpCmd==0x99)||(MemOpCmd==0x5A))
{
int i;
for(i=0; i<8; i++)
{
iButton.Identify[i]=_iButtonReadByte(); //读出iButton的系列号
}
for(i=0; i<8; i++)
{
_iButtonWriteByte(iButton.PassWord[i]); //提供密码给iButton校验
}
}
}
//===========================================================================
void iButtonScratchpadWriteByte(UCHAR TargetAddr, UCHAR dbWriteByte) //TargetAddress=10h-3fh
{
_iButtonReset();
_iButtonSendRomFuncCmd(0); //0x00 needn't system code return
_iButtonWriteAddress(0xC0|TargetAddr,1); //0xD0 in ScratchPad Section
_iButtonWriteByte(dbWriteByte);
_iButtonReset();
}
//===========================================================================
unsigned char iButtonScratchpadReadByte(UCHAR TargetAddr) //TargetAddress=10h-3fh
{
UCHAR dbReadByte;
_iButtonReset();
_iButtonSendRomFuncCmd(0); //0x00 needn't system code return
_iButtonWriteAddress(0xC0|TargetAddr,0); //0xD0 in ScratchPad Section
dbReadByte=_iButtonReadByte();
_iButtonReset();
return dbReadByte;
}
//===========================================================================
/*void iButtonScratchpadWrite(UCHAR *pTxBuffer, UCHAR ByteCount) //整块写
{
if(ByteCount==0) return;
_iButtonReset();
_iButtonSendRomFuncCmd(0); //0x00 needn't system code return
_iButtonWriteAddress(0xD0,1); //0xD0 in ScratchPad Section
while(ByteCount>0)
{
_iButtonWriteByte(*pTxBuffer);
pTxBuffer++;
ByteCount--;
}
_iButtonReset();
}
//===========================================================================
void iButtonScratchpadRead(UCHAR *pRxBuffer, UCHAR ByteCount) //整块读
{
if(ByteCount==0) return;
_iButtonReset();
_iButtonSendRomFuncCmd(0); //0x00 needn't system code return
_iButtonWriteAddress(0xD0,0); //0xD0 in ScratchPad Section
while(ByteCount>0)
{
*pRxBuffer=_iButtonReadByte();
pRxBuffer++;
ByteCount--;
}
_iButtonReset();
}*/
//===========================================================================
void iButtonInit(UCHAR RomFuncCmd, UCHAR *pPassWord) //PassWord=33h,55h,F0h,CCh-Skip ROM
{
iButton.RomFuncCmd=RomFuncCmd;
iButton.PassWord=pPassWord;
}
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -