PIC16F877 i2c code and Proteus simulation

This post provides the i2c code (using the i2c module built in the hardware[1]) for PIC16F877 microcontroller.

PIC16F877 i2c

This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the ‘Downloads‘ section at the bottom of this page.

It is assumed that you know how to blink an LED with PIC16F877 microcontroller. If you don’t then please read this page first, before proceeding with this article. It is also assumed that you know how i2c protocol works, if you don’t then please read this page first.

The result of simulating the code in Proteus is shown below.

PIC16F877 i2c Schematic

In the above circuit[2], RC4 pin is being used as SDA pin and RC3 pin is the SCK pin. Both of these pins are pulled up using 10K resistors as required for i2c protocol. Proteus provides an ‘I2C Debugger Tool‘ which is attached on the SDA and SCK pins in the above circuit. This i2c debugger tool receives all the i2c messages and displays them on the ‘I2C Debug‘ window displayed in above figure. Close up image of this ‘I2C Debug‘ window is shown below in the figure.

In the above figure, ‘I2C Debugger Tool‘ is telling us that first of all it received i2c start bit S. Then a value of 0xA0 was received. After that, there was a NACK bit (named as in the above figure). After the NACK, a value of 0xFF was there on i2c bus. After that, an ACK was there (named as A) in the above figure, and in the end i2c stop bit (named as P) was received. When this Proteus simulation started then after 11.601usec PIC16F877 started sending this i2c data and stopped at 219.201usec time.

READ  Humidity and temperature measurements with Sensirion’s SHT1x/SHT7x sensors using PIC18F2550 (Part 1)


The code for the main function is shown below.


I2C code for PIC16F877 was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.


For more detail: PIC16F877 i2c code and Proteus simulation

Current Project / Post can also be found using:

  • proteus i2c
  • proteus i2c debugger
  • how can i display a message on an I2c lcd in proteus
  • using i2c in proteus

Leave a Comment

= 4 + 3

Read previous post:
PicoDetector : a PIC-based simple and cheap metal detector

A short video clip is sometimes better than a long explanation : The idea of this circuit is to hack PIC...

Scroll to top