Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Explain me this PIC16f887 I2C eeprom code

Status
Not open for further replies.

tahir4awan

Full Member level 4
Joined
Nov 29, 2010
Messages
208
Helped
16
Reputation
32
Reaction score
17
Trophy points
1,308
Location
Germany
Activity points
3,084
Hi can anyone explain me this simple code
============================================================

void main(){
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
PORTB = 0;
TRISB = 0; // Configure PORTB as output

I2C1_Init(100000); // initialize I2C communication
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xA2); // send byte via I2C (device address + W)
I2C1_Wr(2); // send byte (address of EEPROM location)
I2C1_Wr(0xAA); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal

Delay_100ms();

I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xA2); // send byte via I2C (device address + W)
I2C1_Wr(2); // send byte (data address)
I2C1_Repeated_Start(); // issue I2C signal repeated start
I2C1_Wr(0xA3); // send byte (device address + R)
PORTB = I2C1_Rd(0u); // Read the data (NO acknowledge)
I2C1_Stop(); // issue I2C stop signal
}


===========================================================
 

Re: Pic16f887 i2c eeprom

Could be more precise concerning what is your doubt ?
 

Re: Pic16f887 i2c eeprom

yes in builtin EEPROM we simply send data with address and also write data with address.
But in external EEPROM

first what is the purpose of value in I2C1_Init(100000);

second from where can know device address I2C1_Wr(0xA2);

third why the write operation is repeated after delay and why the device address is changed from A2 to A3

I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xA2); // send byte via I2C (device address + W)
I2C1_Wr(2); // send byte (data address)
I2C1_Repeated_Start(); // issue I2C signal repeated start
I2C1_Wr(0xA3); // send byte (device address + R)

and last how can be data read without address of the EEPROM location I2C1_Rd(0u);
 

Re: Pic16f887 i2c eeprom

me, only familiar with picc not microc and ccs. So i am guessing that the 100000 may be the clock sped of i2c. The device address you may know from the datasheet of eeprom and the device reading need that incrimantation. You may refer datasheet
 
Re: Pic16f887 i2c eeprom

tahir

I guess is missing you first read some I2C EEPROM datasheet.
There you can find answer for all that questions.
I feel it´s better than we explain punctually informations sometimes not so deep like a device documentation.

+++
 

Re: Pic16f887 i2c eeprom

Guys I did every thing but still it is not working.

void main(){
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
PORTB = 0;
TRISB = 0; // Configure PORTB as output

I2C1_Init(100000); // initialize I2C communication
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0x02); // send byte via I2C (device address + W)
I2C1_Wr(2); // send byte (address of EEPROM location)
I2C1_Wr(0xF0); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal

Delay_100ms();

I2C1_Start(); // issue I2C start signal
I2C1_Wr(0x02); // send byte via I2C (device address + W)
I2C1_Wr(2); // send byte (data address)
I2C1_Repeated_Start(); // issue I2C signal repeated start
I2C1_Wr(0x03); // send byte (device address + R)
PORTB = I2C1_Rd(0u); // Read the data (NO acknowledge)
I2C1_Stop(); // issue I2C stop signal
}

------------------------------------------------------------------------------------------------------------------------


See the picture of EEPROM and read its device address and chip address. I changed the code according to theses values but still not working.
Still I didn't understand two things why the read operation is repeated and why EEPROM byte address is simply written as 2 why not in HEX ? I2C1_Wr(2);

Please do answer me or you can send me a simple code of read/write operation of EEPROM
 

hoe!
Why you changed from address 0xA2 to 0x02 it will never work!

Commonly all the i2c eeproms have a default adress of 0b1010xxxs
I think on your example the phisical address (A2,A1,A0 pins on the eeprom chip) is 001 so your address is 0b1010001s where s is the read/write bit... So
The address for write is 0b10100010 or 0xA2 and the address for read is 0x10100011 or 0xA3


Remember these are the i2c addresses... Not the internal eemprom address... After you issue the i2c address (and get an acknowledge) you set the internal address and then the data byte you want to write...
To read, you issue the read address and it will read the last address pointed... So you must issue a dummy write just to set the internal address pointer and the issue a read command...

So.. Read the i2c spec... Its all there....
 

Re: Pic16f887 i2c eeprom

Yes I know the default address is 1010 but see the picture I attached the address written is 0000
 

Re: Pic16f887 i2c eeprom

mmmm i dont know which software you're using but i think the address is still 0xA2 the 0000 i think is the internal address pointer... The 1 is the external i2c address pins...
 

Re: Pic16f887 i2c eeprom

Things are becoming even more complicated. After tried every thing I didn't succeed but then I used software I2C library routines and it worked but with abnormal result.

The example code given in MikroC was for 1 byte address EEPROM but in REAL PIC SIMULATOR the EEPROM 24C64 has 1 word address. see the picture below.



so I edited the code like this

-------------------------------------------------------------------------------------------------------------------------
// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections
void main()
{
delay_ms(500);
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // control byte
Soft_I2C_Write(0x00); // address high byte
Soft_I2C_Write(0x20); // address low byte
Soft_I2C_Stop();
}
-------------------------------------------------------------------------------------------------------------------------
note that I didn't write EEPROM data because the code is working fine wothout data and is pointing to memory location 0020 see the picture below




but whenever I insert the data the result becomes abnormal

-------------------------------------------------------------------------------------------------------------------------
// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections
void main()
{
delay_ms(500);
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // control byte
Soft_I2C_Write(0x00); // address high byte
Soft_I2C_Write(0x20); // address low byte
Soft_I2C_Write(0xAA); // data byte
Soft_I2C_Stop();
}
-------------------------------------------------------------------------------------------------------------------------
see the result below




I don't know what to do I tried every thing like convert address to 1 byte but nothing.
I prefer you please check code on REAL PIC SIMULATOR.
 

Re: Pic16f887 i2c eeprom

Come on guys I have posted my problem in detail. Is there no one who can help me?
 

Re: Pic16f887 i2c eeprom

i saw several problems with that simulator. Use proteus.
I am not familiar with microc in picc i written the code and worked fine
 
Re: Pic16f887 i2c eeprom

If a program is working in one simulator and it is giving error on other simulator then what will be the chances the program will work in real time.
 

Re: Pic16f887 i2c eeprom

if the simulator program in pc may work in different way. I tested the code in the hardware.
do not trust the simulator perfectly, do it in hardware and test. It is good and correct way. the simulator gives you ideal condition, but not in realtime
 
Last edited:
Re: Pic16f887 i2c eeprom

NEVER trust the simulators. Every software has some bugs, no software is bug free, specially nowdays when the software is depending on different external libraries, frameworks,...
You should test your uC software on real hardware (custom made for your application, or kind of developping system - EasyPic6, for example) have a separate programmer that you can use to read the SEEPROM,...

Usually, i use EasyPic6 as base development platform, PicKit2 for SEEPROMs and final uC programming in the target system that can be, by means of ports used, a lot different from development system.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top