Joined: 31 Mar 2004 Posts: 23 Location: Switzerland
Help please!! Programing EEPROM with PIC18F452
Posted: Wed Mar 31, 2004 7:04 am
hello
I'm beginner with PICs. I want to write and read to an EEPROM memory (24LC256) via I2C Bus.
I'm looking for a code example.
My HW:
PICDEM 2 Plus
MPLAB ICD 2
CCS Compiler (PCH and PCM)
PIC 18F452
Thanks, Pablo[/b]
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
Posted: Wed Mar 31, 2004 1:04 pm
First, make sure you read through the data sheet for the eeprom. It will tell you just how you are supposed to write to and read from the eeprom. I've used that same combination so here is a small bit of code that might be helpful.
output_low(wp);// write enable the eeprom
delay_us(15);
i2c_start();// send start signal
i2c_write(0xA0);// address the eeprom
add = (address >> 8) & 0xFF;
i2c_write(add);// send the MSB of the memory location
add = address & 0xFF;
i2c_write(add);// send the LSB of the memory location
i2c_write(value);// write the data
i2c_stop();// stop i2c communications
output_high(wp);// write disable the eeprom
i2c_start();
i2c_write(0xA0);// address the eeprom(write mode)
add = (address >> 8) & 0xFF;
i2c_write(add);// send the MSB of the memory location you want to read
add = address & 0xFF;
i2c_write(add);// send the LSB of the memory location you want to read
i2c_start();
i2c_write(0xA1);// address the eeprom(read mode)
clipboard = i2c_read(0);// read data from the eeprom
i2c_stop();
return(clipboard);
}// end of eeread()
If you want to write more than just one byte to the eeprom, you can have succesive i2c_write's, before the i2c_stop, and the eeprom will automatically increment it's address to store the data. The same goes for reads, also.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum