I am looking for example(s) on writing/reading EEPROMs (93c46) using 89c51 in C. Any help shall be higly appreciated. Mansoor
void chngestate(unsigned char command) {unsigned char i; cs=0;//chip select Din=0;//In CLK=0;//Clock CLK=0; cs=1; Din=1; CLK=1; CLK=0; for (i=0;i<7;i++) { if (command&0x80) Din=1; else Din=0; CLK=1; command<<=1; CLK=0; } buf<<=1; if (command&0x80) Din=1; else Din=0; CLK=1; cs=0; _nop_(); _nop_(); } int eeread(unsigned char address) { unsigned int result=0; unsigned int c=0x8000; unsigned char i,buf; buf=address&(0xbf)|(0x80); cs=0; Din=0; CLK=0; CLK=0; cs=1; Din=1; CLK=1; CLK=0; for (i=0;i<8;i++) { if (buf&0x80) Din=1; else Din=0; CLK=1; buf<<=1; CLK=0; } Din=0; //16 bit read msb first for (i=0;i<16;i++) { CLK=1; if (Dout) result|=c;//out CLK=0; c>>=1; } CLK=1; cs=0; Din=0; return(result); } void eewrite(unsigned int dat,unsigned char address) { unsigned int buf1=dat; unsigned char i,buf; buf=address&(0x7f)|(0x40); cs=0; Din=0; CLK=0; cs=1; CLK=0; Din=1; CLK=1; CLK=0; for (i=0;i<8;i++) { if (buf&0x80) Din=1; else Din=0; CLK=1; buf<<=1; CLK=0; } //send 16 bit data msb first for (i=0;i<15;i++) { if (buf1&0x8000) Din=1; else Din=0; CLK=1; buf1<<=1; CLK=0; } if (buf1&0x8000) Din=1; else Din=0; CLK=1; Din=0; cs=0; _nop_(); _nop_(); cs=1; _nop_(); _nop_(); while (Dout!=1); cs=0; _nop_(); _nop_(); }
Thanks for the reply. What about 8 bit mode?
Hi Mansoor, If you don't care about the overhead, I think you can use the following method to implement your own 8-bit read/write.
#define S_CONST_EEPROM_START_ADDR 0x01 // 0x01 ~ 0x3f #define GET_HIGH(xx) ( (BYTE) ((xx) >> 8) ) #define GET_LOW(xx) ( (BYTE) ((xx) & 0x00ff) ) ////////////////////////////////////////////////////////////////////////////// // Function name : EepromWriteOneByte // Description : Eeprom Write One Byte through Driver // Return Value : // [#05] BYTE EepromWriteOneByte(BYTE pos, BYTE byteValue) { BYTE cAdr, cResult; WORD sData, sDataOut; // write out a word cAdr = S_CONST_EEPROM_START_ADDR + pos / 2; vWriteDisable(); sDataOut = sReadWord(cAdr); // Address from 0x00 to 0x3F if (pos % 2) sData = (GET_HIGH(sDataOut)<<8) | byteValue; // odd else sData = GET_LOW(sDataOut) | (byteValue << 8); // even vWriteEn(); cWriteWord(cAdr, sData); vWriteDisable(); cResult = EepromReadOneByte(pos); return cResult; } ////////////////////////////////////////////////////////////////////////////// // Function name : EepromReadOneByte // Description : Eeprom Read One Byte through Driver // Return Value : // [#05] BYTE EepromReadOneByte(BYTE pos) { BYTE cAdr, cResult; WORD sDataOut; // read back check cAdr = S_CONST_EEPROM_START_ADDR + pos / 2; vWriteDisable(); sDataOut = sReadWord(cAdr); // Address from 0x00 to 0x3F if (pos % 2) cResult = GET_LOW(sDataOut); // odd else cResult = GET_HIGH(sDataOut); // even return cResult; }
What do you mean by overheads. Thanks for your efforts in answering my query. Mansoor
I have just completed the project. Thanks for all who helped. I have tried to develop generalize routines for reading/writing in 8 bits. Any body interested,Pl. contact me on mansoork38@hotmail.com. Mansoor