?? ib.c
字號:
/*
*********************************************************************************************************
*Copyright(c)2005,廣東必達保安系統(tǒng)有限公司電子開發(fā)部
*All rights rerserved
*
*文件名稱:ib.c
*文件標(biāo)識:
*摘 要:Dallas iB的IAR V3.20A 底層協(xié)議包
*
*當(dāng)前版本:1.0
*作 者:錢少群
*完成日期:2006年06月20日
*
*取代版本:
*作 者:
*完成日期:
*
*********************************************************************************************************
*/
#include "includes.h"
///////////////////////////////////////////////////
// 1-WIRE 操作
///////////////////////////////////////////////////
/*
*****************************************************
*描述: 讀 DS19XX ID
*
*入口: idBuf ID數(shù)據(jù)區(qū)指針
*
*出口: 返回值 = 1 數(shù)據(jù)有效
* 返回值 = 0 數(shù)據(jù)無效
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TMROM
unsigned char Tm_Rom(unsigned char *idBuf)
{
unsigned char right = 0;
unsigned char n = 8;
unsigned char *tBuf;
tBuf = idBuf;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBREADROM); //33H read rom command
do{
*tBuf++ = Tm_Rxd(); //read data
}while (--n);
--tBuf;
if (Crc_8n(idBuf, 7) == *tBuf) //crc 校驗
{
right = 1;
}
if (*idBuf == 0) right = 0;
if (*idBuf == 0xFF) right = 0;
}
return right;
}
#endif
/*
*****************************************************
*描述: 讀 DS1991 subkey
*
*入口: adr subkey地址
* idBuf ID數(shù)據(jù)區(qū)指針
* passwordBuf subkey密碼區(qū)指針
* dataBuf subkey數(shù)據(jù)區(qū)指針
* n 字節(jié)數(shù)
*出口: idBuf ID數(shù)據(jù)區(qū)指針
* dataBuf subkey數(shù)據(jù)區(qū)指針
* 返回值 = 1 數(shù)據(jù)有效
* 返回值 = 0 數(shù)據(jù)無效
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TMRDS
unsigned char Tm_Rds(unsigned char adr,
unsigned char *idBuf,
unsigned char *passwordBuf,
unsigned char *dataBuf,
unsigned char n)
{
unsigned char right = 0;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBREADSUBKEY); //read subkey
Tm_Txd(adr); //send low of address
adr = ~adr;
Tm_Txd(adr);
adr = 8; //read identifier(ID)
do{
*idBuf++ = Tm_Rxd();
}while (--adr);
adr = 8; //write password
do{
Tm_Txd(*passwordBuf++);
}while (--adr);
do{ //read data
*dataBuf++ = Tm_Rxd();
}while (--n);
right = Tm_Rst(); //reset TM
}
return right;
}
#endif
/*
*****************************************************
*描述: 寫 DS1991 subkey
*
*入口: adr subkey地址
* idBuf ID數(shù)據(jù)區(qū)指針
* passwordBuf subkey密碼區(qū)指針
* dataBuf subkey數(shù)據(jù)區(qū)指針
* n 字節(jié)數(shù)
*出口: idBuf ID數(shù)據(jù)區(qū)指針
* 返回值 = 1 數(shù)據(jù)寫正確
* 返回值 = 0 數(shù)據(jù)寫錯
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TMWRS
unsigned char Tm_Wrs(unsigned char adr,
unsigned char *idBuf,
unsigned char *passwordBuf,
unsigned char *dataBuf,
unsigned char n)
{
unsigned char right = 0;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBWRITESUBKEY); //write subkey
Tm_Txd(adr); //send address
adr = ~adr;
Tm_Txd(adr);
adr = 8; //read identifier(ID)
do{
*idBuf++ = Tm_Rxd();
}while (--adr);
adr = 8; //write password
do{
Tm_Txd(*passwordBuf++);
}while (--adr);
do{ //write data
Tm_Txd(*dataBuf++);
}while (--n);
right = Tm_Rst(); //reset TM
}
return right;
}
#endif
/*
*****************************************************
*描述: 讀 DS1994 時鐘
*
*入口: timeBuf 時間區(qū)指針
*
*出口: 返回值 = 1 讀時鐘對
* 返回值 = 0 讀時鐘錯
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TM1994
unsigned char Tm_1994(unsigned char *timeBuf)
{
return Tm_Rdp(02, 02, timeBuf, 5);
}
#endif
/*
*****************************************************
*描述: 讀 DS1904 時鐘
*
*入口: dataBuf 數(shù)據(jù)區(qū)指針
*
*
*
*出口: 返回dataBuf數(shù)據(jù)區(qū)數(shù)據(jù)
* 返回值 = 1 讀數(shù)據(jù)對
* 返回值 = 0 讀數(shù)據(jù)錯
*
*原創(chuàng): 楊富添
*時間: 2008年10月13日
*修改:
*時間:
******************************************************
*/
#if TMRDP1904
unsigned char Tm_Rdp1904(unsigned char *dataBuf)
{
unsigned char right = 0;
unsigned char n = 5;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBREADSUBKEY); //66H read clock
do{
*dataBuf++ = Tm_Rxd();//receive data (1個控制字節(jié) + 4字節(jié)數(shù)據(jù))
}while (--n);
right = Tm_Rst(); //reset TM
}
return right;
}
#endif
/*
*****************************************************
*描述: 寫 DS1904
*
*入口:
*
* dataBuf 數(shù)據(jù)區(qū)指針
* n 字節(jié)數(shù)
*出口: 返回值 = 1 寫數(shù)據(jù)對
* 返回值 = 0 寫數(shù)據(jù)錯
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TMWRP1904
unsigned char Tm_Wrp1904(unsigned char *dataBuf)
{
unsigned char right = 0;
unsigned char n = 5;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBWRITESUBKEY);//write scratchpad
do{
Tm_Txd(*dataBuf++);//send data
}while (--n);
right = Tm_Rst(); //reset TM
}
return right;
}
#endif
/*
*****************************************************
*描述: 讀 DS1996 頁
*
*入口: adrHigh 數(shù)據(jù)高地址
* adrLow 數(shù)據(jù)低地址
* dataBuf 數(shù)據(jù)區(qū)指針
* n 字節(jié)數(shù)
*出口: 返回dataBuf數(shù)據(jù)區(qū)數(shù)據(jù)
* 返回值 = 1 讀數(shù)據(jù)對
* 返回值 = 0 讀數(shù)據(jù)錯
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TMRDP
unsigned char Tm_Rdp(unsigned char adrHigh,
unsigned char adrLow,
unsigned char *dataBuf,
unsigned char n)
{
unsigned char right = 0;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBREADMEMORY); //read memory
Tm_Txd(adrLow); //send low of address
Tm_Txd(adrHigh); //send high of address
do{
*dataBuf++ = Tm_Rxd();//receive data
}while (--n);
right = Tm_Rst(); //reset TM
}
return right;
}
#endif
/*
*****************************************************
*描述: 寫 DS1996 頁
*
*入口: adrHigh 數(shù)據(jù)高地址
* adrLow 數(shù)據(jù)低地址
* dataBuf 數(shù)據(jù)區(qū)指針
* n 字節(jié)數(shù)
*出口: 返回值 = 1 寫數(shù)據(jù)對
* 返回值 = 0 寫數(shù)據(jù)錯
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TMWRP
unsigned char Tm_Wrp(unsigned char adrHigh,
unsigned char adrLow,
unsigned char *dataBuf,
unsigned char n)
{
unsigned char right = 0;
unsigned char ta1;
unsigned char ta2;
unsigned char ta3;
//unsigned char ta4;
//ta4 = n;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBWRITESCRATCHPAD);//write scratchpad
Tm_Txd(adrLow); //send low of address
Tm_Txd(adrHigh); //send high of address
do{
Tm_Txd(*dataBuf++);//send data
}while (--n);
Tm_Rst();
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBREADSCRATCHPAD);//read scratchpad
ta1 = Tm_Rxd();
ta2 = Tm_Rxd();
ta3 = Tm_Rxd();
Tm_Rst();
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBCOPYSCRATCHPAD);//copy scratchpad
Tm_Txd(ta1);
Tm_Txd(ta2);
Tm_Txd(ta3);
right = Tm_Rst(); //reset TM
}
return right;
}
#endif
/*
*****************************************************
*描述: 讀 DS1971 頁
*
*入口: adr 數(shù)據(jù)地址
* dataBuf 數(shù)據(jù)區(qū)指針
* n 字節(jié)數(shù)
*出口: 返回值 = 1 讀數(shù)據(jù)對
* 返回值 = 0 讀數(shù)據(jù)錯
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
#if TMRD71
unsigned char Tm_Rd71(unsigned char adr,
unsigned char *dataBuf,
unsigned char n)
{
unsigned char right = 0;
if (Tm_Rst() == 1) //reset TM
{
Tm_Txd(IBSKIPROM); //skip rom
Tm_Txd(IBREADMEMORY); //read memory
Tm_Txd(adr); //send low of address
do{
*dataBuf++ = Tm_Rxd();//receive data
}while (--n);
right = Tm_Rst(); //reset TM
}
return right;
}
#endif
//////////////////////////////////////////////////////
//// DS19XX 操作時序
//////////////////////////////////////////////////////
/*
*****************************************************
*描述: DS19XX 復(fù)位時序
*
*入口:
*
*出口: 返回值 = 1 復(fù)位正確
* 返回值 = 0 復(fù)位錯誤
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改: 馮純枝
*時間: 2005年12月12日
******************************************************
*/
unsigned char Tm_Rst(void)
{
unsigned char rst = 0;
if (Tm_Wdrst() == 1) rst = 1;
Delay_10Us(41); //delay 410us
return rst;
}
/*
*****************************************************
*描述: DS19XX 發(fā)送字節(jié)時序
*
*入口: valTx 發(fā)送數(shù)據(jù)
*
*出口:
*
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
void Tm_Txd(unsigned char valTx)
{
unsigned char n = 8;
s(DDRTM, TM); //Set output
do{
c(PORTTM, TM); //clr TM
Delay_5Us(); //delay 8us
if (b(valTx,0))
{
s(PORTTM, TM); //SET TM
}
Delay_10Us(9); //Delay 90us
s(PORTTM, TM); //set TM
Delay_5Us(); //delay 8us
valTx = valTx >> 1;
}while (--n);
}
/*
*****************************************************
*描述: DS19XX 接收字節(jié)時序
*
*入口:
*
*出口: 返回數(shù)據(jù)
*
*
*原創(chuàng): 錢少群
*時間: 2005年3月21日
*修改:
*時間:
******************************************************
*/
unsigned char Tm_Rxd(void)
{
unsigned char n = 8;
unsigned char valRx = 0;
do{
s(DDRTM, TM); //set output
c(PORTTM, TM); //clr TM
Delay_5Us(); //delay 5us
valRx = valRx >> 1;
s(PORTTM, TM);
c(DDRTM, TM);
Delay_10Us(1); //delay 10us
if (b(PINTM, TM)) valRx |= 0x80;//test TM if TM=1 then set '1'
Delay_10Us(10); //delay 100us
}while (--n);
return valRx;
}
/*
*****************************************************
*描述: DS19XX 看門狗讀復(fù)位時序
*
*入口:
*
*出口: 返回值 = 1 復(fù)位正確
* 返回值 = 0 復(fù)位錯誤
*
*原創(chuàng): 馮純枝
*時間: 2005年12月12日
*修改:
*時間:
******************************************************
*/
unsigned char Tm_Wdrst(void)
{
unsigned char rst = 0;
s(DDRTM, TM); //Set output
c(PORTTM, TM); //clr TM
Delay_10Us(50); //delay 500us
s(PORTTM, TM); //PULL UP
c(DDRTM, TM); //Set input
Delay_10Us(9); //delay 90us
if (b(PINTM, TM) == 0) //test TM if TM=0 then OK=1
rst = 1;
//OK為標(biāo)志OK=0=false OK=1=true
return rst;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -