PIC16F877A I2C Tutorial

Here we are going to see PIC16F877A I2C Tutorial.

Prerequisites

Before getting into I2C Tutorial of PIC16F877A, Please read the below topics….

PIC16F877A I2C Tutorial

Introduction

I²C is a serial computer bus, which is invented by NXP semiconductors previously it is named as Philips semiconductors. The I²C bus is used to attach low speed peripheral integrated circuits to microcontrollers and processors. I²C bus uses two bidirectional open-drain lines such as SDA (serial data line) and SCl (serial clock line) and these are pulled up with resistors. I²C bus permits a master device to start communication with a slave device. Data is interchanged between these two devices. Typical voltages used are +3.3V or +5V although systems with extra voltages are allowed. Nowadays new microcontrollers have inbuilt I²C Registers. PIC16F877A also has separate registers for I2C. Unlike UART, you can connect and communicate to multiple devices using the same I2C bus.

Most of the PIC microcontrollers have built-in Master Synchronous Serial Port (MSSP).

Master Synchronous Serial Port (MSSP)

The Master Synchronous Serial Port (MSSP) module is a serial interface, useful for communicating with other peripheral or microcontroller devices. These peripheral devices may be serial EEPROMs, shift registers, display drivers, A/D converters, etc. The MSSP module can operate in one of two modes:

• Serial Peripheral Interface (SPI)
• Inter-Integrated Circuit (I2C)

The I2C interface supports the following modes in hardware:

• Master mode
• Multi-Master mode
• Slave mode

In this tutorial, we will learn how to operate the MSSP module of PIC Microcontroller as I2C master. And EEPROM will act as a slave.

The MSSP module in I2C mode fully implements all master and slave functions (including general call support) and provides interrupts on Start and Stop bits in hardware to determine a free bus (multi-master function). The MSSP module implements the standard mode specifications, as well as 7-bit and 10-bit addressing. Two pins are used for data transfer:

  • Serial clock (SCL) – RC3/SCK/SCL
  • Serial data (SDA) – RC4/SDI/SDA

The user must configure these pins as inputs or outputs through the TRISC<4:3> bits.

Registers Used for I2C

The MSSP module has three associated registers. These three registers are used for I2C.

The MSSP module has six registers for I2C operation. These are:

  • MSSP Status Register (SSPSTAT)
  • MSSP Control Register 1 (SSPCON1)
  • MSSP Control Register 2 (SSPCON2)
  • Serial Receive/Transmit Buffer Register (SSPBUF)
  • MSSP Shift Register (SSPSR) – Not directly accessible
  • MSSP Address Register (SSPADD)

SSPSTAT – MSSP Status Register

This register is the status register of the MSSP Module. The lower six bits of the SSPSTAT are read-only. The upper two bits of the SSPSTAT are read/write.

BF: This is the Buffer Full status bit. In the transmit mode this bit will set when we write data to SSPBUF register and it is cleared when the data is shifted out. In the receive mode, this bit will set when the data or address is received in the SSPBUF register and it is cleared when we read the SSPBUF register.

UA: This is the Update Address bit and is used only in 10-bit address mode. It indicates that the user needs to update the address in the SSPADD register.

R/W: This is the Read/Write bit information. In the slave mode, it indicates the status of R/W bit during the last address match. In the master mode, 1 indicates that transmit is in progress and vice versa.

S : This bit indicates that a Start bit is detected last and it will be cleared automatically during Reset.

P : As above, this bit indicates that a Stop bit is detected last and it will be cleared automatically during Reset.

D/A : This is the data or addresses indicator bit and it is used only in slave mode. If it is set, the last byte received was data otherwise it will be addressed.

CKE : Setting this bit enables SMBus specific inputs. SMBus is another bus similar to I2C, which is compatible with each other.

SMP : Setting this bit disables slew rate control and vice versa.

SSPCON1 – MSSP Control Register 1

The SSPCON1 register is readable and writable, which is used for control the I2C.

SSPM0 ~ SSPM3 : These are synchronous serial port mode select bits.

1111 = I2C Slave mode, 10-bit address with Start and Stop bit interrupts enabled
1110 = I2C Slave mode, 7-bit address with Start and Stop bit interrupts enabled
1011 = I2C Firmware Controlled Master mode (Slave Idle)
1000 = I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
0111 = I2C Slave mode, 10-bit address
0110 = I2C Slave mode, 7-bit address

CKP : This is the SCL clock release control bit. It is used only in slave mode. Setting this bit releases the clock. If zero, it holds the clock (clock stretch).

SSPEN :  This is the synchronous serial port enable bit. Setting this bit enables the serial port.

SSPOV : This is receiving overflow indicator bit. If this bit is set during receive mode, it indicates that a byte is received while SSPBUF is holding the previous value. And it has no application in transmit mode. We must clear this bit in software.

WCOL : It is the write collision detect bit. If this bit is set during master to transmit mode, it indicates that a write to SSPBUF register was attempted when I2C conditions were not valid for a transmission to be started. And if it is set during a slave transmit mode, it indicates that SSPBUF register is written when it is transmitting the previous word. We must clear this bit in software.

SSPCON2 – MSSP Control Register 2

The SSPCON2 register is readable and writable, which is used to control the I2C.

SEN : Start Condition or Stretch Enable bit. In master mode, setting this bit to initiate start condition on SCL & SDA pins and it will be automatically cleared by the hardware. And in slave mode setting this bit enables clock stretching for both slave receive and slave transmit. If it is cleared in slave mode, clock stretching is enabled only for slave transmit.

RSEN : Repeated start condition enable bit. This bit has application only in master mode. Setting this bit will initiate repeated start condition on both SCL & SDA pins and it will be cleared automatically in hardware.

PEN : Stop condition enable bit. This bit has application only in master mode. Setting this bit will initiate stop condition on both SCL & SDA pins and it will be automatically cleared in hardware.

RCEN : Receive enable bit. This bit also has application only in master mode. Setting this bit enables receive mode for I2C.

ACKEN : Acknowledge sequence enable bit. Setting this bit initiates acknowledge sequence on SCL & SDA lines and it will send ACKDT (see below) bit. This bit will be automatically cleared in hardware. It has application only in master receive mode.

ACKDT : Acknowledge data bit. 1 means not acknowledge and 0 means acknowledge. This value will be transmitted when we set the ACKEN bit (above). This bit has application only in master receive mode.

ACKSTAT : Acknowledge status bit. 1 indicates that acknowledge was not received from the slave and vice versa. This bit has applications in master transmit mode only.

GCEN : General call enable bit. Setting this bit enables interrupt when a general call address is received in the register SSPSR.

SSPBUF & SSPSR & SSPADD

SSPSR is the shift register used for shifting data in or out. SSPBUF is the buffer register to which data bytes are written to or read from.

SSPADD register holds the slave device address when the SSP is configured in I2C Slave mode. When the SSP is configured in Master mode, the lower seven bits of SSPADD act as the baud rate generator reload value.

In receive operations, SSPSR and SSPBUF together create a double-buffered receiver. When SSPSR receives a complete byte, it is transferred to SSPBUF and the SSPIF interrupt is set.

During transmission, the SSPBUF is not double buffered. A write to SSPBUF will write to both SSPBUF and SSPSR.

Circuit Diagram

LCD:

RS –  RC0

RW – RC1

EN – RC2

Data Lines – Port B

EEPROM:

SDA – RC4

SCL – RC3

Programming

In this code, I’m writing “EmbeTronicX” into EEPROM. Then I’m reading the data byte by byte using I2C. After that Displaying that data received from EEPROM. In this project, I divided into three parts. main.c, LCD.h, I2C.h. If you want to download the full project please Click Here.

Main.c

I2C.H

LCD.H

Output

PIC16F877A I2C Tutorial

[ Please find the output image Here ]

0 0 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
gagan singh
gagan singh
June 10, 2018 5:21 AM

hii,
I read this full tutorial,after that i want to implement this on Prteus8.6,
what i cant find i2c in proteus.plz help me,how i simulate this.

gagan singh
gagan singh
Reply to  gagan singh
June 10, 2018 5:24 AM

i find the 24lc256 eeprom,but cant find the i2c in proteus library.

EmbeTronicx India
EmbeTronicx India
Reply to  gagan singh
June 10, 2018 10:09 AM

Hi Gagan Singh,

You just do the connection between EEPROM and Controller with wire in proteus

gagan singh
gagan singh
Reply to  EmbeTronicx India
June 13, 2018 7:23 AM

hi,
YOU use Four main component in your this i2c Tutorial e.g. PIC16F877A,LCD,I2C,and EEPROM 24LC256.
but i found only Three component e.g. PIC16F877A,LCD,and EEPROM 24LC256 mean not found I2C Debugger.
So my quietion is Can i simulate the same code using Only EEPROM 24LC256 interfacing with PIC(Withought using I2C Debugger). becouse I2C is not availble in Proteus 8.6SP2.
please give me answer as soon as possible

EmbeTronicx India
EmbeTronicx India
Reply to  gagan singh
June 14, 2018 6:52 AM

I2C debugger is only for debugging purpose. But to simulate you need I2C debugger. I was using Proteus 8.0.

gagan singh
gagan singh
Reply to  EmbeTronicx India
June 14, 2018 12:09 PM

can you send me i2c library,GSM library and process how i add them to proteus,bcoz i begginer in this line

gs
gs
October 6, 2018 7:51 AM

hi admin… when i try to set the time using button it display as 1: ,1; instead of 11,12 so what is the prblm pls help me…what s the use of convd and convup function i didn’t get. pls rply……thank you…..

7
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: