PIC24, DsPIC33, DsPIC30 (16bit PIC Microcontrollers) I2C (Inter Integrated Circuit) Code + Proteus Simulation



This post will provide working code for built-in hardware I2C module for PIC24, DsPIC33 and DsPIC30 with Proteus Simulation. I wrote two functions for reading and writing from specific registers of slave that works on I2C.

I am using MPLAB x IDE and C30 Compiler and Proteus Simulation on v8.0 (code and Proteus Simulation can be downloaded from downloads section at the bottom of this post). This code will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing the code. I supposed that you know basic about I2C communication if not then please click here


Proteus Simulation:

            In the following figure I used I2C debugger for viewing what I send to I2C bus and PIC is master and is responsible for generating clock on SCL pin. SDA pin has data on it. I2C is synchronous serial communication and was developed by Philips. SDA and SCL are both open drain so these should be pulled up by external resisters.


Figure 1. Proteus Simulation.

Code:

In the following figure 2 we have main function in which I first enabled the and configure I2C module with 100kHz clock speed. And then we have a while loop in which it will write I2C slave device having address 0x30 with register address 0x20 and the value is 0xFF. For instance  which is shown in figure 3.


Figure 2. Main Function.

Figure 3. I2C


In this following figure I configured I2C module as 7bit address, slew rate enabled, smbus disabled and with clock speed 100kHz which is 0x4E calculated from the formula given in figure 5.


Figure 4. Open I2C

Figure5. Formula for clock speed calculation.

In the following figure 6 we have WriteByte_I2C1() function which is responsible for writing registers of I2C the first parameter passed to this function is IC or sensor address which is 7 bits + one bit for reading or writing(in this case I am writing to it so LSB bit is 0) and second parameter is its internal resister address whose value we are going to change. In this function first I reset all variables which is recommended by applications note of 16bit microcontroller. Then generated a start sequence on I2C bus now for every sequence we have to wait for interrupt flag for insuring that the event is generated successfully then write address IC and here I wait for ack to be received from IC. If IC is not present on I2C bus there will be no ack. Once the ack is received Master puts address of its internal register if that register is present that we have another ack to be received. And then master put value of that register. Then again an ack will be received which means data is successfully written. In the end we have sop sequence for ending this communication.


Figure 6. I2C write function.

In the following figure 7 we have I2C debugger which is used instead of placing an IC here:


Figure 7. I2C debugger.

  • S represents start sequence generated on I2C bus.
  • 30 represents 0x30 IC address.
  • A represents ACK received.
  • 20 represents register address.
  • A represents again ACK.
  • FF represents data value.
  • A represents again ACK.
  • P represents stop sequence.

In the following figure 8 we have ReadByte_I2C() which is same as write data byte sequence but with an extra function of RestartI2C1() which is responsible for generating restart sequence on I2C bus. After that we have to change one LSB to 1 for reading data. After this Slave will put data on I2C bus and master with NOTACK sequence ends the communication. This function will return the data value that is being read form slave.

Figure 8. I2C read function.


Reading Suggestions:




Downloads:

You can download Code and Proteus Simulation
Click here

For all topics Click here.

©IbrahimLabs.blogspot.com


All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 
© Copyright 2013 IbrahimLabs. All rights reserved.




Popular posts from this blog

Defining code regions in MPLAB X IDE

Digital AC voltmeter Schematic + Code + Proteus Simulation

Automatic Streetlight control using LDR and PIC (Code + Proteus Simulation)