PIC Microcontroller Communication with I2C | Ashraf Madhoun | Skillshare

PIC Microcontroller Communication with I2C

Ashraf Madhoun, An Engineer with +10 Year Experience

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
12 Lessons (1h 26m)
    • 1. 1 Introduction

    • 2. 2 What is I2C

    • 3. 3 Things Your Need to Know about I2C

    • 4. 4 Advantages to I2C

    • 5. 5 DisAdvantages to I2C

    • 6. 6 Operation of I2C Bus

    • 7. 6A Operation Explained in More Details

    • 8. 7 I2C Library Routines

    • 9. 8 Circuit Design for I2C Testing

    • 10. 9 Coding Simple Example

    • 11. 10 Practical Example Reading Data from DS1307 Time and Date IC

    • 12. The End


About This Class

I²C (pronounced I-squared-C)

PIC Microcontroller Communication with I2C Bus: A step by step guide to Master I2C Protocol and Start using it in your projects

I²C Allows communication of data between I2C devices over two wires. which makes it easier for anyone to exchange information without too much wiring.

What You Will Learn in This Course:

  • What is I²C and how it works

  • How to implement I²C in your project

  • What is the difference between I²C and other communication methods

  • Why using I²C will make your life easier

  • The advantages and disadvantages of using I²C

  • How to code I²C the right way and how to talk to multiple devices easily

  • A practical example of I²C enabled DS1307 Data and Time IC and how to read data from it using I²C

What you’ll learn

  • What is I²C and how it works
  • Advantages of I²C
  • Disadvantages of I²C
  • I²C Operation
  • Main I²C Bus Library Functions
  • Interface different devices with PIC Microcontroller using I²C
  • How to implement I²C in your project
  • What is the difference between I²C and other communication methods
  • Why using I²C will make your life easier
  • How to code I²C the right way and how to talk to multiple devices easily
  • A practical example of I²C enabled DS1307 Data and Time IC and how to read data from it using I²C

Are there any course requirements or prerequisites?

  • A computer to run the software
  • A basic knowledge in C Programming language
  • No Material is required for this Course

Who this course is for:

  • Anyone interested in talking to multiple devices easily via I²C the right way
  • Anyone Interested in Interfacing Microcontroller with other ICs or devices via I²C
  • Anyone Interested in Controlling different devices using I²C Protocol
  • PIC Microcontroller and Arduino Lovers
  • Microcontrollers Geeks


1. 1 Introduction: hello and will come to this new course in which we are going to talk about Pick Michael control communication with I Square C bus using my Corsi or C language. This is educational engineering team what you will learn in this course. First, you'll learn how or what is I square. See why we are using it. The advantages I to see or square See that this advantages off by square, See how toe open it using I square See on the working principle behind this very interesting and famous communication protocol Andi, finally, we'll talk about micro see proof or pick our square C bus library functions. We will take a quick example on how to implement I square see using micro see libraries and we will talk about each function what does and why we do recommend using micro see for programming pick projects with I square See, I'm sure that you will enjoy the scores on that You will learn a lot from it So state owned on join us in this journey that will take you to the next level and programming pick makes controller on communicating with other devices. Thanks for watching this quick introduction. This is a national from educational engineering team 2. 2 What is I2C: I square. See, it's pronounced just like what I said. I square see first. What you need to know is that this protocol was created first by Philips Semiconductors four years and communications off Peripherals, devices or professor on devices within a TV set. Andi, commonly written as I to see which stands for inter integrated circuits again, it's pronounced I square see, but it's certain eye to see. It allows communication off that are between I to see the voices over two wires. So all what you need toe make communication between devices is just two wires, so it won't consumer Escobar. It won't take a lot fixing It's just two wires. I square See is sometimes called two yr. Interface on this is relevant to the fact that it only use two wires toe communicate with other devices. As you can see in this, their ground I square see, uses only two wires. These are the two man wires for communication. This is the master pic microcontroller. So as you can see in this design, this is our master microcontroller. This is the first line of communication on this is the second line of communication. Now let's say that you want to connect different devices together. Toe pick my controller. You should only take two wires from the man. Two lines on. Start connecting them to your slave devices. As you can see here, here and here, you can connect as my devices as you want. Each of these devices will have its own unique address. This will take address One. The 2nd 1 will take address to on the failed one will take address three on so on. You can connect as many devices as you want. So this diagram shows how many device can be connected with this simple to wire system. Using only two pens off the microcontroller, they are called as the A on a CEO on. We will cover this later in this course. For now, what you need to know is that it only uses two wires. It's pronounced as I square. See, Andi. It's a very state of the art way to communicate bit Beck microcontroller with other devices . That was a quick central toe. I square see communication protocol. I hope that now the idea is more clear to you on. We will go more deep in the coming lessons. Thanks for watching this lesson. This is educational engineering team 3. 3 Things Your Need to Know about I2C: the devices. I don't have to be identical. As long as the support I square see protocol. As you can see in this restoration, we have different devices. This is the first device on it has address one on this is the second device on it has other stool. This is the fare device Andi has address three. No, The fares device with address one is additional temperature sensor. The 2nd 1 is a real time clock on the 3rd 1 is a serial LCD decidedly on the bus could carry on even more devices. As we mentioned in our previous lecture. As you can see here, others want others to us v, temperature, sense or block on. There's that whole city now, one very important thing that you need to know is that communication takes place from the master pick to the individual selected slave. As you can see, we already mentioned that there has to be a pic microcontroller connected to your design. So let's say that our pick five control is still is here, and it will send data here that way. Toe different individual slave devices on this is considered a master which is big. Makes controller on these three on whatever you connect here are slaves that I sent from master to slave devices The master since that data to slave device that we want to control . So if I want to control device to the master will send data toe device numbers to using the address here identified here. So if I want to control it say temperature I will send data toe device connected to address one Since it's a temperature sensor on if I want to control this LCD device I must send that Onley toe address three This is how indication is done within the I square See protocol? No. One thing that we need to mention is that we have two lines as cl Andi asked the A These two lines are open drain This basically means that that ship can drive its output. No, but it cannot drive it high for the line to be able to go high, you must provide pull up resistors to the five volts supply. So as you can see here we have two resistors on these two resistors are connected to five volts. This is how you provide pull up resistors to the five volts. Possibly this is done again because these are open drain lines. This means that they can go low, but they cannot go high and know that for them to go high, we must build them up using to pull up resistors. Words store from the STL line is connected. The five All slime Again. What is the store from the A. C. A. Line to the five volt line here. Ethnic. Choose another color to make things more peer. This is the first through the store. As you can see from the cereal line. This is all a sister, the five volts on the other one from the SD airline here, through the resistor onto the five volts. You only need one set off pullup resistors for the whole ice core CPAs not for each device . As you can see, two resistors connected to these two lines is more valuable. The value of their story is not really critical. Anything from one kilo toe 47 Caro can be used. Their values depends on the length off the bus line. If there is, the stores are missing. The A C L on as the airlines will always be law nearly zero volt on the Ice Square C bus would not work. So make sure that whenever you use I Square C bus to include these two pull up resistors, this is something that's taken for granted and without it, the ice core see protocol won't work now. One thing or one other thing that we do in gym is that I square. Ciba's is suitable when the microcontroller on the devices we want to control or exchange that I with are in close proximity, like in the very same area or on the same PCB. These devices usually are smart devices can be memories. Module temperature sensors Syria on this al city real time clocks analog to digital converter on digital to analogue converters. Again, this means that I square see protocol is suitable when you don't have a very long distance toe. Exchange information, so it's suitable for small distance information. Exchange on control. Me similar to see their communication in SP, I mood that a trance fear in I square see mood is saying Karan's on by their excellent again by the directional means that you can exchange that a both ways. At the same time, this time, only two pins are used for that. A transmission 14 that the data, which is as the A that stands for serial data on the other. Four. Cloak, which A. C L that stands for serial clock so only two pins are used to exchange data again. Cereal flock for the bounces on serial data for exchange off information. This is something that you need to take into consideration this Communication Cancer Board a maximum off 112 devices on the bus. The specifications declares that 100 28 devices can be connected to the I Square C bus, but also defines 16 reserved addresses, which means that you cannot use these 16 addresses. They are connected in a simple way. By using only two valuables. It'll boat output pins. So can you imagine that you can connect Ah, 112 devices using only two pins off your pic microcontroller? This is a huge thing. This is something that you don't see every day on. It wouldn't make your communication Life are way much easier. You can connect sensors. I'll see this ablaze on a look. Traditional converters on Disney Channel converters without any worries you can easily communicate on control thes 112 devices using these two lines. So overall, it's a very easy and straightforward way of communicating on controlling slave devices, using one master device and using only two pens, one for data on 14 o'clock Now toe make a quick summary of everything that we learned in this lecture fares. You have to connect to pull up resistors 25 balls because a. C. L and as the A R only open train lines, they provide very low voltage around the revolt. So without these resistors, you cannot use I square see protocol. The second thing that you need to know is that you only need two lines or two pens from pic microcontroller 14 Syria flock on the other for serial data. The fair thing is that you can connect upto 112 devices. I'll control them easy. The fourth thing that you need to know is that when you use ice core, see, you have to make sure that devices that are connected using these two lines are in a very close distance with the pick makes controller were preferable on the very same PCB because this communication protocol doesn't provide enough power for long distance communication. So many it's used in closed the stones. Communication. That's it for this quick. Lawson, if you have any questions, please ask in that you want able have been learning from all of us here at educational engineering team. 4. 4 Advantages to I2C: advantages off Ice Square. See now the first thing is that it's faster than a single cereal, allowing large quantities off that Toby transferred quickly. But it's slower than the S P I. The bus can operate at various speeds, which are generally dictated by the slowest device on the bus. Common bus speed are 100 kilobytes per second, which is in a standard mode while and fast mood. It can be upto 400 kilobytes on in high speed mood. It can go upto one mega by per second, but on Univision off the specifications allows for a maximum through part off 3.4 megabyte in high speed wound. So we can say that it's it can go from 100 kilobyte upto three point for megabytes, depending on the moon that you are using. The second thing that you need to know is that I Square see is using only two pens, so it's like a single cereal. But the those two wires can support up to 112 slave devices, so connecting a single master toe a single slave with an SP I bus requires four lines. Each additional slave requires one additional trips elect and put out with pain on the master. This can become very cover, some in situations where lots of devices must be slave to one master. But in our case in I Square see, it's only uses to Weill's. It's like again a single serial about those two wires can support up to 100 world slave devices without the need for additional yr for each new device that we add, toe our communication or toe our circuit. So it's very preferable over the SP I communication protocol, since it only uses two wires and it doesn't require more wiles for each new device that we connect now. The third thing is that this I square see Protocol Cancer Board, a multi master system which allow more than one masters to communicate with all devices on the bus. Unlike SP I I square see Cancer Board multi master system again. Such system allow more than one device Toby, master on the system aunt to control other devices on the bus. Although the master device cannot talk to each other over the bus and must take tails using the bus lines so you cannot use two masters at the very same time you have to identify one master pairs time. So when the first month ends or stops, sending data or controlling the second master can take control. Andi, uh, the way you want. So this is one thing that s p I doesn't have on no other serial communication protocol hards. It's a multi master system which allows more than one masters control at any time now. The fourth advantages is that it supports multiple slave or slaves upto 112 slave devices I square see device. All devices may have external address spends, which allows you to assign unique address to each device and therefore allow multiple off the same device to operate on a single bus again I square see, with its support toe more than 100 or 100 world slave device. Ah, is something Toby Ah considered a very good advantages because it can allow you toe use the same slave device twice by giving it more than one others, I swear sees aboard slave acknowledgement, which means that you can be absolutely sure that you are actually communicating with something with S p I. A master can be sending death at no nothing at all and have no way to find out. So I square see, has this feature, which is slave acknowledgement that pins you that you are sending that at the right slave on that it has received your data, so it's faster. It requires only two wires. It's a board small team master system. Andi. It supports up to 112 slave with slave acknowledging These are four features that make it more than enough to use I Square see instead of us be I serial communication protocol. And I'm sure that if you are some someone ho is looking for the best way to communicate with other devices. You will find out that this is the easiest based on more professional way to communicate using Pick my control or any other type Michael controlled. Thanks for watching this lesson. We'll talk about that. This advantages are square. See in the next lesson. That's it for now. This is educational engineering team 5. 5 DisAdvantages to I2C: this advantage is off I square. See now the first advantage is that communication via ice core see is more complex than with Ah, us art or SP I communication. The signalling must add her to assert state and political for the devices on the bus to recognize it on valid I square See communication so it will take a bit more work toe implement this communication protocol I square see draws more power than other CR communication buses due to the open drain topology off the communication lines. We already covered the open drain. Andi, it's one downside of I square. See, the fair thing is that single devices can sit there. Communication speed. Slower operational devices can delay the operation off the faster speed devices. So if you have 100 device, the system or the communication protocol will goal at the speed of the slowest device in the bus. So if one is running at 100 kilobytes per second and one is running at one megabyte per second, the line speed will be 100 kilobytes per second, so the slowest device will slow the whole operation. The the Ice Square C bus can result in the entire bus hanging when a single device on the bus stops operating. Cycling the bar to the bus can be used to start the bus and restore proper operation, but again because it can support up to 112 device using only two lines. If one of these devices is out of service, off has a problem. You will find that all devices in this pass are not working properly on the entire bus is hanging because one device is off. I Square See is designed for fairly short range on the same PCB as the master or via a relatively short cable. Compare toe R s 232 rs for 85 or can bus. So this is a down side because you are limited with a very small space. When connecting other devices do the ice that microphone floor are using I two c protocol. These are the downsides off using I square. See, they are downsides, but I think that you can ah, work around them if you set your mind on if you have a clear plan I to see or I Square C has a lot of other advantages that we already mentioned Andi using just two lines, It will make your life away much easier. Communicating with other devices. That's it for this quick, This advantage I square see less on. If you have any questions, please ask in the cloud able. And if you haven't reviewed the course yet, you can review it in the dashboard to help us improve on reach. More audience. This is Ashraf from educational engineering team. 6. 6 Operation of I2C Bus: I square C bus operation. As you can see as this is a single serial communication oclock signal is necessary to synchronize the operation off. Both devices. It's always generated by a master device, which is a microcontroller on its frequency, directly affects the board rate. Now when you talk about this, when a master and slave components are synchronized by the clock, every data exchange is always initiated by the master. Once the AMA's SP module has bean enabled, it waits for a start condition. Toka. The monster device fares since the start pit, which is Logic zero again, start pet logic zero through the as the ape in. So if you looked at this diagram up here, you can see that this is the area where we are sending the start bit, which is allergic zero. Then it's followed by a seven or 10 bit address. These are the other spits, in this case, from a zero to a six. There are seven other spirits off the selected slave device and finally the bit which requires that write or read to the device. So here we have a read write bet. As you can see, let's show the here it is. A read write bet on this is ah, a bit When it zero, This means that master is sending better when it's one. This means that master is requesting data from the slave devices. So this is called, uh, lead, right? Ah, request a bit that at this point, if the slave address exists on the bus, the slave will send on acknowledgement bed to the master. As you can see, this is the acknowledgment. Bet Andi. If it's one in this position, it indicates that the addressed slave did not respond or was unable to process the request . So again, a start bit are seven pit address, then read. Write requests, then acknowledgement. Bet this is the whole package. This is done on the S D. A line all serial data address. At this point in the slave again address exists on the bus. The slave will send an acknowledgement bit to the master that that is then transferred toe on the US The airline in the direction that waas specified by the master on at lone Acknowledgement pit is sent at the end off each transferred bite by by the receiving end of the transmission The only exception is that when the master is in, receive mode on the slave is in transmit mode. The master will not send on, acknowledged or acknowledge bit after the very last bit received, so it will only be sent if it goes from master to slave. But it won't be sent if it goes from slave to master. The one last thing that you need to know is that the communication is stopped with the master sending a stop command. The start and stop commands are simply a transition from high to low, which means a start on the as the airline with a C l high or low, old or low to high on the SD, a line with a C l high transition for the data bits are always performed while a C L is low . As you can see, that is only transmit when a C L is low. The high state is only for the star. Stop commands. Now if we need to summarise as you can see here in this area, stop condition as the eye goes high after a c l. This is as the A on its going high after a c l went high as even see here we have a delay between two. The 1st 1 was a C L. Then after that, as the air went high, this indicates the end of transmission. Let's symbolize what we have learned here as the A will send a little bit indicating the start of transmission as the A goes low before a C l as you can see here, this is us D. A. And it went low. Then a c l went low. Okay, now these are that look policies that are on off, on off, on off. There are pulses on this is the man parables A C l. It's cereal cloak. Um source or look buses. No, this is a thought bit. Then several address pits. These are the seven other spirits. Then we have a read write requests that are sent that a bit. Then we have an acknowledgement bit that indicates if the address slave did not respond or was unable to process the request. After that, we have another operation on it Has the very same elements start either spit. But this one is eight at a bit. Other spirit on this is the acknowledgment. Bet again. Um, after this, we can see that this is the stop condition, which is ah, like that opposite off the south condition. So he as the as the A goes low before a c l here as the a goes high after a c l Okay, on this is the as the a going high after a c l wentai. This means that we have a stop condition on that transmission, that that transmission was ended and it's done. This is what's going behind the Kirsten. This is what's happening happening in the background. Off I score. See communication on. That was a quick summary. If you have anything that is still not clear about what's going on, we can explain it in more details. You can just ask in the Q and A board again summarize things as the A will send a start bit . Then it will fall by 7 to 10. Add respite, then we have a read write bit that indicates if masters requesting that are receiving that , then we have acknowledgement bit, which basically indicates if the slave device did not respond or was unable unable to process the request. After that we have the stub bait off the stop condition where, as the airline goes high after a C L line goes high, that's it for this lesson. If you have any questions, please asking that you aren't able. Thanks for watching this is educational engineering team. 7. 6A Operation Explained in More Details: one thing that still needs to be cleared regarding the operation off I square see bust operation as the sequence off. Sending that. Now, let's say that we have a slave device. Our multiple slave devices on one master device the first tip in communication between muscle and slave devices would be addressing the device that we're trying to communicate with. And since we have a lot of slave devices, first we have to write or to send the address of the device that we need to send or request data toe on form on. This is done using the first part off this operation, which is this part. So we will use I square See writing protocol toe send seven toe, 10 bit address Blust one bit for either sending data or requesting data from the slave device. So we will give the address on. We will add Blas. Either zero. In case we are sending that or one in case we are receiving better. Andi, after something this we will get a binary value that has eight bit, which is the address off the slave device. So the first step we have slave address. Okay. Now, since we have slave device address, which is the first slab step we need for sending that obits. We need another additional step if slave device has its own memory. Andi, when we are sending data to a slave device, we have to specify that memory location inside the slave device that we are sending that at all, usually most live devices has a barone memory or flash memory that start with zero x 00 address and so on. So you can simply arrived the address of the slave device. Ah, memory add location. So if this is the mirror location inside the slave device on, we want to address the second location. So it will start with zero x 00 on. We want zero x 01 Then the second step will be trying toe. Okay, let's turn the corner. The second step would be okay. This shows another like him would be sending slave nobody. How about this? Okay. No, The first thing waas eight bit for the slave address. Then we have let's say zero x 01 for the member address off the slave advice. Then the failed stable will be, in this case, sending the data itself. So let's say that we want to send zero X A. So sending data toe a specific location in de memory off a slave device will take three steps. First, you need to send the others with a slave device. Second, you need to send the address off the memory location inside the slave device that you want to send or receive data to or from ONDA Fair Step will be sending the data itself for receiving the data from the slave device. So it's a three step operation on the last step, which is Number three is explained in this diagram above. If we I took a quick look here, you can see that these are eight that bits. The's are affairs there bits that we are either sending or receiving toe or from the ah slave device using I square See protocol. So the operation involves three steps slave device address, then slave device, memory location, address, then that that itself. Either way, we are either sending that are or receiving that I talk on from this location on. That's it. This is ah need to be cleared for moving, going with the functions off the ice core. See operation. Thanks for rushing this quick. Let's say clarification off the ice core C bus operation If you have any question off, you didn't understand what I was saying. You can simply ask in the affluent able Thanks. Rushing habit learning. This is Ashraf from educational engineering team. 8. 7 I2C Library Routines: my Chrissy Pro for Pick I Square C. Bus Library functions. Programming on ice. Accorsi Bus communication is not difficult. Most of the combined Lear's provide library routines, which make it easy, might receive proof or pick provides I Square C library, which supports the master I square. See mood. Andi In this last one, we are going to take a quick look at these functions inside that my Chrissy Pro Library. So let's start by taking a quick look at some microcontrollers functions in order to use that Zeier I square see routine. We have to make sure that we are selecting the appropriate I square See protocol. Now you need to know that some microcontrollers have multiple I square see module on. In order to use the desired I square c library routine, we have to make sure that we are selecting the one that we are using. So in this case, if we are writing I square see one, this means that we are using the very fares I square see a communication protocol which is the first, would you on it has I square see to which peace and that we are using the second model Now , this is something to take into consideration when using ice. Coursey. Ah, there is a quick note. Not all pick make controllers have on I square See module. So if you have a pick make controller on, do you don't know if it has I score C or not. You must go to the letter sheet on search for I square. See, for those who don't have on ice cores, say module or if Annie in both output either than the default hard weapons are required, there is some software functions that can be used. Andi, you can buy on extra. I see all integrated chip that has built in a square. See political. You can connect it to your pick my front rural, but we don't recommend using this method. You can just go on brushes. A pic microcontroller that supports I school see out of the box because having extra connection wouldn't be a good practice when it comes to see their communication again. Some doesn't have I square see protocol. Some have more than one eyes to see or I square see protocol, so you need to make sure that you and read that ever sheet before starting with this I square See communication protocol. Ah, practice. No. These are the ice core C library routines. We are going to start with I square, see one underscore in it or initialize this function Initialize the I secure See with desired clock So you would live the clock between two. Like this. You you're right. The number here you need to refer to the voice data sheet for correct values and respect with the oscillator value. This routine should always be called before using any other functions off ice Coursey Library. The maker Control has toe. Have hardware integrated I square See module before you can use this function. It's not really Ah, good practice Toe added your coat on choosing pick. Make sure insure that has that doesn't have I square. See, you will get a combined. You need to know that calculation off the ice square. See cloak value is carried out by the compiler as it would produce a relatively large called if performed on the library level. Therefore, compiler needs to know the value off the parameter in the combined time. That's why this parameter needs to be a constant and not a variable. So, for example, you can buy I square see one initialized and at 10,000 here or 20,000 depending on your specifications. On your oscillator speed, we will talk about this number in more details in the practical section. But for now, what you need to do is that you have to identify a number here so that you can initialize the I square See protocol. This is the most important function which is, I swear, see initialized without it The pick make winter are 13 organized that you are using I square see communication political. So now let's take a quick look at the second function which is I square see start dysfunction determines if the I square C bus is free and issues a start signal. If there is no error, the function returns zero. Before using this function the ice square see must be configured first using the I square see initialized function So you have to initialize die square See then you can start that I square See protocol No, I square See repeated Start this function issues repeated Start signal. Before using this function, you must initialize the the I square. See on this I square see repeated start from its name its on Lee used to repeat start there initialization or the art square See protocol. So if you started the I square see and you needed to repeat start that same module You have to write this line anywhere inside your code so that you can start the I square see all over again. It's a very useful function if you ah suspect that something went wrong with communication . Next we have the I square. See is idle function This function tests if I square c bus is free. If the bus is free, it returns one. Otherwise it will return zero. Before using this function the I square see must be configured fares using the I square See initialized? No, mostly this function is used inside if statement So if I score c is ideal Do this or check if the bus is free then do something like since that chick Ah slave device or whatever. Then we have the i square c r d. This function reads one bite from the slave and since out or since not acknowledged signal if parameter acknowledge is zero, otherwise it sends acknowledge. Before using this function, you must initialize the I square. See ah function which is I square. See initialize on start signal needs to be issued as well. Using the I square. See start function. So before using the read function toe read one bite from the slave. You have to initialize the protocol and you have to start it. Okay. And one quick example for using this assembly is simply writing I to see read And the return of this function will be stored in assured variable so that we can ah deal with it. It was a temperature or if it waas any other kind of signal? No, we have another functional which is I square. See right This function since that a bite via I square c bus. If there were no heroes, it retail zero. If there were errors it wants to sell zero will send another number and it's send that, uh the thigh square bus. Andi, you must configure the initialization on start the protocol before using this function. Ah, for example, you can write I square see one fight then and but our theses on sends out this number Roaches too. This is better on. We're sending it via di Square, see communication protocol on and again. We already ah took the ice square C r D ah, This function now to the last function which is by square. See one underscore Stop this function issues stop signal the kind of bed that we already talked about in the operation lesson. Before using this function, you have to initialize the I two c protocol or I square. See protocol because you are stopping it here. So it's common sense this function again, issues are stop signal indicating that's the end off. Ah, serial communication using the I Square See protocol. That's it for this quick intro toe I Square C library routines. If you have any question regarding and off these routines or functions, you can ask in the Q and A ball, we will see them in action in eggs. An example in the practical section. But for now, that's it. Thanks for watching. If you have any questions, please ask that you weren't able. This is Ashraf from educational Engineering Team 9. 8 Circuit Design for I2C Testing: Hello. Anyone come to this in unison and which are going to make the circuit design necessary for testing out I square. See communication? No, let's start by bringing up protests you can use honey, sir, could design software that you are comfortable with, but I prefer brought us because they had a simulation future. Let's create a new projects. Okay, lets plows on save our project. Okay, select folder. Now let's call it first dist. Next, Next, Next, then finished. Now I'm going to use pic 16 F 877 because I know that it has I square see communication protocol. We need a Christa cut my stools on. We need to pull up resistors as we already mentioned and are introduction Now, let's first connect the crystal Now it's took a busters. These have to be 33 people. Farhod. Same here. No listening this to ground. As you can see, it's a little on the letters to are the pens that we used to connect or crystal and traitors. Now we need to connect the Emcor to think here on the store, on the plus five balls Power. Okay, the last thing that we need is looking for us The A and A C L As you can see on our C three and R C four, we have as D A and A C L. Let's first to pull up resistors. So let's tow bar lines or a power line here on Let's drag to resist stores. Hear, hear! Let's choose for born seven que No for each These are because as the air A C L, uh, opened ran lines, so we have to build them up to five volts. Okay, now there are connected. Now in order, Doctor. Ah, test I square. See protocol. We can add devices like temperature controller, a serial city or any other device that supports I square see and kissed out or code you can add on e e P room or any other device again. But since we are using protests and simulation moods, we can add ice square, See? Ah device. It has a built in device. Monitor's off device. Ah, the bag off. As you can see here, we have I square see in this menu on if you went as you can see here this menu you can but I to see the bigger and you can simply drag it on a little bit here. Now, what you need to do is connect as the A with us the area and you need to connect a c l with a CIA on that regard can be shorted with the C l a pin. Now, this is our simulation. This is the schematic for our circuit design. In the next lesson, we are going to court this pick my friend stroller on send and receive that tow this I square see device and brought a software. That's it for this lesson. If you have any question or if you couldn't find any item, all of the design isn't clear. I can clarify things again. This is the oscillator area, so that big can function correctly. On this is the NCLR pin. The's are connections related. Big knife controller on These are the connection rated toe I square See protocol. These are to pull up resistor again because as the a c l r l train bends so you have to pull them up using Apple Ab sisters 4.47 Kato pullup resistors and I to see. This is a device a slave device on. This is our master, which is the make controller. This slave device can be anything from Richard Controller Ah, digital cereal city or any other device? That's it for this quick design on again. This car this is applicable in real life. Using the very same connection steps to a pull up resistors on as the air A CEO on these two pull up resistors are connected to the device and here we can connect other devices as well. That's it. Thanks for watching. See you Next. In which CNX lesson which we are goingto program. This makes controller. 10. 9 Coding Simple Example: Hello on. Welcome this in us. Um now, since we have created our design, we need to start coding. So go look for micro. See proof for pick on this start by creating a new project. Now click file new new project to a standard project Select place store You thought and choose the device Name pic 16 F 877 A on choose 20 megahertz could still look finish now Here we have our void, man. And thats combined to make sure that everything is ok. Okay, No, let's start by first. Initializing I square. See, Aunt, let's add oclock value 100 1000. Now let's combine And as you can see I square see one initialized is in town so go to micro the library's system libraries on and you can select all of them. But what I need is I square See So now it's combining correctly. This is done via library manager I just imported. Ask the square C library on this line is for initializing the ice square. See communication protocol again. Next thing is starting the ice. Coursey, this is ah used. This line is usedto start the I square. See? Ah protocol in this lecture, we accept them. That that is sent through the as the A. It has a start bit. Then we have seven other spirits. Then we have a read write check bit. Then we have an acknowledgement bit. Andi, it goes so on, um address. Bet, then that a bit Andi, So on. So first you send the address spit to choose which slave you are targeting. Then you send that that orbits toe that slave on you. End the communication. This is on easy to follow scheme. Send the address, then send the data. We will try to explain a more complex example. But for now, that's it. Now, after starting that I square see protocol you have tow send bite via I square. See, that includes device address Blust either read or write. So read. All right. We already mentioned this in the operation lecture. Andi, it means that you have to send 01 depending on the fact that do you want to read that from the eye to see or a square see supported device? Or don't ride data on Since we are going to write that we need right value off zero as you can see in this image. Zero master is sending better on one If master is requesting that. So we will send the address bit Last zero or one anarchist will be zero because we are sending data. Um, just like what we explained in the operation lecture. As you can see, this is the address, which is the first thing that we want to send. Um let's go back to our according you must write. I square see one, then underscore w off which stands for right on Dhere. We need to write the address off the ice square. See device blast zero or one. So let's say that this used the quicken versa on this Say that we have a two. As you can see, this is the address for our I square See device on. This is zero because we are sending the value of zero because we are sending data for writing better, and it will be one if we are requesting that are from slave devices. So the slide zero x a tool. You can find the address of the device that you are trying toe communicate with via the data sheet and you will explain this in the DS time Oclock example. But for now, you just need to learn that this line is for sending the slave address that the master is trying to communicate with. Okay, Now, the second thing that we want to send is we wouldn't light I square see one wr and we were like between these two prophecies, we will ride the address wall E beram location that we are going toe right information in. So let's right information and other stool. You can choose any other number depending on your needs, but I know that all of these addresses are empty. So I chose to You can choose anything. Other address. Now we have sent the address of the slave device. We have seen the address off the memory location where we want to insert data. Next thing is writing the data itself. So let's say that you want right zero x A. This will send this value and picks up this amount on that matches this as you can see. 0101010101 So that's it, said the others of the slave device. Since the others of the memory location, the slave device. Then send the data that you want to store in this address. In this live device, it's a very simple and straightforward process. After that, you have to stop the Eid square, see one protocol, then add a delay so that you will allow your make ripping through a lot of stabilize after sending or receiving data. Now let's combine. Our court has got C combined correctly. Now let's go here to our simulation area. Let's make sure that we are loading that I text five now on the simulation. Once the simulation runs, you can see that this is what we are sending the that are toe address a toe. It's written here in this line, and you can view more information about the data that you are sending from these lines. Now this is it for the simulation of this I square see device and brought us professional software. I'll try to explain things in more details. With a practical example, we will try to examine the DS time clock. I see that uses ah, I square see protocol. So stay tuned for the next lesson. If you have any questions regarding this, listen you can ask him if you aren't able, I'll be more than happy to help answering any question that you have. This is Ashraf from educational engineering team. 11. 10 Practical Example Reading Data from DS1307 Time and Date IC: hello and will come to this annual Asan in which I'm going to explain how to implement I square see, or how I score c is implemented in the controlling off the US 10 or one three 07 I see which is on. I see to read time date Ah, hours and minutes. So let's start by taking a quick look at this cold consist off LCD library because we are using LCD display numbers, it has methods. This one is for reading data from the I C and we'll talk about it in a minute. Andi, these are variables for time. Second hour day on. This is the form out of time and date. This is the main thought. Here we are. Initializing the ice Percy Protocol here. Really? Initializing the other city on Dhere. We are tearing off the cursor on disciplining time on date on the all city Here we are reading the value of from location zero and we will talk about this in a minute. Here we are reading developed the data from location one on here are really that are from location to and storing them in second minute on our now Let's clarify the first thing, which is second minute on our he is. I have a day ground, as you can see in thes. And this diagram location zero zero. Ich ich means hicks up, which is the location zero has, ah, seconds inside it and they range from zero toe 59 on vacation. One has minutes, location, toe has hours. So if we went back to our code, you can see that we're reading from from location zero and you are storing it in seconds. That's why it's convenient. So let me move this here. Andi. Okay, here it is. As you can see. 2nd 0 second zero on here. Minutes one on here. We have minutes. Medication one on our location to and here have hours, location, toe. So we are reading from the locations Were one and two for second minute on our let's go back to the read, the US May thought that use ice core. See protocol. So moving back here. This is the read and we are reading from the address that's entered here. This is a basic method thing. So when you call on the third and if you have zero here it will replace the address, which is here with zero. So we are reading from location zero. Now, the first thing that we need to know is the address off the chip itself. So here the ice Percy Protocol has that DS 130 several. I see. Which is the time and date I see that we're using at location zero x zero d zero. So this is the icy location zero x zero. The second thing is of allocation of the register inside the I C. All memory the I see here is the slave device. And we already explained this in the operation lesson. And since this, I see is a slave device has an address which is zero x d zero on, which is the first thing that we are trying to address. The second thing that we need to send is the address off the location inside this slave device memory, which is basically this thing. No, this is the address off the I C. I squared c ah, enable I see. Which is that he asked 1307 And this is the ah address inside the register, which is zero. If we are, um, aiming to get seconds as in this picture. 04 seconds. 14 minutes on 24 hours. So after getting this address, what we are doing here is simply I square, see one repeated start. So we are repeating the ice Coursey protocol. Then we are sending the same others that we have sent here, followed by one. So in this on this, we have sent the address followed by direction better she zero for right on one for real. So zero x 68 followed by zero will equal zero x d zero. So if we went back here this is the icy itself. Andi, if we want to address the I see, we need to make sure that we are defining without we are reading or we are writing so far, reading from the I C here we don't have a method for writing. If you are reading, this is the icy address. We must follow this address by zero. This means that we are writing toe this I square See, I see Andi, If we need to read, we must add one. Sophie added 00 Blast zero x 68 will equal zero x the zero. I won't explain how toe some these numbers you can search for on online calculators. Do this again after doing this. What we need to do is reading data from this location, which is zero on what we will get. Here is seconds because we are getting We are addressing that second spit on this data hotel using this return method at the line and will be stored in the shores in Tygart. And if we went back here, we will find that second this the result off courting this method will be stored in the second. Okay, Now, to explain this and more clear way, open up veins and try to show you exactly what I was talking about. Now let's say that we have a lot pick make friends all here on we have our I see here on there is two lines which are basically the two ice core. See protocol communication lines on. Since this is a nice E must has an address. And as we mentioned in or code, this is the address zero x 68 Let's write it here. No, this is the address of this icy on. This is our picnic control so it will send data and send and receive that auto run. From this I see which is the time I see now to make things more clear, let's see say that this I see has memory and inside it's more. Since it's a time related, I see that we use to read seconds, minutes, hours, date or any other types of data. Each of these this is a memory. So on this is the location zero x 00 This is zero X is Iran. And we know from the shit of this I see which is that the US 13 zero several. We know that this location is four seconds on this location is four minutes. We won't talk about seconds. Only on what applies for this will work for any other. Now, in order to talk with this icy on, start addressing this I see we need fares to know with a re are reading or writing. We know for sure that we are writing that at this. I see at the location of seconds which is basically this locations your x 00 and we know that the icy look, I d which is the slave device is zero x 68 from what we learned in the operation. Listen, we know that if we have the address of the slave device, we need to add either zero blouse zero or 10 if we are writing better and one if we are reading that So here are sending that this slave device. So it will be this. We will add zero to this address because we are writing that if we are reading that, I will add one. So the address that we are going toe send at the beginning off the communication is this address zero x 68 lost zero. Or as you can see here after something thes two values, who will get zero x d zero, which is the value, or the address off the chip or the DS 13 07 So now we did the first step, which is Cindy talking to the slave device though the right slave device, because big Mac, French or can, or any controller can be connected to multiple slave devices. So we said the name or the in this case, the idea of the slave device. The seconds they will be identifying the exact memory location inside this slave device that we want to communicate with, which is zero x 00 Because we are talking about seconds on. This is what we have done here. We sent the value zero to this method, which is four seconds on what we have received from this method. We started in the second variable. So if you went back here, it will send the icy address plus zero because you are writing data. Then it will send the address off the memory location inside the the s 1307 that we want to speak with. Which is the seconds address seconds as this image is location zero. As you can see, it's four seconds. So after this, what we will do is simply reading the data from this location. And since we are reading, we need to send for reading from this location. We had to send the same. I see. Ah, as in this line. But instead of zero, we are going toe. Send one, because we are reading data now. So the first step. Okay, let's let's take a quick screen short for this course, so that I can Dublin What has have been it's years. Okay, Okay. Now Okay. No, the first step Waas simply sending the I c ah, at this followed by zero because you are writing data to the seconds location. Then we have sent the zero location, which is the location inside this I see inside this I see on after that we have seen the same icy location, but for But we did Ah followed full of that number which is the icy location by one. Because here we are reading that. Okay, now one might ask why you did We send one here on day zero here because here we are writing that the icy okay now followed by one. This means that we want to read from this icy. Then once we send the Read rd, we assigned the occasional church zero Inside this I see memory. We started at injury data variable on. We returned this variable so it will return the seconds Now If you want to read minutes, what should we do? Its symbol. You can change this address killing me chairs the color this other This on this address must be changed with one water out form treat hours. We must change this on this with two. Why is that? It's because we are dealing with different memory. Location inside the slave device. 04 seconds. 14 minutes to four hours. Okay. Why haven't we changed? Thesis numbers if you want to read minutes or hours because thes are for I see, um, let's say that your name is Mike. Andi, If we want to talk to you, we must say, Mike, blast zero. And if we want toe hear from you, we must say my last one. We want need to change your name each time we want toe read or write to and from, you know. So that's it. This is the closest thing that I could get. So getting you understand how this work. If you still have problem understanding this, I can explain it in more details. But for now, what you need to know is that you need to know that I see I d which is zero x 68 in this case on, you need to follow this number by zero. If you are writing on, buy one. If you are reading now this is done This is the first step. The second step will be sending the location or the address off the register inside the I C . Memory or the slave device memory. So our slave device is zero x 68 Inside it. There is zero X 00 memory location, which has seconds Data. Andi. It has also zero x 01 which has minute Minister data on we have zero x 02 which has hours that so if you need to read seconds, we have to talk with location zero inside the slave device. And if we need to read minutes, we have to talk with location one inside the safe device and so on. That's it. I think that this is everything that I could say about this. Ah, and how to use I square See protocol toe send and receive data to run from the DS 1307 very famous date on time. I see which is basically on I square See enabled. Ah, I see. So it allows communication via the eye square. See protocol. That's why it's very famous on. It doesn't require that fires to be connected to the pick or to any microcontroller. Thanks for watching this lesson. If you have any questions, please ask in the Q and A board have been learning from all of us here at Education Engineering team. This is ushered off on again. Please ask if you have any questions. 12. The End: This is the end of our course. If you have any question, you can ask it in the human A bolt or send us a message via any off our communication methods provided on our profile. I'll be more than happy to help you on to clear toe help clearing any issues that you have or any concerns again. You can teach us these methods via email via mobile number or what sub or via Skype at NGS I'm 89. Thanks for washing this course. I hope that you gained a lot of knowledge. Ah, If you haven't yet reviewed our course, please do help us improve on reach More audience. Thanks for going this far in our course. Ah, that's it for now. This is Ashraf from Educational Engineering team.