Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

[SOLVED] Explain me this PIC16f887 I2C eeprom code

Status
Not open for further replies.

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
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
}


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

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,181
Helped
1,140
Reputation
2,299
Reaction score
1,116
Trophy points
1,403
Location
Brazil
Activity points
53,323
Re: Pic16f887 i2c eeprom

Could be more precise concerning what is your doubt ?
 

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
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);
 

thunderboympm

Full Member level 5
Joined
Sep 17, 2007
Messages
244
Helped
32
Reputation
64
Reaction score
29
Trophy points
1,308
Location
Malappuram, India
Activity points
2,467
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
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,181
Helped
1,140
Reputation
2,299
Reaction score
1,116
Trophy points
1,403
Location
Brazil
Activity points
53,323
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.

+++
 

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
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
 

Kurenai_ryu

Advanced Member level 2
Joined
Jun 10, 2006
Messages
671
Helped
159
Reputation
316
Reaction score
99
Trophy points
1,308
Location
Bolivia
Activity points
5,913
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....
 

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
Re: Pic16f887 i2c eeprom

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

Kurenai_ryu

Advanced Member level 2
Joined
Jun 10, 2006
Messages
671
Helped
159
Reputation
316
Reaction score
99
Trophy points
1,308
Location
Bolivia
Activity points
5,913
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...
 

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
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.
 

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
Re: Pic16f887 i2c eeprom

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

thunderboympm

Full Member level 5
Joined
Sep 17, 2007
Messages
244
Helped
32
Reputation
64
Reaction score
29
Trophy points
1,308
Location
Malappuram, India
Activity points
2,467
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
 

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
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.
 

thunderboympm

Full Member level 5
Joined
Sep 17, 2007
Messages
244
Helped
32
Reputation
64
Reaction score
29
Trophy points
1,308
Location
Malappuram, India
Activity points
2,467
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:

ZASto

Advanced Member level 3
Joined
Jan 9, 2008
Messages
720
Helped
143
Reputation
289
Reaction score
127
Trophy points
1,323
Location
Messy Corner, 44°47'31.56"N, 20°28'9.16"E
Activity points
4,858
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.
 

tahir4awan

Full Member level 3
Joined
Nov 29, 2010
Messages
183
Helped
16
Reputation
32
Reaction score
16
Trophy points
1,308
Activity points
2,858
Re: Pic16f887 i2c eeprom

Thanks for your help.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top