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.

I2C example pic18f24k50 with mikroC

Status
Not open for further replies.

cllunlu

Member level 4
Joined
Nov 21, 2006
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,900
Hi Everyone,

I wanna i2c communication between pic18f24k50 and at24c64. I have looked at i2c example in mikroC library manager. But I couldnt understand it. Especially read and write functions. Anyone Show some simple example.

A0,A1 and A2 pin is grounded.

Thank you...
 

embRTS

Full Member level 4
Joined
Sep 8, 2013
Messages
216
Helped
36
Reputation
72
Reaction score
35
Trophy points
28
Location
Bangalore
Activity points
1,209
Hello,
So as per the details you provided I assume that you are using MikroC compiler

So I will tell you the general idea

1. As you have mentioned the pins are grounded so the bits(A0, A1 andA2) will be ZERO in the 7 bit address of at24c64. (please refer datasheet)

2. Now the you start like this

HTML:
// FOR WRITING DATA to Slave 

1.>Start I2C Communication
2.> Send I2C 7 bit address along with LSB (0th bit) as ZERO [IF you want to write Something LSB has to be Zero]
3.> Write the Data 
4.> Stop I2C Communication

HTML:
// FOR READING
1.>Start I2C Communication
2.> Send I2C 7 bit address along with LSB (0th bit) as Logic - ONE [IF you want to READ Something LSB has to be ONE]
3.> Restart I2C Communication OR Stop and Start I2C communication
4.> Read Data 
5.> Stop I2C Communication

Try to implement this using the library functions(I2C) provided by Mikro C and let me know if you face any difficulty
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
mikroC PRO PIC comes with an example of I2C eeprom. Use it. It works for me.

- - - Updated - - -

Edit:
Code:
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)
LATB = I2C1_Rd(0u);        // Read the data (NO acknowledge)
I2C1_Stop();               // issue I2C stop signal

I2C1_Init(100000);

//I2C1 communication is initialized at 100 kbps baudrate
Code:
I2C1_Start();

//I2C1 communication is started, only master controls the I2C clock
Code:
I2C1_Wr(0xA2);

//The address byte for AT24LC64 is formed like this

bits 7 to bits 4 will be 1010. This is the 4bit control code for read or write.

bits 3 to 1 for the actual address set on the eeprom chip. Here it is 001

bit 0 is Read/Write bit.

Here we are writing to eeprom so bit 0 is 0 and so the address byte becomes

1010 001 0 = 10100010 = 0xA2

Code:
I2C1_Wr(2);

Next he is sending address of eeprom location (2).

So the data that will be sent next will be written at eeprom address 0x02
Code:
I2C1_Wr(0xAA);

Now data 0xAA is written at address 0x02 of eeprom
Code:
I2C1_Stop();

I2C communication is stopped.


Now reading data from eeprom and displaying the read byte on PORTB.

These three lines are already explained

Code:
I2C1_Start();              // issue I2C start signal
I2C1_Wr(0xA2);             // send byte via I2C  (device address + W)
I2C1_Wr(2);                // send byte (data address)
Now a repeated start signal is issued

Code:
I2C1_Repeated_Start();



Now read address has to be sent so in the address byte the bit 0 will be 1

so the address byte will be

1010 001 1 = 10100011 = 0xA3

Read address is sent.

Code:
I2C1_Wr(0xA3);

eeprom sends data at address 0x02.

master reads the data and does a not ack.

Code:
LATB = I2C1_Rd(0u);

I2C communication is stopped

Code:
I2C1_Stop();
 

cllunlu

Member level 4
Joined
Nov 21, 2006
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,900
I wanna use at24c64 eeprom. The address is 0x0000 to 0x1FF. When I send to the address to eeprom, How do I write. For example first hi part, after that low part?
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Yes, you have to send Address high byte first and then Address Low Byte and then data to be written to eeprom.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top