{"id":2180,"date":"2024-07-15T15:56:33","date_gmt":"2024-07-15T15:56:33","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2180"},"modified":"2024-08-11T17:49:14","modified_gmt":"2024-08-11T17:49:14","slug":"i2c-lcd-pcf8574a","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2180","title":{"rendered":"I2C LCD (PCF8574A)"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">We already learn LCD interfacing in GPIO part. LCD Display requires lots of digital I\/O pin and the circuit requires more space. In market I<sup>2<\/sup>C LCD display are available and the interfacing is not so difficult. You just need the hardware and the I<sup>2<\/sup>C IC which is connected with that LCD. Usually in maximum I<sup>2<\/sup>C LCD module use PFC8574A. The PCF8574A is a I<sup>2<\/sup>C parallel port expander with latch outputs with high-current drive capability for directly driving LEDs. First look at the hardware how it is connected to LCD.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">Here pins A<sub>2<\/sub>:A<sub>1<\/sub> are connected to V<sub>cc<\/sub> via 1K\u03a9 resistance. SDA and SCL are internally pull-up by V<sub>cc <\/sub>so no need for an extra pull-up resistors. Let\u2019s look at the I<sup>2<\/sup>C protocol provide by datasheet<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"871\" height=\"492\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/PCF8574A.jpg\" alt=\"\" class=\"wp-image-2182\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/PCF8574A.jpg 871w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/PCF8574A-300x169.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/PCF8574A-768x434.jpg 768w\" sizes=\"(max-width: 871px) 100vw, 871px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"314\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-1-1024x314.jpg\" alt=\"\" class=\"wp-image-2185\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-1-1024x314.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-1-300x92.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-1-768x235.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-1.jpg 1437w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">Here the logical address is 0111 and the physical address is 111 (since all pin are connected to V<sub>cc<\/sub>). So the device has I<sup>2<\/sup>C address 0b0011 1111 or 0x3F. From the data sheet provided we need to start I<sup>2<\/sup>C then write the device address with write operation and send data. So our first step is to initialize the LCD ones with I<sup>2<\/sup>C start and device ADD+W then we just send data, no farther need of I<sup>2<\/sup>C start and device ADD+W command. We just need to change some user function in the LCD library. Let\u2019s go step by step. Since the device can work with 100KHz frequency.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-4 wp-block-paragraph\" style=\"color:#6c8a97\">void system_init(void) -&gt; I<sup>2<\/sup>C initialization function<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid system_init(void)\n{\n  TWSR=0x00;                \/\/--- Presclar = 1=4^0 \n  TWBR=0x20;                \/\/--- F_CPU=8MHz and Prescaler=1\n  TWCR=(1&lt;&lt;TWEN);           \/\/--- TWI Initialazion\n  TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT)|(1&lt;&lt;TWSTA); \/\/--- TWI Interrupt desable, TWI enable, TWI Start\n  while(!(TWCR&amp;(1&lt;&lt;TWINT)));             \/\/--- Wait untial start condition is executed\n  twi_write(0x3F&lt;&lt;1 | 0);  \/\/PCF8574A---- Device ID-- 0b0111 1110\n  LCD_INIT();              \/\/LCD Initializion\n  _delay_ms(100);          \/\/Some delay for LCD initialization\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s look the write command or data function. For write command the R\/S pin remain 0 and EN pin goes from 1 to 0.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-6 wp-block-paragraph\" style=\"color:#6c8a97\">void LCD_cmd(unsigned char cmd) -&gt; write command format<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid LCD_cmd(unsigned char cmd)\n{\n  twi_write(cmd|0b00001100); \/\/ EN = 1 and Backlight ON(Pin \u2013P3)\n  _delay_ms(2);             \n  twi_write(cmd|0b00001000); \/\/EN = 0 and Backlight ON\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-7 wp-block-paragraph\" style=\"color:#6c8a97\">void nibble_cmd(unsigned char LCD_value) &nbsp;-&gt; write command<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid nibble_cmd(unsigned char LCD_value)\n{\n  unsigned char display_nibble;\n  display_nibble=LCD_value&amp;0xF0;                 \/\/mask lower nibble because DD4-DD7 \n  pins are used\n  LCD_cmd(display_nibble);                       \/\/Send upper Nibble\n  display_nibble=((LCD_value&lt;&lt;4) &amp; 0xF0);       \/\/mask the upper Nibble\n  LCD_cmd(display_nibble); \n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#5c5c5c\">Now for data send R\/S pin is 1 and EN pin goes from 1 to 0.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-9 wp-block-paragraph\" style=\"color:#6c8a97\">void LCD_write(unsigned char data) -&gt; write data format<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvoid LCD_write(unsigned char data)\n{\n  twi_write(data|0b00001101); \/\/ EN = 1, R\/S=1 and Backlight ON\n  _delay_ms(2);\n  twi_write(data|0b00001001); \/\/ EN = 0, R\/S=1 and Backlight ON\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-10 wp-block-paragraph\" style=\"color:#6c8a97\">void nibble_data(unsigned char LCD_data) -&gt;&nbsp; write data<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid nibble_data(unsigned char LCD_data)\n{\n  unsigned char data_nibble;\n  data_nibble=LCD_data&amp;0xF0;                   \/\/mask lower nibble because DD4-DD7 pins are used\n  LCD_write(data_nibble);                      \/\/Send upper Nibble\n  data_nibble=((LCD_data&lt;&lt;4) &amp; 0xF0);         \/\/mask the upper Nibble\n  LCD_write(data_nibble); \n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">LCD other function remain the same. Download the I2CLCD.c and I2CLCD.h file, the main program as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n  #include&lt;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\n  #include&lt;util\/twi.h&gt;\n  #include&quot;I2CLCD.h&quot;\nint main(void)\n  {\n   system_init();\n   LCD_write_string(1,2,&quot;RUET EEE 091109&quot;);\n   while(1)\n     {\n       LCD_write_string(1,1,&quot;Subeer kumar Sarkar&quot;);\n       _delay_ms(500);\n       LCD_Display_Right();\n     }\n return 0;\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-12 wp-block-paragraph\" style=\"color:#252525\">If you don\u2019t see the character on the display rotates the potentiometer of the I<sup>2<\/sup>C LCD. The connection as shown in the figure.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"775\" height=\"266\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-LCD-Interfacing.jpg\" alt=\"\" class=\"wp-image-2201\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-LCD-Interfacing.jpg 775w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-LCD-Interfacing-300x103.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/I2C-LCD-Interfacing-768x264.jpg 768w\" sizes=\"(max-width: 775px) 100vw, 775px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/I2C-LCD.rar\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-13 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/I2C-LCD.rar\">I2C LCD.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We already learn LCD interfacing in GPIO part. LCD Display requires lots of digital I\/O pin and the circuit requires more space. In market I2C LCD display are available and the interfacing is not so difficult. You just need the hardware and the I2C IC which is connected with that LCD. Usually in maximum I2C [&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-2180","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2180","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=2180"}],"version-history":[{"count":15,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2180\/revisions"}],"predecessor-version":[{"id":2644,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2180\/revisions\/2644"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}