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.
void I2C_Start(void) { SDA=HIGH; Delay(15); SCLK =HIGH; Delay(15); SDA = LOW; Delay(15); SCLK = LOW; Delay(15); }
void I2C_Stop(void) { SCLK=LOW; Delay(15); SDA=LOW; Delay(15); SCLK=HIGH; Delay(15); SDA=HIGH; }
void I2C_Write(unsigned char j) { for(i=0;i<8;i++) { SDA = ((j & 0x80) ? 1 : 0); SCLK = HIGH; LongDelay(18); SCLK = LOW; j<<=1; } SDA=HIGH; Delay(15); SCLK = HIGH; Delay(15); SCLK = LOW; Delay(15); }
unsigned char I2C_Read(void) { unsigned char i,j; SDA= HIGH; j = 0; for(i=0;i<8;i++) { j<<=1; SCLK = HIGH; Delay(50); j |= SDA; SCLK = LOW; } Delay(15); SDA = LOW; Delay(15); SCLK = HIGH; Delay(15); SCLK = LOW; Delay(15); SDA = HIGH; Delay(15); return j; }
unsigned char I2C_EEPROM_Rd(void) { unsigned char i,j; //this is the function working //for eeprom read.couldnt use //i2c_read function. SDA = HIGH; j = 0; for(i=0;i<8;i++) { SCLK = HIGH; j<<=1; j |= SDA; Delay(15); SCLK = LOW; } return j; }
void EEPROM_Location(unsigned int x) { ptr = x; }
void EEPROM_WrByte(unsigned char x) { I2C_Start(); //START I2C PROTOCOL I2C_Write(EEPROM_WR); //CTRL WORD FOR WRITE MODE I2C_Write((char)(ptr>>8)); //HIGH ADDRESS BYTE I2C_Write((char)ptr); //LOW ADDRESS BYTE I2C_Write(x); //WRITE THE DATA I2C_Stop(); ptr++; //INCREMENT THE ADDRESS LongDelay(15); //5.21MS DELAY return; }
unsigned char EEPROM_RdByte(void) { unsigned char x; I2C_Start(); I2C_Write(EEPROM_WR); //WRITE MODE I2C_Write((char)ptr>>8); //HIGH ADDRESS BYTE I2C_Write((char)ptr); //LOW ADDRESS BYTE I2C_Start(); I2C_Write(EEPROM_RD); //READ MODE x = I2C_EEPROM_Rd(); //READ THE VALUE I2C_Stop(); ptr++; //INCREMENT THE ADDRESS BY 1 return(x); }
void main(void) { unsigned char a,l,c; unsigned char x,y,z; TMOD = 0x20; TH1 = 0xFD; TL1 = 0x00; SCON = 0x50; TR1 = 0x01; a = 0x41; l = 0x42; c = 0x43; ptr = 0x0000; EEPROM_Location(ptr); EEPROM_WrByte(a); LongDelay(15); EEPROM_Location(ptr); EEPROM_WrByte(0x44); LongDelay(15); EEPROM_Location(ptr); EEPROM_WrByte(c); LongDelay(15); Delay(1000); ptr = 0x0000; EEPROM_Location(ptr); x = EEPROM_RdByte(); LongDelay(15); SBUF= ' '; printf("\n x is %bx",x); TI = 0; Delay(1000); EEPROM_Location(ptr); y = EEPROM_RdByte(); LongDelay(15); SBUF= ' '; printf("\n y is %bx",y); TI = 0; Delay(1000); EEPROM_Location(ptr); z = EEPROM_RdByte(); LongDelay(15); SBUF= ' '; printf("\n z is %bx",z); TI = 0; Delay(1000); while(1); }
The output of the above main function is, x is 41 y is 1 z is 43. I dont know why this problem is coming. This routine is already tested. But now this one is giving problem. Can anybody tell me what is the problem in my coding? do i need to change my coding? will there be any problem if RTC and Seial EEPROM share the same I2C bus?
Thank you.