//函数声明
#include ”reg51.h”
#include ”intrins.h”
//管脚声明
sbit CS?_93=P1^0
sbit CLK?_93=P1^1
sbit DI?_93=P1^2
sbitDO_93=P1^3
sbit VCC?_93=P1^4
#define READ_c 0x02
#define WRITE_c 0x01
#define RRASE_c 0x03
#define RWEN_c 0x00
#define RWDS_c 0x00
#define RRAL_c 0x00
#defineWRAL_c 0x00
void delay(unsigned int N)
{
int i;
for(i=0;i<N;i++)
}
void SEND_1(void)
{
DI_93=1;
CLK_93=1;
_nop_();
CLK_93=0;
_nop_();
}
void SEND_0(void)
{
DI_93=0;
CLK_93=1;
_nop_();
CLK_93=0;
_nop_();
}
void start_operation(unsigned char operation_code)
{
CS_93=1;
SEND_1();
if (operation_code&0x02)
SEND_1();
else
SEND_0();
if (operation_code&0x01)
SEND_1();
else
SEND_0();
}
void SEND_BYTE(unsigned char dd)
{
unsigned char i;
for (i=0;i<8;i++)
{
if (dd>=0x80)
SEND_1();
else
SEND_0();
dd=dd<<1
}
}
unsigned char RECEIVE_BYTE(void)
{
unsigned char i,dd;
DO_93=1;
for (i=0;i<8;i++)
{
dd<<1;
CLK_93=1;
_nop_();
CLK_93=0;
_nop_();
if (DO_93)
dd!=1;
}
return(dd)
}
void EWEN(void)
{
_nop_();
CS_93=1;
start_operation(EWEN_c);
SEND_BYTE(0xC0);
CS_93=0;
}
void EWDS(void)
{
_nop_();
CS_93=1;
start_operation(EWDS_c);
SEND_BYTE(0x00);
CS_93=0;
}
void ERAL(void)
{
_nop_();
CS_93=1;
start_operation(ERAL_c);
SEND_BYTE(0x80);
CS_93=0;
}
unsigned char READ_BYTE(unsigned char address)
{
unsigned char dat;
CS_93=CLK_93=0;
CS_93=1;
start_operation(READ_c)
SEND_BYTE(address)
dat=RECEIVE_BYTE();
CS_93=0;
}
bit WRITE_BYTE(unsigned char address,unsigned char dat)
{
unsigned char e,time,temp=address;
e=0;
while(e<3)
{
CS_93=CLK_93=0;
CS_93=1;
EWEN();
_nop_();
CS_93=1;
_nop_();
start_operation(WRITE_c)
SEND_BYTE(address);
SEND_BYTE(dat);
CS_93=0;
_nop_();
CS_93=1;
time=0;
DI_93=1;
while(1)
{
if (DO_93==1)
break;
if (time>20)
break;
time++;
}
CS_93=0;
if (READ_BYTE(temp)==dat)
{
return(0)
}
e++
}
return(1);
}