We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi , The following code works fine while reading the contents from the EEPROM (24C04) but does not continuously writes to EEPROM. Please suggest the correction in this code. #include <REG52.H> #include <stdio.h> #include <intrins.h> sbit SDA = P2^0; sbit SCL = P2^1; #define uchar unsigned char #define uint unsigned int #define Byte unsigned char #define Word unsigned int #define bool bit #define true 1 #define false 0 #define SomeNOP(); _nop_();_nop_();_nop_();_nop_(); void I2CStart(void); void I2CStop(void); void WaitAck(void); void SendAck(void); void SendNotAck(void); void I2CSendByte(Byte); Byte I2CReceiveByte(void); void contr(uchar); void contw(uchar,uchar); void main(void) { unsigned char count=0; PCON |= 0x80; // Set bit 7 of the PCON register (SMOD = 1) SCON = 0x50; // 0101,0000 (Mode 1 and RxD enable) TMOD |= 0x20; // Timer #1 in autoreload 8 bit mode TH1 = 0xFA; // Baud rate is determined by // Timer #1 overflow rate TR1 = 1; // Turn on Timer 1 TI = 1; contw(0,'P'); contw(1,'O'); while(1) { if(count==5) count=0; contr(count++); } } /**-------------------------------------------------------------------------------- void I2CStart(void) I2C ---------------------------------------------------------------------------------*/ void I2CStart(void) { SDA=1; SCL=1; SomeNOP();//INI SDA=0; SomeNOP(); //START SCL=0; SomeNOP(); } /**-------------------------------------------------------------------------------- void I2CStop(void) I2C ---------------------------------------------------------------------------------*/ void I2CStop(void) { SCL=0; SDA=0; SomeNOP(); //INI SCL=1; SomeNOP(); SDA=1; //STOP SomeNOP(); } /**-------------------------------------------------------------------------------- bit I2CAck(void) I2C ----------------------------------------------------------------------------------*/ void WaitAck(void) { SDA=1; SomeNOP(); SCL=1; while(SDA) SomeNOP(); SCL=0; SomeNOP(); } /**-------------------------------------------------------------------------------- void SendAck(void) I2C ---------------------------------------------------------------------------------*/ void SendAck(void) { SDA=0; SomeNOP(); SCL=1; SomeNOP(); SCL=0; } /**-------------------------------------------------------------------------------- void SendNotAck(void) I2C **--------------------------------------------------------------------------------*/ void SendNotAck(void) { SDA=1; SomeNOP(); SCL=1; SomeNOP(); SCL=0; } /**-------------------------------------------------------------------------------- void I2CSend(uchar ch) I2C ---------------------------------------------------------------------------------*/ void I2CSendByte(Byte ch) { uchar i=8; while (i--) { SCL=0; _nop_(); SDA=(bit)(ch&0x80); ch<<=1; SomeNOP(); SCL=1; SomeNOP(); } SCL=0; } /**-------------------------------------------------------------------------------- uchar I2CReceive(void) I2C ---------------------------------------------------------------------------------*/ Byte I2CReceiveByte(void) { uchar i=8; Byte ddata=0; SDA=1; while (i--) { ddata<<=1; SCL=0; SomeNOP(); SCL=1; SomeNOP(); ddata|=SDA; } SCL=0; return ddata; } /**-------------------------------------------------------------------------------- void contr(uchar) I2C Content Read ---------------------------------------------------------------------------------*/ void contr(uchar loc) { char i; I2CStart(); I2CSendByte(0xA2); WaitAck(); I2CSendByte(loc); WaitAck(); I2CStop(); I2CStart(); I2CSendByte(0xA3); WaitAck(); printf("%c",I2CReceiveByte()); SendNotAck(); I2CStop(); } /**-------------------------------------------------------------------------------- void contw(uchar,uchar) I2C Content Write *Error in function* ---------------------------------------------------------------------------------*/ void contw(uchar loc,uchar info) { I2CStart(); I2CSendByte(0xA2); WaitAck(); I2CSendByte(loc); WaitAck(); I2CSendByte(info); WaitAck(); I2CStop(); }
//24c01-24c16读写驱动程序, sbit a0=ACC^0; //定义ACC的位,利用ACC操作速度最快 sbit a1=ACC^1; sbit a2=ACC^2; sbit a3=ACC^3; sbit a4=ACC^4; sbit a5=ACC^5; sbit a6=ACC^6; sbit a7=ACC^7; void s24(void) { _nop_();scl=0;sda=1;scl=1;_nop_();sda=0;_nop_();scl=0; } void s240(void) { _nop_();scl0=0;sda0=1;scl0=1;_nop_();sda0=0;_nop_();scl0=0; } void p24(void) { sda=0;scl=1;_nop_();sda=1; } void p240(void) { sda0=0;scl0=1;_nop_();sda0=1; } unsigned char rd24(void) { sda=1; scl=1;a7=sda;scl=0; scl=1;a6=sda;scl=0; scl=1;a5=sda;scl=0; scl=1;a4=sda;scl=0; scl=1;a3=sda;scl=0; scl=1;a2=sda;scl=0; scl=1;a1=sda;scl=0; scl=1;a0=sda;scl=0; sda=1;scl=1;scl=0; return(ACC); } void wd24(unsigned char dd) { ACC=dd; sda=a7;scl=1;scl=0; sda=a6;scl=1;scl=0; sda=a5;scl=1;scl=0; sda=a4;scl=1;scl=0; sda=a3;scl=1;scl=0; sda=a2;scl=1;scl=0; sda=a1;scl=1;scl=0; sda=a0;scl=1;scl=0; sda=1;scl=1; } unsigned char read(unsigned int address){ unsigned char dd; dd=((address&0x7ff)/256)<<1; s24();wd24(0xa0|dd);scl=0;wd24(address);scl=0; s24();wd24(0xa1|dd);scl=0;dd=rd24();p24();return(dd); } void write(unsigned int address,unsigned char dd){ unsigned char ddd; ddd=((address&0x7ff)/256)<<1; s24();wd24(0xa0|ddd);scl=0;wd24(address);scl=0;wd24(dd);scl=0;p24(); time=0; //time为定时器时间参考,time增加1代表1ms,如果没有用定时器,取消该行 while (1) { s24(); wd24(0xa0|ddd); sda=1; if (sda==0) break; if (time>10) break; //此行防止由于eeprom器件损坏后的死循环 scl=0; } }