PIC 18 I2C — External EEPROM
Interfacing external EEPROM with PIC Microcontroller — I2C — Episode # 01
Hi, this is a Hello World for I2C Protocol.
We’re gonna use an external chip, Serial 24C04 EEPROM Memory, write a byte into it, via I2C Protocol, capture its value back, and present it to LEDs attached to the PIC18F4520’s PORTD :)
During this episode, we’re gonna use a Saleae Logic Analyzer Software by reading RC3 and RC4 pins with a Chinese & cheap clone Logic Analyzer 24 Mhz8CH :/
All right, the above review contains spoilers, right? Let’s make a lab!
Material & Software Needed:
1 x PIC development board: EasyPIC™ v7 - Chip: PIC18F45k22, or EasyPIC™ v8* - Chip: PIC18F47k42, or PICGENIOS* - Chip: PIC18F4520;1 x USB Logic Analyzer 24M 8CH Microcontrolador ARM FPGA Debug Tool;1 x Saleae Logic Analyzer Software;1 x Serial 24C04 EEPROM Memory;1 x Microchip’s PICkit™ 2* -> used here therein! Download: or Microchip’s PICkit™ 3 or the new MPLAB PICkit™ 4 or mikroProg suite for PIC;1 x mikroC PRO for PIC IDE; and...* x It took a lot of patience;)
Here Goes the Step-by-step so you can replicate this lab by yourself:
1º Step — As for the usability of this cheap USB Logic Analyzer 24M 8CH, please consider to check this post:
Binary Counter — Logic Analyzer — Build an LED Binary Counter & Improve The Code — ArduSeries #88
2º Step — As soon as you get this working, open mikroc PRO for PIC, run a new project and paste this code:
3º Step — Connect RC3 to Logic Analyser’s CH1, RC4 to CH2 and GND to PIC’s GND;
4º Step — Run Saleae Logic Analyzer Software;
5º Step —Run mikroC PRO for PIC IDE; copy & paste the code above; build it; compile it; use PicKit 2 to burn .hex on PIC18F4520 chip;
6º Step —Now, immediately after you click Start Simulate (1second) at Saleae Logic Analyzer Software, press the RESET button at your development PIC board; Here is the configuration that worked for me;
7º Step — Now you must capture two packets (Frames) sent by the code above; separated by .1s;
8º Step — Let’s analyze the first frame;
The first frame is the Writing to a Slave on the I 2C bus, the master will send a start condition (A)
on the bus with the slave’s address, as well as the last bit (the R/W bit) set to 0, which signifies a write (B).
After the slave sends the acknowledge bit (C),
the master will then send the register address (D)
of the register it wishes to write to. The slave will acknowledge again(E),
letting the master know it is ready. After this, the master will start sending the register data to the slave(F),
the slave acknowledge after each byte (G)
until the master has sent all the data it needs to (sometimes this is only a single byte), and the master will terminate the transmission with a STOP condition(H).
9º Step — Let’s analyze the second frame;
Reading from a slave is very similar to writing, but with some extra steps. In order to read from a slave, the master must first instruct the slave which register it wishes to read from.
This is done by the master starting off the transmission in a similar fashion as the write (A),
by sending the address (B)
with the R/W bit equal to 0 (signifying a write)(C),
followed by the register address it wishes to read from (D).
Once the slave acknowledges this register address (E),
the master will send a START condition again(F),
followed by the slave address (G)
with the R/W bit set to 1 (signifying a read) (H).
This time, the slave will acknowledge the read request(I),
and the master releases the SDA bus but will continue supplying the clock to the slave. During this part of the transaction, the master will become the master-receiver, and the slave will become the slave-transmitter. The master will continue sending out the clock pulses but will release the SDA line so that the slave can transmit data. At the end of every byte of data, the master will send an ACK to the slave, letting the slave know that it is ready for more data. Once the master has received the number of bytes it is expecting, it will send a NACK (J),
signaling to the slave to halt communications and release the bus. The master will follow this up with a STOP condition (K).
And that is all for this I2C Episode.
In the next episode, we’re gonna implement our own I2C library. Be tuned!
Bye o/
Credits & References
Interfacing External EEPROM with PIC18f4550 — I2C Based
https://pic-microcontroller.com/interfacing-external-eeprom-pic-microcontroller/
Download All Files For This Project