{"id":2062,"date":"2024-07-14T17:06:10","date_gmt":"2024-07-14T17:06:10","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2062"},"modified":"2024-08-11T17:30:16","modified_gmt":"2024-08-11T17:30:16","slug":"graphical-lcd","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2062","title":{"rendered":"Graphical LCD"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-34edeafc1767d381efba6b84d85acd6e wp-block-paragraph\" style=\"color:#5c5c5c\">The TFT module is a 128*160 matrix, controlled by the onboard Sitronix ST7735 controller IC. It is a low cost faxable LCD module. TFT stands for Thin Flim Transistor which is a display screen technique used in LCD (liquid crystal display). An active component that serves as a switch for each pixel to be switched on and off is TFT. TFT display has color and any shape and design can be display. We need proper controlling SPI communication to display in TFT. First let\u2019s look the connection diagram.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"287\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/ST7735R-1024x287.jpg\" alt=\"\" class=\"wp-image-2065\" style=\"width:813px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/ST7735R-1024x287.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/ST7735R-300x84.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/ST7735R-768x215.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/ST7735R.jpg 1372w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-b28c7868b63847fa0a2d35b1568568ef wp-block-paragraph\" style=\"color:#5c5c5c\">Don\u2019t worry about the internal connection; it\u2019s already connected into the module available. Look at the pin available in the driver<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"278\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI-LCD-Pin-1024x278.jpg\" alt=\"\" class=\"wp-image-2068\" style=\"width:718px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI-LCD-Pin-1024x278.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI-LCD-Pin-300x81.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI-LCD-Pin-768x208.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI-LCD-Pin.jpg 1350w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3519473a6e2556a578e53f3adc111aa0 wp-block-paragraph\" style=\"color:#5c5c5c\">The module has both hardware and software reset function. We use software reset control. If you have available pin then used a GPIO pin to pull the reset line low for at least 1ms then set the GPIO pin to high state. After hardware reset or software reset wait at least 150ms then transfer data and command. How we send command or data to the TFT display? It is very simple. Pin6 is D\/C function for data and command control. D\/C is 1 for data and D\/C is 0 for command set. Use SPI initialize with maximum clock cycle with mode0. Here we only transfer data or command so need for return function. Look at the function for transfer data and command.<\/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\">\n<p class=\"has-text-color has-link-color has-extra-small-font-size wp-elements-f636f0e2f310ce784f5edcb7afc6e4ad wp-block-paragraph\" style=\"color:#252525\">void WriteCmd (uint8_t cmd) -&gt; Command send<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid WriteCmd (uint8_t cmd)\n{\n SPI_PORT&amp;=~(1&lt;&lt;SPI_DC_PIN);    \n \/\/ clear pin B1=DC; 0=command, 1=data\n SPDR = cmd; \/\/ initiate transfer\n while(!(SPSR&amp;(1&lt;&lt;SPIF)));\n SPI_PORT|=(1&lt;&lt;SPI_DC_PIN);     \n \/\/ set pin return DC high\n}\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color has-extra-small-font-size wp-elements-7737fe85437d88e93eef707458c5b791 wp-block-paragraph\" style=\"color:#252525\">void WriteByte (uint8_t b) -&gt; Data send<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid WriteByte (uint8_t b)\n{\n SPDR = b; \/\/ initiate transfer\n while(!(SPSR&amp;(1&lt;&lt;SPIF)));\n}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c5ae8219beacf00b3c1b70bf3ba0b9fd wp-block-paragraph\" style=\"color:#5c5c5c\">First initialize the TFT LCD. It need some command, here we used software reset.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid InitDisplay(void)\n{\n     WriteCmd(SWRESET);      \/\/ SWRESET  = 0x01\n     _delay_ms(1);           \/\/ 1mS is enough\n     WriteCmd(SWRESET);      \/\/ SWRESET  = 0x01\n     _delay_ms(150);         \/\/ wait 150mS for reset to finish\n     WriteCmd(SLPOUT);       \/\/ take display out of sleep mode, SLPOUT =  0x11\n     _delay_ms(150);         \/\/ wait 150mS for TFT driver circuits\n     WriteCmd(COLMOD);       \/\/ select color mode, COLMOD  = 0x3A\n     WriteByte(0x05);        \/\/ mode 5 = 16bit pixels (RGB565)\n     WriteCmd(DISPON);       \/\/ turn display on, DISPON = 0x29\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-05ba1ce1617795ad562cd2d7a475f3cb wp-block-paragraph\" style=\"color:#5c5c5c\">Why we send 0x05 command after select color mode? The TFT display has color combination-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"187\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-1024x187.jpg\" alt=\"\" class=\"wp-image-2081\" style=\"width:706px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-1024x187.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-300x55.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-768x140.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6.jpg 1401w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2e87b590f546f5bb0ff9659005223c20 wp-block-paragraph\" style=\"color:#5c5c5c\">We use 16bit or 2byte format which has Red color 5bit, Green color 6bit and Blue color 5Bit. Example for black 0x0000, blue 0x001F, red 0xF800, green 0x0400 etc. Now if we want to send color to display we have to send the hex value after memory 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 Write565 (int data, unsigned int count)\n{\nWriteCmd(RAMWR); \/\/ memory write command, RAMWR = 0x2C\nfor (;count&gt;0;count--)\n\t{\n      SPDR = data &gt;&gt; 8 ;   \/\/ write upper 8 bits\n      while(!(SPSR&amp;(1&lt;&lt;SPIF)));   \n\t  SPDR = data &amp; 0xFF;  \/\/ write lower 8 bit\n      while(!(SPSR&amp;(1&lt;&lt;SPIF)));\n\t}\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-46049222b64a4cb88354facf623ae527 wp-block-paragraph\" style=\"color:#5c5c5c\">In LCD just set the coordinate location (x,y) and send data to display, but TFT it is not so simple. Because the coordinates are not a single location, it is a rectangular region. To specify the region we need the controller commands CASET and RESET. The column address set command 0x2A, set the column boundaries or X coordinates. The row address set command 0x2B, set the row boundaries or Y coordinates.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid SetAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)\n{\n  WriteCmd(CASET); \/\/ set column range (x0,x1), CASET = 0x2A\n  WriteWord(x0);\n  WriteWord(x1);\n  WriteCmd(RASET); \/\/ set row range (y0,y1), RASET = 0x2B\n  WriteWord(y0);\n  WriteWord(y1);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-37604f294d1d4817d4b7cc5af0d489c4 wp-block-paragraph\" style=\"color:#5c5c5c\">Since the TFT display is nothing but a set of (128*160) dots pixel. To set any dots at any position is just send coordinates and colors. To set a pixel the function is the combination 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=\"\">\nvoid DrawPixel (uint8_t x, uint8_t y, int color) \/\/Pixel On\/Off\n{\n\tSetAddrWindow(x,y,x,y);\n\tWrite565(color,1);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-111040c243c66be63803f8ca494b2a76 wp-block-paragraph\" style=\"color:#5c5c5c\">With is pixel format we can write line, rectangular, triangle. ellipse or circle. But what about characters? Unlike the 16&#215;2 LCD character display modules, we can\u2019t send its ASCII code 0x39 and expect to see corresponding \u20189\u2019 is appear on the display. We previously build custom character in LCD display, now we implement previous technique to create \u20189\u2019. We use 6&#215;8 pixel region and draw in 5&#215;7 pixel. So that we have space to display the next digit to the display.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"358\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/pixel-map-1024x358.jpg\" alt=\"\" class=\"wp-image-2089\" style=\"width:772px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/pixel-map-1024x358.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/pixel-map-300x105.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/pixel-map-768x269.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/pixel-map.jpg 1369w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-917b08b40e8851609cea0f1fd0b986f1 wp-block-paragraph\" style=\"color:#5c5c5c\">Similarly other character can be draw by pixel mapping. Now if pixel is 0 set the pixel color background, if pixel is 1 set the pixel color to foreground. Calculate every character value and store in flash memory. Let\u2019s display \u20189\u2019 to the display \u2013<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nconst uint8_t FONT_CHARS&#x5B;9]&#x5B;5] PROGMEM ={\n     { 0x3E, 0x51, 0x49, 0x45, 0x3E }, \/\/ 0\n     { 0x00, 0x42, 0x7F, 0x40, 0x00 }, \/\/ 1\n     { 0x42, 0x61, 0x51, 0x49, 0x46 }, \/\/ 2\n     { 0x21, 0x41, 0x45, 0x4B, 0x31 }, \/\/ 3\n     { 0x18, 0x14, 0x12, 0x7F, 0x10 }, \/\/ 4\n     { 0x27, 0x45, 0x45, 0x45, 0x39 }, \/\/ 5\n     { 0x3C, 0x4A, 0x49, 0x49, 0x30 }, \/\/ 6\n     { 0x01, 0x71, 0x09, 0x05, 0x03 }, \/\/ 7\n     { 0x36, 0x49, 0x49, 0x49, 0x36 }, \/\/ 8\n     { 0x06, 0x49, 0x49, 0x29, 0x1E }, \/\/ 9\n}\nvoid PutCh (char ch, uint8_t x, uint8_t y, int color)\n{\n  int pixel;\n  uint8_t row, col, bit, data, mask = 0x01;            \n  SetAddrWindow(x,y,x+4,y+6);\n  WriteCmd(RAMWR);\n  for (row=0; row&lt;7;row++)\n      {\n      for (col=0; col&lt;5;col++)\n           {\n             data = pgm_read_byte(&amp;(FONT_CHARS&#x5B;ch]&#x5B;col]));\n             bit = data &amp; mask;\n             if (bit==0) pixel=BLACK;\n             else pixel=color;\n             WriteWord(pixel);\n           } mask &lt;&lt;= 1;\n      }\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-a069dcbaa75a39c5384bcdf3a55df7f6 wp-block-paragraph\" style=\"color:#5c5c5c\">Similarly other character can be writing into the TFT. One interesting thing is that you can rotate the screen.&nbsp; The TFT display can be orientated in any direction. Before going into programming fist clear all the display, it is simply by sending 0 to the pixel. Then orientate the screen. The 2 functions are.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid ClearScreen(void)\n{\n  SetAddrWindow(0,0,XMAX,YMAX); \/\/ set window to entire display\n  WriteCmd(RAMWR);              \/\/ memory write command, RAMWR = 0x2C\n  for (unsigned int i=40960;i&gt;0;--i) \/\/ byte count = 128*160*2\n\t{\n\t   SPDR = 0 ;\n       while(!(SPSR&amp;(1&lt;&lt;SPIF)));\n\t}\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid SetOrientation(int degrees)\n{\n  uint8_t arg;\n  switch (degrees)\n\t{\n\tcase 90: arg = 0x60; break;\n\tcase 180: arg = 0xC0; break;\n\tcase 270: arg = 0xA0; break;\n\tdefault: arg = 0x00; break;\n\t}\n  WriteCmd(MADCTL); \/\/axis control, MADCTL = 0x36\n  WriteByte(arg);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-d49225af8e9b97a85fb8d229507c7457 wp-block-paragraph\" style=\"color:#5c5c5c\">Here we write a program is to display load condition. We use pin PB7 for load. If PB7 is 1 display shows LOAD ON and circle turn green. If PB7 is 0 display shows LOAD OFF and circle turn green. 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\/***************************************************************\/\n\/********      Subeer Kumar Sarkar                    **********\/\n\/********      Electrical &amp; Electronic Engineer       **********\/\n\/***************************************************************\/\n\/********   Program for ghraphical display interface  **********\/\n\/***************************************************************\/\n\/***************************************************************\/\n  #include&lt;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\n  #include&quot;TFTLCD.h&quot;\n  int main(void)\n  {\n    init_spi();    \/\/Initilize SPI\n    InitDisplay(); \/\/ initialize TFT controller\n    _delay_ms(500);\n    ClearScreen();\n    SetOrientation(90);\/\/ value should 90,180,270\n    DDRB|=(1&lt;&lt;DDB7);   \/\/For load \n    char *str = &quot;Subeer Kumar Sarkar&quot;; \/\/ text to display\n    GotoXY(2,1); \/\/ position text cursor\n    WriteString(str,GREEN); \/\/ display text\n    char *ptr = &quot;RUET EEE 091109&quot;; \/\/ text to display\n    WriteStringXY(2,2,ptr,MAGENTA); \/\/ display text \n    DrawRect(12,8,126,24,RED);\n    while(1)\n\t   {\n\t\tPORTB|=(1&lt;&lt;PB7);  \n\t\tFillCircle(20,40,12,GREEN);\n\t\tWriteStringXY(6,4,&quot;LOAD ON &quot;,BLUE); \/\/ display text\n\t\t_delay_ms(3000);\n\t\tPORTB&amp;=~(1&lt;&lt;PB7);  \n\t\tFillCircle(20,40,12,RED);\n\t\tWriteStringXY(6,4,&quot;LOAD OFF&quot;,BLUE); \/\/ display text\n\t\t_delay_ms(3000);\n\t}\n return 0;\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"460\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TFT-Display-Output-1024x460.jpg\" alt=\"\" class=\"wp-image-2099\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TFT-Display-Output-1024x460.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TFT-Display-Output-300x135.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TFT-Display-Output-768x345.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/TFT-Display-Output.jpg 1198w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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\/Graphical-LCD-Software-Reset.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">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-f968807aaf30b87335ec47a9a8418c2e wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Graphical-LCD-Software-Reset.rar\">Graphical LCD Software Reset.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The TFT module is a 128*160 matrix, controlled by the onboard Sitronix ST7735 controller IC. It is a low cost faxable LCD module. TFT stands for Thin Flim Transistor which is a display screen technique used in LCD (liquid crystal display). An active component that serves as a switch for each pixel to be switched [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2062","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2062","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=2062"}],"version-history":[{"count":20,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2062\/revisions"}],"predecessor-version":[{"id":2637,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2062\/revisions\/2637"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}