|
< >#include<REG51.H>
#include<INTRINS.H>
#include<ABSACC.H>
#include<MATH.H>
#include<stdio.h></P>
< >#define Uchar unsigned char
#define Uint unsigned int
#define Ulong unsigned long</P>
< >sbit ChipSelect=P1^4;
sbit BankPort0=P1^0;
sbit BankPort1=P1^1;
sbit WriteEn=P3^6;
sbit Led=P1^2;
bit ErrorFlag=0;</P>
<P>void DelayMS ( Uchar TMS )
{
Uchar i,j;
for ( i=4*TMS;i>0;i-- )
for ( j=200;j>0;j-- );
}</P>
<P>/*入口参数:Address为欲读地址,0<=Address<=0x3ffff,既0-256k
出口参数:所读数据,0-0xff
如:data=ReadByte(0x2ffffL);注意:一定要在地址后加L
*/
Uchar ReadByte ( Ulong Address )
{
Uchar Banksel,ReadData;
Uint BankAddress;
if ( Address>=0l&&Address<=0xffffl ){Banksel=0;BankAddress=Address;}
if ( Address>=0x10000l&&Address<=0x1ffffl ){Banksel=1;BankAddress=Address-0x10000l;}
if ( Address>=0x20000l&&Address<=0x2ffffl ){Banksel=2;BankAddress=Address-0x20000l;}
if ( Address>=0x30000l&&Address<=0x3ffffl ){Banksel=3;BankAddress=Address-0x30000l;}
// else { ErrorFlag=1;goto ReadError; }
switch(Banksel)
{
case 0:BankPort0=0;BankPort1=0;break;
case 1:BankPort0=1;BankPort1=0;break;
case 2:BankPort0=0;BankPort1=1;break;
case 3:BankPort0=1;BankPort1=1;break;
}
ChipSelect=0;
ReadData=XBYTE[BankAddress];
_nop_();
_nop_();
ChipSelect=1;
return ( ReadData );
// ReadError: ;
}</P>
<P>/*入口参数:Address为欲写数据的起始地址,该地址应为128的整数倍,包括0L,注意:一定要在地址后加L
WriteData为待写数据,0<=WriteData<=0xff
如:for(i=0;i<=127;i++)
WriteByte ( 0x30000L,i );
则可以把0-127写到0x30000L-0x3007fL
如果要写0xbb到芯片的0x30001L,则需写成:
WriteByte ( 0x30000L,ReadByte(0x30000L) );
WriteByte ( 0x30001L,0xbb );
for ( i=0;i<126;i++ )
WriteByte ( 0x30002L+i,ReadByte(0x30002L+i) );
这样才可以保证其他地址内容不变
*/
void WriteByte ( Ulong Address,Uchar WriteData )
{
Uchar Banksel;
Uint BankAddress;
if ( Address>=0&&Address<=0xffff ){Banksel=0;BankAddress=Address;}
else if ( Address>=0x10000l&&Address<=0x1ffffl ){Banksel=1;BankAddress=Address-0x10000l;}
else if ( Address>=0x20000l&&Address<=0x2ffffl ){Banksel=2;BankAddress=Address-0x20000l;}
else {Banksel=3;BankAddress=Address-0x30000l;}//if ( Address>=0x30000l&&Address<=0x3ffffl )
// else { ErrorFlag=1;goto WriteError; }
switch(Banksel)
{
case 0:BankPort0=0;BankPort1=0;break;
case 1:BankPort0=1;BankPort1=0;break;
case 2:BankPort0=0;BankPort1=1;break;
case 3:BankPort0=1;BankPort1=1;break;
default :break;
}
ChipSelect=0;
XBYTE[BankAddress]=WriteData;
if ( ((BankAddress+1)%128)==0 )DelayMS ( 10 );
ChipSelect=1;
}</P>
<P>void DisableProtect ( void )//禁止软件写保护
{
WriteByte ( 0x5555,0xaa );
WriteByte ( 0x2aaa,0x55 );
WriteByte ( 0x5555,0x80 );
WriteByte ( 0x5555,0xaa );
WriteByte ( 0x2aaa,0x55 );
WriteByte ( 0x5555,0x20 );
DelayMS ( 10 );
}</P>
<P>void EarseChip ( void ) //整片擦除为0xff,一般不调用
{
WriteByte ( 0x5555,0xaa );
WriteByte ( 0x2aaa,0x55 );
WriteByte ( 0x5555,0x80 );
WriteByte ( 0x5555,0xaa );
WriteByte ( 0x2aaa,0x55 );
WriteByte ( 0x5555,0x10 );
DelayMS ( 50 );
}</P>
<P>void main ( void )
{
Ulong i;
DisableProtect ( );
EarseChip ( );
for ( i=0x0000l;i<=0x3ffffl;i++ )
{
WriteByte ( i,0x99 );
}
P1=ReadByte ( 0x2ffffL );
while(1);
}
</P> |
|