|
这个程序我已经研究很长时间了,但是就是无法读写93C86
程序如下
#define EEPROM_DO (PINB&0x10)
#define SET_EEPROM_CS (PORTC=PORTC|0x80)
#define CLR_EEPROM_CS (PORTC=PORTC&0x7F)
#define SET_EEPROM_CLK (PORTC=PORTC|0x40)
#define CLR_EEPROM_CLK (PORTC=PORTC&0xBF)
#define SET_EEPROM_DI (PORTC=PORTC|0x20)
#define CLR_EEPROM_DI (PORTC=PORTC&0xDF)
void high86(void)
{
SET_EEPROM_DI ;
delay_us(4);
SET_EEPROM_CLK ;
delay_us(8);
CLR_EEPROM_CLK ;
delay_us(8);
}
void low86(void)
{
CLR_EEPROM_DI ;
delay_us(4);
SET_EEPROM_CLK ;
delay_us(8);
CLR_EEPROM_CLK ;
delay_us(8);
}
void wd86(uchar ucDa)
{
uchar i;
for (i=0;i<8;i++)
{
if(ucDa>=0x80)
high86();
else
low86();
ucDa=ucDa<<1;
}
}
uchar rd86(void)
{
uchar i,ucDa;
for (i=0;i<8;i++)
{
ucDa<<=1;
delay_us(4);
SET_EEPROM_CLK ;
delay_us(8);
CLR_EEPROM_CLK ;
delay_us(8);
if (EEPROM_DO)
ucDa |= 1;
}
return(ucDa);
}
void ewen86(void)
{
delay_us(4);
SET_EEPROM_CS ;
delay_us(4);
high86();
low86();
low86();
high86();
high86();
wd86(0xff);
delay_us(4);
CLR_EEPROM_CS;
delay_us(4);
}
uint read93lc86_word(uint address)
{
uint uiDa; //uiDa = ucDa1<<8 + ucDa0
uchar ucDa0,ucDa1;//ucDa1 high byte
//ucDa0 low byte
delay_us(4);
CLR_EEPROM_CS ;
CLR_EEPROM_CLK ;
delay_us(4);
SET_EEPROM_CS ;
delay_us(4);
high86();//110
high86();
low86();
if((address&0x200)==0x200)
high86();
else
low86();
if((address&0x100)==0x100)
high86();
else
low86();
wd86(address);//wd86(uchar ucDa);
ucDa1=rd86();
ucDa0=rd86();
delay_us(4);
CLR_EEPROM_CS ;
delay_us(4);
uiDa=ucDa1*256+ucDa0;
return(uiDa);
}
void write93lc86_word(uint address,uint uiDa)
{
CLR_EEPROM_CS ;
delay_us(4);
CLR_EEPROM_CLK ;
delay_us(4);
SET_EEPROM_CS ;
delay_us(4);
ewen86();
delay_us(4);
SET_EEPROM_CS ;
delay_us(8);
high86();
low86();
high86();
if((address&0x200)==0x200)
high86();
else
low86();
if((address&0x100)==0x100)
high86();
else low86();
wd86(address);
wd86(uiDa/256);//write high byte
wd86(uiDa%256);//write low byte
delay_us(4);
CLR_EEPROM_CS ;
delay_us(4);
SET_EEPROM_CS ;
delay_us(4);
} |
|