Explore the fundamentals of I2C and SPI communication protocols, essential for interfacing sensors, modules, and displays, and facilitating MCU-to-MCU communication. Master these crucial skills for seamless integration and efficient data transfer in embedded systems and electronics projects, so let’s start!

Disclaimer:

Advertisements

If you missed the previous chapter, you can read it here:

Understanding SPI Communication in PIC Microcontrollers:

Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be utilized for communication between two microcontrollers. With its high data transmission speed, SPI is particularly useful in systems where rapid data streaming is vital, such as in memory card readers, digital audio interfaces, and digital signal processing.

How SPI Works in PIC Microcontrollers:

SPI communication is based on a master-slave architecture, where the master device controls the data flow using clock signals, and the slave devices follow the master’s clock to transmit or receive data. The primary components of an SPI connection include the master device, one or more slave devices, and four lines for communication:

  • MISO (Master In Slave Out): Carries data from the slave back to the master.
  • MOSI (Master Out Slave In): Sends data from the master to the slave.
  • SCK (Serial Clock): The clock pulses supplied by the master device to synchronize data transmission.
  • SS (Slave Select): A pin on each slave device that the master can set to low to select a particular slave device.
I2C and SPI communication

In the context of PIC microcontrollers incorporating SPI functionality, the implementation can typically be broken down into several key steps:

Configuration:

Firstly, the SPI module within the PIC microcontroller needs to be configured. This entails setting the clock rate, data order (MSB or LSB first), and mode (of which there are 4, varying by clock polarity and phase).

Data Transmission:

When data transmission begins, the master selects the slave device by pulling its SS line low. Afterward, the master sends a clock pulse through the SCK line, and data begins to be conveyed on the MOSI and MISO lines simultaneously.

I2C and SPI communication

Data Reception:

As data is clocked out from the master, it is clocked into the slave on the rising or falling edges of the clock signal, depending on the configured SPI mode. Conversely, as data from the slave is received by the master, it is read on the opposite clock edge from that used for writing.

Advantages of SPI Communication:

  • Speed: SPI supports higher throughput than I2C or UART due to its full-duplex communication capability.
  • Simplicity: Fewer pins and wires make PCB layout and design simpler with fewer routing complexities.
  • Separate Device Selection: The individual SS lines allow the master to communicate with multiple slaves by selecting one at a time.

Considerations When Using SPI on PIC Microcontrollers:

  • Distance: SPI is generally used for short distances since the clock signal can degrade over long distances.
  • Resource Requirements: Each slave device requires a dedicated SS pin on the master, which can be a limitation if many slaves are needed.
  • Collision and Contention: They are not issues in SPI since the master controls all communication timing, but proper management of SS lines is crucial to avoid conflicts.

In conclusion, SPI communication is a reliable and efficient way to transfer data in a system with a PIC microcontroller at its heart. Its simplicity and speed make it an excellent choice for a wide array of applications. However, considering the application’s specific requirements and limitations is essential in selecting the most suitable communication protocol.

Practical example:

Let’s make a practical thing to understand SPI more clearly.

mikroC Example Code:

/*
  Project name "Understanding SPI communication"
  Program written by Mithun K. Das; +8801972448270(WhatsApp)
  MCU:PIC16F76; X-Tal: 8MHz; mikroC pro for PIC v7.6.0
  Date:23-02-24
*/

// SPI module connections
sbit SS1 at RC0_bit;
sbit SS2 at RB0_bit;
sbit SS1_Direction at TRISC0_bit;
sbit SS2_Direction at TRISB0_bit;

int value1= 0x01;//dummy value
char info = 0xAA;// another dummy value

void main() 
{
  SS1 = 1;
  SS2 = 1;
  SS1_Direction = 0;
  SS2_Direction = 0;
  SPI1_Init();
  while(1)
  {

     SS1 = 0;             //select slave1
     SPI1_Write(value1);   //send value
     SS1 = 1;             //deselect slave1
     
     Delay_ms(2000);

     SS2 = 0;             //select slave2
     SPI1_Write(info);   //send value
     SS2 = 1;             //deselect slave 2



     if(value1<100)value1++;
     else value1 = 0; //just a value increment

  }
}

Here I’m interfacing two slave SPI devices with Master SPI. And the circuit diagram is:

Circuit diagram:

I2C and SPI communication

The code and the simulation result are explained in this video.

I2C Communication:

The Inter-Integrated Circuit (I2C) protocol is a synchronous, multi-master, multi-slave, packet-switched, single-ended, serial communication bus invented by Philips Semiconductor (now NXP Semiconductors). It’s widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication. Here’s an introduction to using I2C communication with a PIC microcontroller.

What is I2C?

Before diving into the specifics of the PIC microcontroller, it’s important to understand the basics of I2C itself. I2C allows two or more devices to communicate with each other via a control bus and data bus. All the devices connected through I2C communicate through these two wires:

  • SDA (Serial Data) – the line for the data transfer
  • SCL (Serial Clock) – the line for the clock signal
I2C and SPI communication

Features of I2C in PIC Microcontrollers:

  • Two-Wire Protocol: As mentioned, I2C communication requires only two wires, making it cost-effective for complex communication within a circuit.
  • Addressing Scheme: Each device connected to the I2C bus is identified by a unique address—either 7-bit or 10-bit—which is used to match the address sent by the master.
  • Speed Variants: The I2C protocol supports multiple speed grades, with the standard mode being 100 kbit/s.

Setting Up I2C on a PIC Microcontroller:

  1. Initialization: Begin by configuring the I2C module of the PIC microcontroller. This setup includes setting the I2C mode (Master or Slave), the clock speed, and initiating the required registers.
void I2C_Init(const unsigned long feq_K) //function to initiliaze I2C
{
    //... PIC microcontroller registers setup code here
}
  1. Start Condition: A high-to-low transition on the SDA line, while SCL is high, signifies that a communication start is requested by the master.
void I2C_Start(void) //function to start a I2C communication
{
    //... PIC microcontroller registers for initiating a start condition
}
  1. Sending the Address: The master device sends out the address of the device it wants to communicate with, followed by an operation bit (read or write).
void I2C_Address(unsigned char address, char read_write) //function to send device address over I2C
{
    //... PIC microcontroller registers to send device address
}
  1. Read/Write Operation: The master can now write data to or read data from the slave device.
void I2C_Write(unsigned char data) //function to write data over I2C
{
    //... PIC microcontroller registers to send one byte of data
}
unsigned char I2C_Read(char flag) //function to read data over I2C
{
    //... PIC microcontroller registers to read one byte of data
}
  1. Stop Condition: When communication ends, the master generates the stop condition.
void I2C_Stop(void) //function to stop I2C communication
{
    //... PIC microcontroller registers to generate a stop condition
}

Troubleshooting and Considerations:

  • Ensure pull-up resistors are connected to both the SDA and SCL lines as they are open-drain.
  • Check for the correct address for the slave devices.
  • Be aware of the bus capacitance limit, which may affect the number of devices you can connect.
  • Clock synchronization is automatic, but ensure the slave devices can handle the chosen clock speed.

Summary:

I2C communication is a versatile option available in most PIC microcontrollers, facilitating communication with various peripherals and sensors. Understanding the basics of setting up and implementing I2C communication can greatly expand the capabilities of your projects.

Always check the specific datasheet and I2C module reference details of the PIC you’re working with, as register names and specific details may vary between different PIC families and models. To more about I2C, you can read this article too.

Read more on:


Practical example:

For basic I2C communication, let’s test with debugger.

Circuit for basic I2C:

I2C and SPI communication

mikroC code for I2C:

/*
  Project name "Understanding I2C communication"
  Program written by Mithun K. Das; +8801972448270(WhatsApp)
  MCU:PIC16F76; X-Tal: 8MHz; mikroC pro for PIC v7.6.0
  Date:23-02-24
*/

// Software I2C connections
sbit Soft_I2C_Scl           at RC3_bit;
sbit Soft_I2C_Sda           at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections


void main() 
{

  Soft_I2C_Init();           // Initialize Soft I2C communication

  while (1)
  {
      Soft_I2C_Start(); // issue I2C start signal
      Soft_I2C_Write(0x01);
      Soft_I2C_Write(0x02);
      Soft_I2C_Write(0x03);
      Soft_I2C_Write(0x04);
      
      Soft_I2C_Stop(); // issue I2C stop signal


      Delay_ms(2000);
  }
}

Explanation in a video:

Both the code of I2C and the circuit diagram are explained in this video.

I hope this will give you the basic knowledge about I2C communication and now you can carry on to explore more.

Conclusion:

Understanding SPI and I2C communication protocols opens doors to a myriad of possibilities in interfacing sensors, modules, and displays, and facilitating MCU-to-MCU communication. Mastery of these protocols is not just desirable but essential for any engineer. Remember, there’s no shortcut to expertise; practice is paramount. So, let’s embark on the journey of exploration and skill-building with SPI and I2C. See you soon for more exciting discoveries!

You can check other articles too:

For Professional Designs or Help:

Loading

MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *