#include<reg52.h>
sbitCS=P2^7;
sbitSK=P2^6;
sbitDI=P2^5;
sbitDO=P2^4;
/*
externunsignedcharReadChar(unsignedcharaddress);
externvoidWriteChar(unsignedcharaddress,unsignedcharInData);
externvoidReadString(unsignedchardata*RamAddress,unsignedcharRomAddress,unsignedcharNumber);
externvoidWriteString(unsignedchardata*RamAddress,unsignedcharRomAddress,unsignedcharNumber);
*/
//Writeenablemustprecedeallprogrammingmodes.
voidEwen(void){
unsignedchartemp,InData;
CS=0;
SK=0;
CS=1;
InData=0x98;//10011XXXX
for(temp=9;temp!=0;temp--){//9
DI=InData&0x80;
SK=1;SK=0;
InData<<=1;
}
CS=0;
}
//Disablesallprogramminginstructions.
voidEwds(void){
unsignedchartemp,InData;
CS=0;
SK=0;
CS=1;
InData=0x80;//10000XXXX
for(temp=9;temp!=0;temp--){//9
DI=InData&0x80;
SK=1;SK=0;
InData<<=1;
}
CS=0;
}
//Readsdatastoredinmemory,atspecifiedaddress.
unsignedintRead(unsignedcharaddress){
unsignedchartemp;
unsignedintresult;
Ewen();
SK=0;DI=1;//110A5-A0
CS=0;CS=1;
SK=1;SK=0;//1
address=address&0x3f|0x80;
for(temp=8;temp!=0;temp--){//8
DI=address&0x80;
SK=1;SK=0;
address<<=1;
}
DO=1;
for(temp=16;temp!=0;temp--){//16
SK=1;
result=(result<<1)|DO;
SK=0;
}
CS=0;
Ewds();
return(result);
}
//WritesmemorylocationAn-A0.
voidWrite(unsignedcharaddress,unsignedintInData){
unsignedchartemp;
Ewen();
SK=0;DI=1;//101A5-A0
CS=0;CS=1;
SK=1;SK=0;//1
address=address&0x3f|0x40;
for(temp=8;temp!=0;temp--){//8
DI=address&0x80;
SK=1;SK=0;
address<<=1;
}
for(temp=16;temp!=0;temp--){//16
DI=InData&0x8000;
SK=1;SK=0;
InData<<=1;
}
CS=0;DO=1;
CS=1;SK=1;
while(DO==0){//busytest
SK=0;SK=1;
}
SK=0;CS=0;
Ewds();
}
/*
//ErasememorylocationAn-A0.
voidErase(unsignedcharaddress){
unsignedchartemp;
Ewen();
SK=0;DI=1;//111A5-A0
CS=0;CS=1;
SK=1;SK=0;//1
address|=0xc0;
for(temp=8;temp!=0;temp--){//8
DI=address&0x80;
SK=1;SK=0;
address<<=1;
}
CS=0;DO=1;
CS=1;SK=1;
while(DO==0){
SK=0;SK=1;
}
SK=0;CS=0;
Ewds();
}
//Erasesallmemorylocations.ValidonlyatVCC=4.5Vto5.5V.
voidEral(void){
unsignedchartemp,InData;
Ewen();
CS=0;
SK=0;
CS=1;
InData=0x90;//10010XXXX
for(temp=9;temp!=0;temp--){//9
DI=InData&0x80;
SK=1;SK=0;
InData<<=1;
}
CS=0;DO=1;
CS=1;SK=1;
while(DO==0){
SK=0;SK=1;
}
SK=0;CS=0;
Ewds();
}
//Writesallmemorylocations.ValidonlyatVCC=4.5Vto5.5V.
voidWral(unsignedintInData){
unsignedchartemp,address;
Ewen();
CS=0;
SK=0;
CS=1;
address=0x88;//10001XXXX
for(temp=9;temp!=0;temp--){//9
DI=address&0x80;
SK=1;SK=0;
address<<=1;
}
for(temp=16;temp!=0;temp--){//16
DI=InData&0x8000;
SK=1;SK=0;
InData<<=1;
}
CS=0;DO=1;
CS=1;SK=1;
while(DO==0){
SK=0;SK=1;
}
SK=0;CS=0;
Ewds();
}
*/
unsignedcharReadChar(unsignedcharaddress){
unsignedchartemp=address>>1;
if(address&0x01)return((unsignedchar)(Read(temp)>>8));
elsereturn((unsignedchar)(Read(temp)));
}
voidWriteChar(unsignedcharaddress,unsignedcharInData){
unsignedchartemp=address>>1;
if(address&0x01)Write(temp,(unsignedint)(Read(temp)&0x00ff|(InData<<8)));
elseWrite(temp,(unsignedint)(Read(temp)&0xff00|InData));
}
voidReadString(unsignedchardata*RamAddress,unsignedcharRomAddress,unsignedcharNumber){
while(Number!=0){
*RamAddress=ReadChar(RomAddress);
RamAddress++;
RomAddress++;
Number--;
}
}
voidWriteString(unsignedchardata*RamAddress,unsignedcharRomAddress,unsignedcharNumber){
unsignedinttemp;
if(Number==0)return;
if(RomAddress&0x01){
WriteChar(RomAddress,*RamAddress);
RamAddress++;
RomAddress++;
Number--;
}
if(Number==0)return;
while(Number>>1){
temp=*RamAddress;
RamAddress++;
temp=temp|(*RamAddress)<<8;
RamAddress++;
Write(RomAddress>>1,temp);
RomAddress++;
RomAddress++;
Number--;
Number--;
}
if(Number)WriteChar(RomAddress,*RamAddress);
}