{"id":1105,"date":"2022-07-27T07:11:22","date_gmt":"2022-07-27T07:11:22","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1105"},"modified":"2022-07-27T07:28:02","modified_gmt":"2022-07-27T07:28:02","slug":"stm32-and-nextion-display-part-3-led-control","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1105","title":{"rendered":"STM32 and Nextion Display Part 3: LED Control"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/m.media-amazon.com\/images\/S\/abs-image-upload-na\/9\/AmazonStores\/ATVPDKIKX0DER\/4551205750cd023df17bdecd5dbbb3ee.w400.h400.jpg\" alt=\"\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1085\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1085\" target=\"_blank\">here<\/a>), we saw how to how to send strings from STM32 to Nextion display. In this guide, we shall see how to control two LEDs using Nextion display.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>LED control code.<\/li><li>Nextion display setup.<\/li><li>STM32 Setup.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. LED Control Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>For the two LEDs, they shall be connected to PA0 and PA1 as shown here:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM.png\" alt=\"\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence the code as following:<\/p>\n\n\n\n<p>led.c file:<\/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;}\">#include &quot;led.h&quot;\n\n\nvoid led_init()\n\t{\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE0_1;\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE1_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE1_1;\n\t}\n\nvoid led1_on()\n\t{\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BS0;\n\n\t}\n\nvoid led1_off()\n\t{\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BR0;\n\n\t}\n\nvoid led2_on()\n\t{\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BS1;\n\n\t}\n\nvoid led2_off()\n\t{\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BR1;\n\t}\n<\/pre><\/div>\n\n\n\n<p>led.h<\/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;}\">#ifndef LED_H_\n#define LED_H_\n#include &quot;stm32f4xx.h&quot;\nvoid led_init();\nvoid led1_on();\nvoid led1_off();\nvoid led2_on();\nvoid le2_off();\n#endif \/* LED_H_ *\/\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Nextion Display setup:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>First thing add two two-state buttons as following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"463\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-55-46-463x1024.png\" alt=\"\" class=\"wp-image-1106\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-55-46-463x1024.png 463w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-55-46-136x300.png 136w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-55-46-400x885.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-55-46-250x553.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-55-46.png 546w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure><\/div>\n\n\n\n<p>From right side from attribute, set the text to be LED1 and LED2 as following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"321\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-57-36-321x1024.png\" alt=\"\" class=\"wp-image-1107\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-57-36-321x1024.png 321w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-57-36-94x300.png 94w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-57-36-400x1277.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-57-36-250x798.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_09-57-36.png 426w\" sizes=\"(max-width: 321px) 100vw, 321px\" \/><\/figure><\/div>\n\n\n\n<p>Next, add two text area <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"933\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-933x1024.png\" alt=\"\" class=\"wp-image-1089\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-933x1024.png 933w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-273x300.png 273w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-768x843.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-1400x1536.png 1400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-1150x1262.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-750x823.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-400x439.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01-250x274.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-13-01.png 1524w\" sizes=\"(max-width: 933px) 100vw, 933px\" \/><\/figure><\/div>\n\n\n\n<p>Resize the text size according to your screen.<\/p>\n\n\n\n<p>From right side, attribute, note the objname and set txt_maxl to 30:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"322\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-14-15-322x1024.png\" alt=\"\" class=\"wp-image-1090\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-14-15-322x1024.png 322w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-14-15-94x300.png 94w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-14-15-400x1271.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-14-15-250x794.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-14-15.png 430w\" sizes=\"(max-width: 322px) 100vw, 322px\" \/><\/figure><\/div>\n\n\n\n<p>Then, we need to generate fonts as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"983\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-1024x983.png\" alt=\"\" class=\"wp-image-1093\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-1024x983.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-300x288.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-768x737.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-1150x1104.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-750x720.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-400x384.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38-250x240.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-16-38.png 1198w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Select the type of font you like and give a name to the font:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"878\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-18-44.png\" alt=\"\" class=\"wp-image-1094\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-18-44.png 900w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-18-44-300x293.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-18-44-768x749.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-18-44-750x732.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-18-44-400x390.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-18-44-250x244.png 250w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p>Press Generate font and save the file<\/p>\n\n\n\n<p>After that, press yes.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"376\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-19-26.png\" alt=\"\" class=\"wp-image-1095\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-19-26.png 960w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-19-26-300x118.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-19-26-768x301.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-19-26-750x294.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-19-26-400x157.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-19-26-250x98.png 250w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Next, highlight the first two-state button that labeled LED1:<\/p>\n\n\n\n<p>On Touch Release Event, add the 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;}\">print &quot;$&quot;\nprint &quot;0&quot;\nprints bt0.id,1\nprints bt0.val,1\nprint &quot;&amp;&quot;\n<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"449\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-1024x449.png\" alt=\"\" class=\"wp-image-1109\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-1024x449.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-300x132.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-768x337.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-1536x674.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-1150x504.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-750x329.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-400x175.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44-250x110.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-02-44.png 1578w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For the second two-state button:<\/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;}\">print &quot;$&quot;\nprint &quot;0&quot;\nprints bt1.id,1\nprints bt1.val,1\nprint &quot;&amp;&quot;\n<\/pre><\/div>\n\n\n\n<p>Now, debug the LCD code:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"330\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-1024x330.png\" alt=\"\" class=\"wp-image-1097\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-1024x330.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-300x97.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-768x248.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-1150x371.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-750x242.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-400x129.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30-250x81.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-24_07-21-30.png 1222w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When the first toggle is generated which is on command.<\/p>\n\n\n\n<p>You will get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"991\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-1024x991.png\" alt=\"\" class=\"wp-image-1110\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-1024x991.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-300x290.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-768x743.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-1150x1113.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-750x726.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-400x387.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48-250x242.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-04-48.png 1306w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For second state:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"988\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-1024x988.png\" alt=\"\" class=\"wp-image-1111\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-1024x988.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-300x290.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-768x741.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-1150x1110.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-750x724.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-400x386.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21-250x241.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/2022-07-27_10-06-21.png 1376w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As you can see, position 3 of the string is changed. The first two bytes are $ and 0. The third byte is the ID and it is 3 and 4 in my case. The last byte is &amp; character.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Upload the new code to Nextion display using SD-card method as shown in part 2.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. STM32 Setup:<\/h2>\n\n\n\n<p>In Nextion_uart setup function, we shall enable RX and IDLE line interrupt 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;}\">\tUSART1-&gt;CR1|=USART_CR1_RXNEIE|USART_CR1_IDLEIE;\n\tNVIC_EnableIRQ(USART1_IRQn);<\/pre><\/div>\n\n\n\n<p>For the buffer, we shall declare the 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;}\">#define NEXTION_BUFF_LENG 10\nvolatile char NEXTION_BUFF[NEXTION_BUFF_LENG];\nvolatile uint32_t itr;\nvolatile uint8_t end_of_data;<\/pre><\/div>\n\n\n\n<p>Buffer length is 10 byte.<\/p>\n\n\n\n<p>Character array of 10 bytes.<\/p>\n\n\n\n<p>itr which will hold the position of current position of the array.<\/p>\n\n\n\n<p>end of data to indicate the end of current transfer.<\/p>\n\n\n\n<p>For the interrupt handler:<\/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;}\">\nvoid USART1_IRQHandler(void)\n{\n    if (USART1-&gt;SR &amp; USART_SR_RXNE) { \/*If the interrupt source \n    the RX-Line fill the buffer*\/\n    \tNEXTION_BUFF[itr++]=USART1-&gt;DR;\n    }\n    \/*If it is from IDLE line, set the end of data to 1 and read DR register*\/\n    if(USART1-&gt;SR &amp; USART_SR_IDLE)\n    \t{\n    \t\tend_of_data=1;\n    \t\t(void)USART1-&gt;DR;\n    \t}\n    if(itr==NEXTION_BUFF_LENG){\n\n    \titr=0;\n    }\n}\n<\/pre><\/div>\n\n\n\n<p>Now, we shall check if the new data is available 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;}\">uint8_t received_data()\n\t{\n\treturn end_of_data;\n\t}\n\n<\/pre><\/div>\n\n\n\n<p>It shall return the value of end_of_data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For processing the data:<\/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;}\">\nvoid process_data()\n{\n\tif(NEXTION_BUFF[0]==0x24) \/*LED control*\/\n\t\t{\n\t\t\tif(NEXTION_BUFF[2]==0x03) \/*LED 1*\/\n\t\t\t{\n\t\t\t\tif(NEXTION_BUFF[3]==0x00){led1_off();nextion_write_text(&quot;t0&quot;,&quot;LED1 is off&quot;);}\n\t\t\t\tif(NEXTION_BUFF[3]==0x01){led1_on();nextion_write_text(&quot;t0&quot;,&quot;LED1 is on&quot;);}\n\t\t\t}\n\n\t\t\tif(NEXTION_BUFF[2]==0x04) \/*LED 2*\/\n\t\t\t{\n\t\t\t\tif(NEXTION_BUFF[3]==0x00){led2_off();nextion_write_text(&quot;t1&quot;,&quot;LED2 is off&quot;);}\n\t\t\t\tif(NEXTION_BUFF[3]==0x01){led2_on();nextion_write_text(&quot;t1&quot;,&quot;LED2 is on&quot;);}\n\t\t\t}\n\n\t\t}\n\n\tmemset(NEXTION_BUFF,0,NEXTION_BUFF_LENG);\n\titr=0;\n\tend_of_data=0;\n\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In the main function:<\/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;}\">#include &quot;delay.h&quot;\n#include &quot;string.h&quot;\n#include &quot;nextion_uart.h&quot;\n#include &quot;led.h&quot;\n\/*Nextion Buffer size*\/\n\n\nchar data_send[10];\nuint8_t counter;\nint main(void)\n\t{\n\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));\n\tled_init();\n\tsystick_init_ms(16000000);\n\tnextion_uar_init(115200,16000000);\n\n\twhile(1)\n\t\t{\n\t\t\tif(received_data()==1)\n\t\t\t\t{\n\t\t\t\t\tprocess_data();\n\t\t\t\t}\n\t\t}\n\t}\n\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/NEXTION_Display.zip\">NEXTION_Display<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/07\/NEXTION_Display.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Demo:<\/h2>\n\n\n\n<p><\/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 loading=\"lazy\" title=\"LED Control using Nextion display\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/diWpGJkmKg8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we saw how to how to send strings from STM32 to Nextion display. In this guide, we shall see how to control two LEDs using Nextion display. In this guide, we shall cover the following: LED control code. Nextion display setup. STM32 Setup. Code. Demo. 1. LED Control Code: For [&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-1105","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\/1105"}],"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=1105"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1105\/revisions"}],"predecessor-version":[{"id":1117,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1105\/revisions\/1117"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}