Hi, i am trying to interface 89c51 with a EEPROM (SPI)(25aa080a). i written the code but it's not working.
the code is below. if there is any mistakes please correct me.
i am trying to interface it in proteus.
thank you.
#include <REGX51.H> #include <intrins.H> #define nop _nop_();_nop_();_nop_();_nop_(); sbit sck = P2^0; sbit swr = P2^1; sbit srd = P2^2; sbit cs = P2^3; unsigned char bdata wdata; //sfr wdata = 0xa0; sbit shift7 = wdata^7; void msdelay(unsigned int ms) { int i,j; for(i=0;i<=ms;i++) for(j=0;j<=122;j++); } void start() { cs = 1; nop; cs = 0; } void stop() { cs = 0; nop; cs = 1; } void write(char wdata) { unsigned char bit_count = 0; while(bit_count<8) { swr = shift7; sck=0; _nop_(); sck=1; _nop_(); wdata = wdata<<1; bit_count++; } } void main() { start(); write(0x06); // write enable command msdelay(10); stop(); msdelay(10); start(); write(0x01); // write enable command msdelay(10); write(0x00); // write enable command msdelay(10); stop(); msdelay(10); start(); write(0x06); // write enable command msdelay(10); stop(); msdelay(10); start(); write(0x02); // write command msdelay(10); write(0x00); // high byte address msdelay(10); stop(); msdelay(10); start(); write(0x02); msdelay(10); write(0x00); msdelay(10); write(0x40); // low byte address msdelay(10); write(0x3f); // data to write in spi eeprom msdelay(10); stop(); while(1); }
Hi,
sbits are used to bit-address sfrs..
So i guess the problem is here:
while(bit_count<8) { swr = shift7; sck=0; _nop_(); sck=1; _nop_(); wdata = wdata<<1; bit_count++; } for (bit_count = 0; bit_count < 8; bit_count++) { swr = ((wdata & 0x80) > 0) ? 1 : 0; sck = 0; _nop_(); sck = 1; wdata = wdata << 1; }
BTW: You should also tell us WHAT does not work, not just saying IT doesn't work...
Christian.
Sorry, I forgot a _nop_() after sck = 1;
C.
I don't think shift7 is doing what you think it's doing.
ND
Thank you Christian for the reply.
the problem is, i just started to learn the debugging in keil. so i used the proteus to simulate, after writing the code, i run the simulation. when i checked the eeprom's memory nothing is there.
i learned, there is lot of mistakes. the main one, i think that i missed to write the status register.
now i can communicate with spi eeprom.
i corrected my code as you said. but still i don't know why my code is not working, I used the bit addressable bdata.
Thank you.
maybe this helps...
http://www.keil.com/support/docs/1863.htm
Thank you again Christian,
i learned about bdata.