//x25045
sbit CS=P1^0; //
sbit SO=P1^1; //
sbit SI=P1^2; //
sbit SCK=P1^3; //
void OutByte(unsigned char x){
unsigned char i;
ACC=x;
for(i=0;i <8;i++){SCK=0;ACC < <=1;SI=CY;SCK=1;}
}
unsigned char InputByte(){
unsigned char i;
for(i=0;i <8;i++){SCK=1;SCK=0;_nop_();ACC < <=1;CY=SO;ACC_0=CY;}
return(ACC);
}
void SetWatchDog(){
SCK=0;CS=0;
OutByte(0x01); //select status register
OutByte(0x10); //set watch dog 600ms,No block protect
CS=1;
}
void WriteDataToE2prom(unsigned char address,unsigned char x){
unsigned char i;
Reset_WatchDog();
SCK=0;CS=0;OutByte(0x06);CS=1; //select write status register,enable write data to the chip
SCK=0;CS=0;OutByte(0x02); //write instruction
OutByte(address); //send start address of the data want to write to chip
OutByte(x); //data
SCK=0;CS=1; //clock
for(i=0;i <0x99;i++){SCK=0;CS=0;OutByte(0x05);ACC=InputByte();
if(!ACC_0){i=0x98;} //wait for chip write data finish
}
} //after write data to chip must run'SCk=0;CS=1;'
void ReadDataFromE2prom(unsigned char address,unsigned char x){//x must less than 9
unsigned char i;
Reset_WatchDog();
SCK=0;CS=0;OutByte(0x03); //enable read data from chip
OutByte(address); //start address
for(i=0;i <x;i++){X25045Temp_Buff[i]=InputByte();}
SCK=0;CS=1;
}
void Store_Data_25045(){
unsigned char i;
Reset_WatchDog();
for(i=0;i <4;i++){WriteDataToE2prom(i+0x10,LampvalueBuff[i]);}
SCK=0;CS=1;
} //store data
void Read_Data_25045(){
unsigned char i;
Reset_WatchDog();
ReadDataFromE2prom(0x10,0x04);
for(i=0;i <4;i++){LampvalueBuff[i]=X25045Temp_Buff[i];}
}
void Store_LED_Status(){
Reset_WatchDog();
WriteDataToE2prom(0x14,LampvalueBuff[S_TH]);
WriteDataToE2prom(0x15,LampvalueBuff[S_TL]);
SCK=0;CS=1;
}
void Read_LED_Status(){
Reset_WatchDog();
SCK=0;CS=0;OutByte(0x03); //enable read data from chip
OutByte(0x14); //start address
LampvalueBuff[S_TH]=InputByte();
LampvalueBuff[S_TL]=InputByte();
SCK=0;CS=1;
}
void Store_Password(){
Reset_WatchDog();
WriteDataToE2prom(0x80,0x59);
WriteDataToE2prom(0x81,0x44);
}
void Read_Password(){
Reset_WatchDog();
SCK=0;CS=0;OutByte(0x03); //enable read data from chip
OutByte(0x80); //start address
PasswordTH=InputByte();
PasswordTL=InputByte();
SCK=0;CS=1;
}
发表时间:2003年4月15日15:09:15