gyb006 发表于 2007-3-11 21:13:43

请教马老师有关EEPROM的程序问题

这个程序我已经研究很长时间了,但是就是无法读写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 lowbyte

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 lowbyte

delay_us(4);

CLR_EEPROM_CS ;

delay_us(4);

SET_EEPROM_CS ;

delay_us(4);

}

machao 发表于 2007-3-13 16:35:53

用AVR的话,我提倡用AVR的TWI硬件口,不提倡用I/O模拟。



解决方案:



1。参考《M128》一书第五章中的内容(练手、学习)

2。采用带I2C标准库的开发平台,如CVAVR(I/O口模拟方式的),只要在你的系统能用,能节省时间。

zbazba 发表于 2011-1-20 13:27:25

TWII2C好像不能读写93C86吧。
页: [1]
查看完整版本: 请教马老师有关EEPROM的程序问题