{"id":2696,"date":"2024-08-16T06:23:26","date_gmt":"2024-08-16T06:23:26","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2696"},"modified":"2024-08-16T10:47:23","modified_gmt":"2024-08-16T10:47:23","slug":"display-control-lcd","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2696","title":{"rendered":"Display Control LCD(GPIO)"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-01b6f76c54e09a791665339623bfb176 wp-block-paragraph\" style=\"color:#5c5c5c\">In this article we interface LCD display with STmirccontroller. Please see my LCD interface section in ATmel part. We use STM32F0308-DISCO board in this interface. Similarly other ST can be interface in same manner. We can use any GPIO pins. Let\u2019s first make a connection and change the logic according to the GPIO pins-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"248\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-with-ST-1024x248.jpg\" alt=\"\" class=\"wp-image-2699\" style=\"width:751px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-with-ST-1024x248.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-with-ST-300x73.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-with-ST-768x186.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-with-ST.jpg 1233w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-fdcd64b71ae25acd468c5a0e76e50497 wp-block-paragraph\" style=\"color:#5c5c5c\">Since we only write so connect R\/W to GND. In the LCD display the function of R\/S pin is-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"642\" height=\"75\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/RS.jpg\" alt=\"\" class=\"wp-image-839\" style=\"width:334px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/RS.jpg 642w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/RS-300x35.jpg 300w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"58\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/D4-D7-1024x58.jpg\" alt=\"\" class=\"wp-image-863\" style=\"width:742px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/D4-D7-1024x58.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/D4-D7-300x17.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/D4-D7-768x44.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/D4-D7.jpg 1348w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6e4a1353812684e938605e5cfdd074c9 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s just send 0x28 command for 2 lines and 5\u00d77 matrix (4-bit mode). Since we use only 4 upper bits, we have to send the Upper 4 bits then lower 4 bits. Let\u2019s make a function for that works-<\/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{\nunsigned char display_nibble;\ndisplay_nibble=LCD_value&amp;0xF0;           \/\/mask lower nibble because DD4-DD7 pins are used\nLCD_cmd(display_nibble);                 \/\/Send upper Nibble\ndisplay_nibble=((LCD_value&lt;&lt;4) &amp; 0xF0);  \/\/mask the upper Nibble\nLCD_cmd(display_nibble);                 \/\/Send lower Nibble\n}\n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-15f81cdd281a4abd5f2f9e8f526dece1 wp-block-paragraph\" style=\"color:#5c5c5c\">In this user file we just separate the bits and first send the upper 4 bits then lower 4 bits. The void LCD_cmd(unsigned char cmd) -> function is used to send the 4 bits. Let\u2019s look at the function and how it works-<\/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\tHAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,GPIO_PIN_RESET);                 \/\/ LCD_RS low\n\tif(cmd_4BIT&amp;0x80) HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1,GPIO_PIN_SET); \/\/ LCD_D7 high\n\telse HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1,GPIO_PIN_RESET);            \/\/ LCD_D7 low\n\tif(cmd_4BIT&amp;0x40) HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0,GPIO_PIN_SET); \/\/ LCD_D6 high\n\telse HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0,GPIO_PIN_RESET);            \/\/ LCD_D6 low\n\tif(cmd_4BIT&amp;0x20) HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,GPIO_PIN_SET); \/\/ LCD_D5 high\n\telse HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,GPIO_PIN_RESET);            \/\/ LCD_D5 low\n\tif(cmd_4BIT&amp;0x10) HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,GPIO_PIN_SET); \/\/ LCD_D4 high\n\telse HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,GPIO_PIN_RESET);            \/\/ LCD_D4 low\n\tHAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,GPIO_PIN_SET);                   \/\/ LCD_E high\n\tfor(uint8_t i=0;i&lt;200;i++);\n\t__nop();\n\tHAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,GPIO_PIN_RESET);                 \/\/ LCD_E low\n\tHAL_Delay(3);\n}\n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-0bdf16e2515560bb7bbfd5b691052d07 wp-block-paragraph\" style=\"color:#5c5c5c\">In both Command or Data write the LCD pin E need to high then after some delay turn the E pin low. We use __nop(); -> no operation function for E pin. Similarly if we wand display character \u2018A\u2019 or 0x41 the only parameter that need to change is the RS pin to high. i.e.-<\/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_write(unsigned char data)\n{\n\tHAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,GPIO_PIN_SET);                   \/\/ LCD_RS high\n\tif(cmd_4BIT&amp;0x80) HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1,GPIO_PIN_SET); \/\/ LCD_D7 high\n\telse HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1,GPIO_PIN_RESET);            \/\/ LCD_D7 low\n\tif(cmd_4BIT&amp;0x40) HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0,GPIO_PIN_SET); \/\/ LCD_D6 high\n\telse HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0,GPIO_PIN_RESET);            \/\/ LCD_D6 low\n\tif(cmd_4BIT&amp;0x20) HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,GPIO_PIN_SET); \/\/ LCD_D5 high\n\telse HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,GPIO_PIN_RESET);            \/\/ LCD_D5 low\n\tif(cmd_4BIT&amp;0x10) HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,GPIO_PIN_SET); \/\/ LCD_D4 high\n\telse HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,GPIO_PIN_RESET);            \/\/ LCD_D4 low\n\tHAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,GPIO_PIN_SET);                   \/\/ LCD_E high\n\tfor(uint8_t i=0;i&lt;200;i++);\n\t__nop();\n\tHAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,GPIO_PIN_RESET);                 \/\/ LCD_E low\n\tHAL_Delay(3);\n} \n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-bd503a74e99129b6765cae12867644e1 wp-block-paragraph\" style=\"color:#5c5c5c\">We done with command &amp; data section, let\u2019s initialize the LCD display with 4bits mode with the following commands-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"996\" height=\"265\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/initilize.jpg\" alt=\"\" class=\"wp-image-882\" style=\"width:410px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/initilize.jpg 996w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/initilize-300x80.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/initilize-768x204.jpg 768w\" sizes=\"(max-width: 996px) 100vw, 996px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-e3ece1ed50aeb59e63e1e2e3e89be109 wp-block-paragraph\" style=\"color:#5c5c5c\">Now send data to the exact positions-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"95\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-Position-1024x95.jpg\" alt=\"\" class=\"wp-image-2720\" style=\"width:787px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-Position-1024x95.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-Position-300x28.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-Position-768x71.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-Position-1536x142.jpg 1536w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LCD-Position.jpg 1588w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid LCD_write_string(unsigned char x, unsigned char y, unsigned char *str)     \n{\n  unsigned char lcd_address&#x5B;4]={0x80,0xc0,0x90,0xd0};\n  nibble_cmd(lcd_address&#x5B;y-1]+x-1);\n  uint8_t i=0;\n\twhile(str&#x5B;i]!=&#039;\\0&#039;)       \/\/ loop will go on till the NULL character in the string\n\t{\n\t  nibble_data(str&#x5B;i]);      \/\/ sending data on LCD byte by byte\n\t  i++;\n\t}\n}\n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-7afc8279a041f21cddec9cb57cc35ad8 wp-block-paragraph\" style=\"color:#5c5c5c\">Just paste it in the main program in while(1) and see what happened- <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; notranslate\" title=\"\">\n        LCD_write_custom(1,1,0);\n\t\tLCD_write_custom(2,1,1);\n\t\tLCD_write_custom(2,2,2);\n\t\tLCD_write_custom(1,2,4);\n\t\tLCD_write_custom(5,2,3);\n\t\tLCD_write_string(6,2,&quot;IoTthingHuB&quot;);\n\t\tLCD_write_string(5,1,&quot;        www&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;       www.&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;      www.i&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;     www.io&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;    www.iot&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;   www.iott&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;  www.iotth&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot; www.iotthi&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;www.iotthin&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;ww.iotthing&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;w.iotthingh&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;.iotthinghu&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;iotthinghub&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;otthinghub.&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;tthinghub.c&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;thinghub.co&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;hinghub.com&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;inghub.com &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;nghub.com  &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;ghub.com   &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;hub.com    &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;ub.com     &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;b.com      &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;.com       &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;com        &quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;om        w&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;m        ww&quot;);HAL_Delay(500);\n\t\tLCD_write_string(5,1,&quot;        www&quot;);HAL_Delay(500);\n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-d9c9e92bedbe173a1d8ec7c1746a7bcc wp-block-paragraph\" style=\"color:#5c5c5c\">For display custom character into LCD display please see my Build your own custom character article.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"STM32 LCD using GPIO\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/-lp5Or_cyLQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-a899f6278c85968b68fd9141db1781f5 wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>After connect LCD with STmicrocontroller sometime the LCD display shown no contents, in this case vary the variable resistance slowly until the display show characters. The contract adjust is very essential in LCD display so care should be taken. Some time the display may hang reset the mcu then try.<\/strong><\/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<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\/LED-GPIO-STM32F0308.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-medium-font-size wp-elements-6bd4c34b1f37b3c0ef3fb1d5d9595c75 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/LED-GPIO-STM32F0308.rar\">LED GPIO STM32F0308.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article we interface LCD display with STmirccontroller. Please see my LCD interface section in ATmel part. We use STM32F0308-DISCO board in this interface. Similarly other ST can be interface in same manner. We can use any GPIO pins. Let\u2019s first make a connection and change the logic according to the GPIO pins- Since [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2696","post","type-post","status-publish","format-standard","hentry","category-arm-standards-peripherals"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2696","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=2696"}],"version-history":[{"count":22,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2696\/revisions"}],"predecessor-version":[{"id":2736,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2696\/revisions\/2736"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}