helo This is shashi shekhar. i have doing a cide for read and write from eeprom 24c04 by i2c for lpc2138.i have send slave address and write bit but there is no ack geting from slave.plz tell me where is fault.in my code display data on lcd my code is /*External EEPROM24C04 Read Write Test Program */
#include <LPC213X.H> #include "delay.h" #include "lcd.h" #include "i2c.h" #include "uart.h"
/**************Function Prototypes************/ void EEPROM_Write(unsigned char data,unsigned char address); unsigned char EEPROM_Read(unsigned char address);
void main(void) { unsigned char read_eeprom; InitUart0(); lcd_init(); I2C1_Init(); I2C1_Start(); lcd_cmd(0x01); lcd_cmd(0x80); lcd_string("shashi shekhar!"); delay(100); lcd_cmd(0xc0); lcd_string("I2C_EEPROM TEST!"); delay(100); lcd_cmd(0x01); lcd_cmd(0x80); lcd_string("Writing... A"); lcd_cmd(0xc0); delay(1000); delay(1000); lcd_string("Reading... A"); EEPROM_Write('A',0x00); //delay(1000); read_eeprom = EEPROM_Read(0x00); lcd_cmd(0x01); lcd_cmd(0x80); //delay(1000); lcd_string("Reading... "); lcd_data(read_eeprom); while(1);
}
/****************Function Definition**********/ void EEPROM_Write(unsigned char data,unsigned char address) /*Function To Write to the EEPROM*/ { I2C1_Init(); I2C1_Start(); I2C1_Write(0xA0,0x18); /*0xA0 is the slave address + write bit(0) and ACK has been received(0x18)*/ I2C1_Write(address,0x28); /*meory address 0x00 has been sent for writing the data on eeprom memory address 0x00,and ACK has been received(0x28)*/ I2C1_Write(data,0x28); /*data character 'A' has been send on address 0x00,and ACK has been received(0x28)*/ I2C1_Stop(); }
unsigned char EEPROM_Read(unsigned char address) /*Function To Read from EEPROM*/ { unsigned char read1; I2C1_Init(); I2C1_Start(); I2C1_Write(0xA0,0x18); I2C1_Write(address,0x28); I2C1_Stop(); I2C1_Start(); I2C1_Write(0xA1,0x40); /*0xA1 is the slave address + read bit(1),and ACK has been returned(0x40)*/ read1 = I2C1_Read(); I2C1_Stop(); return (read1); }
/****************Function Definition**********/ void I2C1_Init(void) /*OK*/ { PINSEL0|=0x30C00000; /*SCL->P0.14,SDA->0.11*/ IODIR0|=0x4800; I2C1CONCLR=0X6C; /*CLEAR ALL FLAG BITS IN I2C1CONCLR Register.*/ I2C1CONSET |= 0x40; /*I2C ENABLE.*/ I2C1SCLH = 0X004B; /*value->75*/ I2C1SCLL = 0X004B; /*value->75*/
void I2C1_Start(void) /*OK*/ { I2C1CONSET |= 0x20; /*Set the Start Bit of I2C1*/ //while(I2C1STAT!=0x08); /*Wait for the Status Set*/ while(!((I2C1CONSET & 0x08)==0x08));//checking for SI to go high while(!(I2C1STAT == 0x08)); I2C1CONCLR=0x08; /*clear the SI BIT */ } void I2C1_Stop(void) /*OK*/ { //I2C1CONCLR=0X08; // I2C1CONSET |= 0x14;
I2C1CONSET = 0X50; // sending stop bit by making STO high while(!((I2C1CONSET & 0x10)==0x10)); I2C1CONCLR = 0X08; }
void I2C1_Write(unsigned char data,unsigned char status) {
I2C1CONSET |= 0X04; /*set the AA bit in I2CONSET Register*/ I2C1DAT = data; delay(100); /*in Data byte data has been send for writing on eeprom*/ //I2C1CONCLR |= 0X28; /*clear the STA AND SI bit in I2CONCLR Register*/
while((I2C1STAT)!=status); /* Wait for Status Set */ I2C1CONCLR |= 0X28; //I2C1CONCLR = 0X08;
unsigned char I2C1_Read(void) { unsigned char read; I2C0CONSET = 0X04; read=I2C1DAT; while ((I2C1STAT)!=0x00000050); /* Wait for Status Set - 0x50 , Data byte has been received,ACK has been returned */ I2C1CONCLR = 0X28; return(read); }