{"id":488,"date":"2021-10-11T05:21:59","date_gmt":"2021-10-11T05:21:59","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=488"},"modified":"2021-10-11T06:47:09","modified_gmt":"2021-10-11T06:47:09","slug":"working-with-spi-displays-and-stm32-driving-st7735","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=488","title":{"rendered":"Working with SPI displays and STM32: Driving ST7735"},"content":{"rendered":"\n<p>In the pervious guide, we took a look at how to send data over SPI (from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=466\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=466\" target=\"_blank\">here<\/a>). In this guide, we shall use SPI to drive a lcd and display some variables on it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-1024x768.jpg\" alt=\"\" class=\"wp-image-489\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-1024x768.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-300x225.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-768x576.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-1536x1152.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-2048x1536.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-1150x863.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-750x563.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-400x300.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6716-250x188.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>In this guide, we will need the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>SPI configuration : <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=466\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=466\" target=\"_blank\">here<\/a><\/li><li>Encoder: <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=430\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=430\" target=\"_blank\">here<\/a><\/li><li>Set core frequency to 100MHz : <a href=\"https:\/\/blog.embeddedexpert.io\/?p=454\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=454\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/li><\/ul>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Connection<\/li><li>ST7735 initialization<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Connection:<\/h2>\n\n\n\n<p>My ST7735 based TFT has the following pinout<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-1024x768.jpg\" alt=\"\" class=\"wp-image-490\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-1024x768.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-300x225.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-768x576.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-1536x1152.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-2048x1536.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-1150x863.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-750x563.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-400x300.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6717-250x188.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>ST7735 pinout<\/figcaption><\/figure><\/div>\n\n\n\n<p>The pins as following:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><tbody><tr><td>Pin<\/td><td>discerption <\/td><\/tr><tr><td>GND<\/td><td>Ground<\/td><\/tr><tr><td>Vcc<\/td><td>5V input<\/td><\/tr><tr><td>RESET<\/td><td>Reset pin<\/td><\/tr><tr><td>A0<\/td><td>Data\/Command<\/td><\/tr><tr><td>SDA<\/td><td>MOSI<\/td><\/tr><tr><td>SCL<\/td><td>SCK<\/td><\/tr><tr><td>CS<\/td><td>slave select<\/td><\/tr><tr><td>LED+<\/td><td>3v3 for LED<\/td><\/tr><tr><td>LED-<\/td><td>Ground of LED<\/td><\/tr><\/tbody><\/table><figcaption>pins discerption<\/figcaption><\/figure>\n\n\n\n<p>Since we will use STM32F411RE Nucelo-64, the connection shall be as following:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><tbody><tr><td>LCD pin<\/td><td>Nucleo Pin <\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>Vcc<\/td><td>5V<\/td><\/tr><tr><td>RESET<\/td><td>PB3<\/td><\/tr><tr><td>A0<\/td><td>PB4<\/td><\/tr><tr><td>SDA<\/td><td>PA7<\/td><\/tr><tr><td>SCL<\/td><td>PA5<\/td><\/tr><tr><td>CS<\/td><td>PB5<\/td><\/tr><tr><td>LED+<\/td><td>3v3<\/td><\/tr><tr><td>LED-<\/td><td>GND<\/td><\/tr><\/tbody><\/table><figcaption>pin connection<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2. ST7735 initializing: <\/h2>\n\n\n\n<p>We start off by defining some macros for reset, slave select and data\/command<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define LCD_RST1 GPIOB-&gt;BSRR|=GPIO_BSRR_BS3\n#define LCD_RST0 GPIOB-&gt;BSRR|=GPIO_BSRR_BR3\n\n#define\tLCD_DC1\tGPIOB-&gt;BSRR|=GPIO_BSRR_BS4\n#define\tLCD_DC0 GPIOB-&gt;BSRR|=GPIO_BSRR_BR4\n\n#define\tLCD_CS1\tGPIOB-&gt;BSRR|=GPIO_BSRR_BS5\n#define\tLCD_CS0\tGPIOB-&gt;BSRR|=GPIO_BSRR_BR5<\/pre><\/div>\n\n\n\n<p>In SPI, to enable the slave, we need to set CS pin to low<\/p>\n\n\n\n<p>To reset the LCD, we need to set the reset pin to low and set it back to high to release from reset<\/p>\n\n\n\n<p>For Data\/Command: When the pin is high, the LCD is in data mode (set pixel color for example). When pin is low, the lcd in command mode (set pixel position for example).<\/p>\n\n\n\n<p>hence, the write data command can be like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void static writecommand(uint8_t c) {\n  LCD_CS0;\n\tLCD_DC0; \/\/Set DC low\n  lcd7735_senddata(c);\n\tdelayuS(2);\n\tLCD_CS1;\n}\nvoid static writedata(uint8_t d) {\n\t\tLCD_CS0;\n\t\tLCD_DC1;\/\/Set DC HIGH\n\t\tlcd7735_senddata(d);\n\t\tdelayuS(2);\n\t\tLCD_CS1;\n}<\/pre><\/div>\n\n\n\n<p>The intializing sequence is as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\tLCD_RST0;           \/\/ RST=0 \n\t\tdelay(10);      \t\/\/delay for 10 millisecond\n\t\tLCD_RST1;           \/\/ RST=1\n\t\tdelay(10);          \/\/delay for 10 millisecond\n\t\tLCD_CS0; \t\t    \/\/enable LCD\n\t\tlcd7735_sendCmd(0x11); \n\t\tdelay(10);      \n\t\tlcd7735_sendCmd (0x3A); \/\/Set Color mode\n\t\tlcd7735_sendData(0x05); \/\/16 bits\n\t\tlcd7735_sendCmd (0x36);\n\t\tlcd7735_sendData(0x14);\n\t\tlcd7735_sendCmd (0x29);\/\/Display on<\/pre><\/div>\n\n\n\n<p>Regarding drawing some shapes etc can be found in the header file of ST7735.h<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>you can download the entire code from here<\/p>\n\n\n\n<p>link : <a href=\"https:\/\/github.com\/hussamaldean\/Enocder-Interface-with-stm32\" data-type=\"URL\" data-id=\"https:\/\/github.com\/hussamaldean\/Enocder-Interface-with-stm32\" target=\"_blank\" rel=\"noreferrer noopener\">Enocder-Interface-with-stm32<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo<\/h2>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/IMG_6718.mp4\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the pervious guide, we took a look at how to send data over SPI (from here). In this guide, we shall use SPI to drive a lcd and display some variables on it. In this guide, we will need the following topics: SPI configuration : here Encoder: here Set core frequency to 100MHz : [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,19,11,12],"tags":[],"class_list":["post-488","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-lcd","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/488"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=488"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/488\/revisions"}],"predecessor-version":[{"id":493,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/488\/revisions\/493"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}