{"id":2131,"date":"2024-07-15T13:08:57","date_gmt":"2024-07-15T13:08:57","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2131"},"modified":"2024-07-15T13:43:02","modified_gmt":"2024-07-15T13:43:02","slug":"i2c-fundamental","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2131","title":{"rendered":"I2C fundamental"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-93ca784a6aa8b5254017c3924cc960a8 wp-block-paragraph\" style=\"color:#5c5c5c\">I<sup>2<\/sup>C( read as I square C) bus first introduced by Philips. Because of its simplicity and flexibility the I<sup>2<\/sup>C bus has become one of the most microcontroller bus system used for interfacing various IC. The I<sup>2<\/sup>C bus use only 2 bidirectional data lines for communicating with the microcontroller and the I<sup>2<\/sup>C protocol specification can support up to 128(27-1) devices attached to the bus.<br>The I<sup>2<\/sup>C protocol use master and slave method, the master which is usually the microcontroller which the slave can be any I<sup>2<\/sup>C devices such as serial EEPROM, I\/O Expender or even another microcontroller. As the name I<sup>2<\/sup>C use only 2 pin to control up to 127 devices.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"279\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-Bus-1024x279.jpg\" alt=\"\" class=\"wp-image-2140\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-Bus-1024x279.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-Bus-300x82.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-Bus-768x209.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-Bus.jpg 1439w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-98355f5ab78cb6aef2743392dba27091 wp-block-paragraph\" style=\"color:#5c5c5c\">The 2 controlling pin are \u2013<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5915a4ce4b78cf88f644dd6e60f2fa8d wp-block-paragraph\" style=\"color:#5c5c5c\">SDA: Serial Data pin.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-e1a10a4d0c1e5b1fcbf7e352351913fb wp-block-paragraph\" style=\"color:#5c5c5c\">SCL: Serial Clock (Synchronize clock)<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8e804f0b14fa6f7f1d8f29972a9547b6 wp-block-paragraph\" style=\"color:#5c5c5c\">Each slave devices have their own individual 7 bits of address length<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"73\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-1-1024x73.jpg\" alt=\"\" class=\"wp-image-2139\" style=\"width:757px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-1-1024x73.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-1-300x21.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-1-768x55.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-1.jpg 1415w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-b3bc7e6b31b809d267c5d1fc0b6ad37d wp-block-paragraph\" style=\"color:#5c5c5c\">Bit0 represent R\/W, For Read bit0= 1 and Write bit0 = 0.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8e93254195f54f044500e7f18352e96e wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s look at the bits available in AVR family-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"868\" height=\"1024\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TWI-Bit-868x1024.jpg\" alt=\"\" class=\"wp-image-2145\" style=\"width:780px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TWI-Bit-868x1024.jpg 868w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TWI-Bit-254x300.jpg 254w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TWI-Bit-768x906.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TWI-Bit-1302x1536.jpg 1302w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TWI-Bit-1736x2048.jpg 1736w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TWI-Bit.jpg 1873w\" sizes=\"(max-width: 868px) 100vw, 868px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"281\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-1-1024x281.jpg\" alt=\"\" class=\"wp-image-2147\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-1-1024x281.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-1-300x82.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-1-768x211.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-1.jpg 1326w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7f44b6b1d31ab37ba01afbc73511c886 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s make the user defined function of start, transmission and stop condition.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-7f55b27be321898c77e5bd7c93c79aed wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Start(void) -&gt; TWI start or repeat start<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t I2C_Start(void)\n{\n\tTWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT)|(1&lt;&lt;TWSTA); \/\/--- TWI Interrupt disable, TWI enable, TWI Start\n\twhile(!(TWCR&amp;(1&lt;&lt;TWINT)));           \/\/--- Wait until start condition is executed\n\treturn(TWSR&amp;0xF8);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-7c9f8f50894c49a5b24e9ae62e5dd371 wp-block-paragraph\" style=\"color:#6c8a97\">void I2C_Stop(void) -&gt; TWI stop condition<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid I2C_Stop(void)\t\n{\n    TWCR=(1&lt;&lt;TWSTO)|(1&lt;&lt;TWINT)|(1&lt;&lt;TWEN);\/\/--- TWI Interrupt disable, TWI enable, TWI Stop\n    while(TWCR&amp;(1&lt;&lt;TWSTO));\t              \/\/--- Wait until stop condition execution\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-efd98b3037512db193187284cad8ad5e wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Write(char data) -&gt; TWI write condition<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t I2C_Write(char data)\n{\n\tTWDR=data;\n\tTWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT);            \/\/--- TWI Interrupt disable, TWI enable\n\twhile(!(TWCR&amp;(1&lt;&lt;TWINT)));            \/\/--- Wait until start condition is executed\n\treturn(TWSR&amp;0xF8);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-1282d6b490cdcb454bb8887df89004e7 wp-block-paragraph\" style=\"color:#5c5c5c\">Read function can be done with Acknowledge or without Acknowledge.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-3c9d567ba3898fcd23e78a446df4817a wp-block-paragraph\" style=\"color:#6c8a97\">char I2C_Read_Ack(void) -&gt; Read with Acknowledge<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nchar I2C_Read_Ack(void)\t\n{\n    TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT)|(1&lt;&lt;TWEA); \/\/--- TWI Interrupt disable, TWI enable, TWI Accknowledge\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));           \/\/--- Wait until transmit is complete\n    return TWDR;\t\t\t             \/\/--- Return received data\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-7dcccda475f6cade6e0f4e6d00e11d4a wp-block-paragraph\" style=\"color:#6c8a97\">char I2C_Read_Nack(void) -&gt; Read without Acknowledge<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nchar I2C_Read_Nack(void)\t\t\n{\n    TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT);\t\/\/--- TWI Interrupt disable, TWI enable\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));\t\/\/--- Wait until transmit is complete\n    return TWDR;\t\t        \/\/--- Return received data\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-a2d33edef5d82d6b1387d185cc0593bf wp-block-paragraph\" style=\"color:#5c5c5c\">Those functions are used according to datasheet provided by the IC. We will Interface I<sup>2<\/sup>C in the next article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I2C( read as I square C) bus first introduced by Philips. Because of its simplicity and flexibility the I2C bus has become one of the most microcontroller bus system used for interfacing various IC. The I2C bus use only 2 bidirectional data lines for communicating with the microcontroller and the I2C protocol specification can support [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2131","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2131"}],"version-history":[{"count":13,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2131\/revisions"}],"predecessor-version":[{"id":2158,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2131\/revisions\/2158"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}