/***************************************************************
*X25043/45applicationProcedures
*absoluteoneaddressaccess
***************************************************************
WARNING:Thefunctionwith'_'ahead,usermaynotuseit.asit
usedinternal
*/
//使用函数:write_status(status)写状态,一般写0
//clr_wchdog(void)清看门狗
//unsignedcharread_byte(address)//读一个字节
//voidwrite_byte(address,Data)//写一个字节
#defineALONE_COMPILE
#ifdefALONE_COMPILE
#include"INTRINS.H"
#endif
#ifndefnop2()
#definenop2()_nop_();_nop_();_nop_()
#endif
#defineWREN0x06
#defineWRDI0x04
#defineRDSR0x05
#defineWRSR0x01
#defineREAD0x03
#defineWRITE0x02
//#definePORTP1
sfrPORT=0x90;//25045的4根io脚接在同一端口,本例为p1
//请根据实际电路更改引脚定义
#define_SI0x80//si接在p1.3,0x80=00001000b
#define_SCK0x40//sck接在p1.2,0x80=00000100b
#define_SO0x20//so接在p1.1
#define_CS0x10//cs接在p1.0
//----------------------------------------------------------------
#ifndefdword
#definedwordunsignedlong
#definewordunsignedint
#definebyteunsignedchar
typedefunion{
wordw;
bytebh;
bytebl;
}WordType;
typedefunion{
dworddw;
wordw[2];
byteb[4];
}DwordType;
#endif
//----------------------------------------------------------------
void_w_byte(Data)
charData;
{
chari;
PORT&=(_SCK^0xff);
for(i=0;i<8;i++)
{
nop2();nop2();/////////////
if(Data&0x80)PORT|=_SI;
elsePORT&=(_SI^0xff);
nop2();nop2();/////////////
PORT|=_SCK;
nop2();nop2();/////////////
Data=Data<<1;
nop2();nop2();/////////////
PORT&=(_SCK^0xff);
nop2();nop2();/////////////
}
}
//----------------------------------------------------------------
char_r_byte(void)
{
chari;
charresult;
result=0;
for(i=0;i<8;i++)
{
nop2();nop2();/////////////
PORT|=_SCK;
result=result<<1;
nop2();nop2();/////////////
if((PORT&_SO)!=0)
result|=0x01;
nop2();nop2();/////////////
PORT&=(_SCK^0xff);
nop2();nop2();/////////////
}
return(result);
}
//----------------------------------------------------------------
voidwrite_status(status)
charstatus;
{
PORT&=(_CS^0xff);
nop2();nop2();/////////////
_w_byte(status);
PORT|=_CS;
nop2();nop2();/////////////
return;
}
//----------------------------------------------------------------
voidclr_wchdog(void)
{
PORT&=(_CS^0xff);
PORT|=_CS;
}
//----------------------------------------------------------------
voidwait_free(void)
{
unsignedintt;
t=3000;
while(--t);
}
//----------------------------------------------------------------
voidwrite_reg(_code)
char_code;
{
write_status(WREN);
PORT&=(_CS^0xff);
nop2();nop2();/////////////
_w_byte(WRSR);
_w_byte(_code);
nop2();nop2();/////////////
PORT|=_CS;
wait_free();
}
//----------------------------------------------------------------
unsignedcharread_byte(address)
unsignedintaddress;
{
charresult;
PORT&=(_CS^0xff);//Chipselect
nop2();nop2();/////////////
_w_byte((char)(address>255?(0x08|READ):READ));
_w_byte((char)(address&0x00ff));
result=_r_byte();
nop2();nop2();/////////////
PORT|=_CS;
//Chipunselect
return(result);
}
//----------------------------------------------------------------
voidwrite_byte(address,Data)
unsignedintaddress;
charData;
{
write_status(WREN);
nop2();nop2();/////////////
PORT&=(_CS^0xff);
nop2();nop2();/////////////
_w_byte((unsignedchar)(address>255?(0x08|WRITE):WRITE));
_w_byte((unsignedchar)(address&0x00ff));
_w_byte(Data);
nop2();nop2();/////////////
PORT|=_CS;
wait_free();
return;
}
/*
//----------------------------------------------------------------
unsignedlongread_data(format,address)
unsignedcharformat;
unsignedintaddress;
{
DwordTyperesult;
switch(format&0xdf)
{
case'L':
result.b[0]=read_byte(address);
result.b[1]=read_byte(address+1);
result.b[2]=read_byte(address+2);
result.b[3]=read_byte(address+3);
break;
case'D':
result.b[2]=read_byte(address);
result.b[3]=read_byte(address+1);
break;
case'C':
result.b[3]=read_byte(address);
break;
}
return(result.dw);
}
//----------------------------------------------------------------
voidwrite_data(format,address,Data)
unsignedcharformat;
unsignedintaddress;
DwordTypedata*Data;
{
switch(format&0xdf)
{
case'L':
write_byte(address,Data->b[0]);
write_byte(address+1,Data->b[1]);
write_byte(address+2,Data->b[2]);
write_byte(address+3,Data->b[3]);
break;
case'D':
write_byte(address,Data->b[2]);
write_byte(address+1,Data->b[3]);
break;
case'C':
write_byte(address,Data->b[3]);
break;
}
}
//----------------------------------------------------------------
*/