PIC 18 I2C — External EEPROM

Interfacing external EEPROM with PIC Microcontroller — I2C — Episode # 01

J3
Jungletronics
5 min readDec 16, 2019

--

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 :)

Fig 1. My memory is an ATWLH312 24CWB specs:)

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 :/

Fig 2. USB Logic Analyzer 24M 8CH Microcontrolador ARM FPGA Debug Tool
Fig 3. shows an example of writing a single byte to a slave register. We are using Saleae App — You can download this sample and run it even with the app disconnected — try it!— Here’s the file:) — see above there are three operations on this I2C writer frame: a) Master sends 0xA2, which is the same as 0b1010 0010 — 7-bits chip address + 0, indicating the master wanna write to & immediate response ACK from the slave; b) the master sends an address 2, which is the address it wants to write to, and c) the data (0xAA) sent so slave appends to that address and write it to a non-volatile storage — external EEPROM— This data, later, will be rescued via software to the LED’s attached on PORTD — see code below;)

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;

Fig 4. Configuring the analyzer

7º Step — Now you must capture two packets (Frames) sent by the code above; separated by .1s;

Fig 5. Showing both frames captured

8º Step — Let’s analyze the first frame;

Fig 6. Frame 1: Please read the sequence below:)
Fig 7. Graph for writing procedure over slave

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.

Fig 8. Frame 2 explained o/
Fig 9. Reading procedure over slave

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

Fig 10. PORTB’s LEDs showing the data printed rescued from an external Serial 24C04 EEPROM Memory:)

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!