{"id":1141,"date":"2022-08-10T05:30:46","date_gmt":"2022-08-10T05:30:46","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1141"},"modified":"2022-08-10T05:30:48","modified_gmt":"2022-08-10T05:30:48","slug":"nec-protocol-decoding-with-stm32-part-2-peripherals-setup","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1141","title":{"rendered":"NEC Protocol Decoding with STM32 Part 2: Peripherals Setup"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"535\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-535x1024.png\" alt=\"\" class=\"wp-image-1139\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-535x1024.png 535w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-157x300.png 157w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-768x1469.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-803x1536.png 803w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-750x1435.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-400x765.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM-250x478.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-07-at-9.29.12-AM.png 806w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide, we took a look at the NEC IR protocol and setup STM32 to run at 64MHz. In this guide, we shall setup some required peripheral.<\/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>Hardware setup.<\/li><li>I2C and OLED setup.<\/li><li>Output Compare Interrupt.<\/li><li>External Interrupt.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Hardware Setup:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>The following parts are required:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>VS1838B which is IR Receiver and can modulate a carrier frequency of 38KHz.<\/li><li>I2C OLED Display<\/li><li>STM32F407 Discovery (or any stm32f4 will work).<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>The connection as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"622\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-1024x622.png\" alt=\"\" class=\"wp-image-1143\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-1024x622.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-300x182.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-768x467.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-1536x933.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-2048x1245.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-1150x699.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-750x456.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-400x243.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-10-at-7.46.22-AM-250x152.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The data of the IR Receiver connected to PA1.<\/p>\n\n\n\n<p>For the OLED display:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>STM32F407<\/th><th>SSD1306 OLED<\/th><\/tr><\/thead><tbody><tr><td>PB8 \/I 2C1<\/td><td>SCL<\/td><\/tr><tr><td>PB9 \/ I2C1<\/td><td>SDA<\/td><\/tr><tr><td>5V<\/td><td>Vcc<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. I2C and OLED Code:<\/h2>\n\n\n\n<p>You can download the OLED with I2C from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=613\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=613\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>However, the I2C code for 100MHz and in this case, the core frequency is 64MHz. Hence, the initialization of I2C 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;}\">uint8_t inited=0;\nvoid i2c_init(void){\nif(inited==0){\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOBEN; \/\/enable gpiob clock\nRCC-&gt;APB1ENR|=RCC_APB1ENR_I2C1EN; \/\/enable i2c1 clock\nGPIOB-&gt;MODER|=0xA0000; \/\/set pb8and9 to alternative function\nGPIOB-&gt;AFR[1]|=0x44;\nGPIOB-&gt;OTYPER|=GPIO_OTYPER_OT8|GPIO_OTYPER_OT9; \/\/set pb8 and pb9 as open drain\nI2C1-&gt;CR1=I2C_CR1_SWRST;\nI2C1-&gt;CR1&amp;=~I2C_CR1_SWRST;\nI2C1-&gt;CR2|=0x20;\nI2C1-&gt;CCR|=0x801b;\nI2C1-&gt;TRISE=0xa; \/\/output max rise \nI2C1-&gt;CR1|=I2C_CR1_PE;\ninited=1;\n}\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Output Comare Interrupt:<\/h2>\n\n\n\n<p>Since it is required to set the timer to have 10uS resolution, the prescaler shall be 640 and the ARR is 0xFFFF 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;}\">static void TIM2_init()\n\t{\n\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\t\tTIM2-&gt;PSC=PRESCALER_10_US;\n\t\tTIM2-&gt;ARR=0xFFFF;\n\t\tTIM2-&gt;DIER=TIM_DIER_CC1IE;\n\t\tTIM2-&gt;CCR1=TIME_120_MS;\n\t\tTIM2-&gt;CR1 |= TIM_CR1_ARPE;\n\t\tTIM2-&gt;EGR|=TIM_EGR_CC1G;\n\n\n\t\t\/\/TIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_0|TIM_CCMR1_OC1M_1;\n\t\tTIM2-&gt;CCER |= TIM_CCER_CC1E;\n\t\tTIM2-&gt;SR =0;\n\t\tNVIC_EnableIRQ(TIM2_IRQn);\n\n\t}<\/pre><\/div>\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;}\"> void TIM2_IRQHandler(void)\n\t{\n\n\t\tif(TIM2-&gt;SR &amp; TIM_SR_CC1IF){ \/*Interrupt from CC1*\/\n\t\tTIM2-&gt;SR =0; \/*Clear pending interrupt*\/\n\n\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. External Interrupt:<\/h2>\n\n\n\n<p>Since the data output of the IR receiver is connected to PA1, we need to set it as interrupt with internal pullup resistor:<\/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;}\">\nstatic void GPIO_init()\n\t{\n\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\tRCC-&gt;APB2ENR |=RCC_APB2ENR_SYSCFGEN;\n\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE1;\n\tGPIOA-&gt;PUPDR|=GPIO_PUPDR_PUPD1_0;\n\n\tSYSCFG-&gt;EXTICR[0]|=SYSCFG_EXTICR1_EXTI1_PA;\n\tEXTI-&gt;IMR|=EXTI_IMR_IM1;\n\tEXTI-&gt;RTSR |=EXTI_RTSR_TR1;\n\tEXTI-&gt;FTSR |=EXTI_FTSR_TR1;\n\tNVIC_EnableIRQ(EXTI1_IRQn);\n\n\t}<\/pre><\/div>\n\n\n\n<p>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 EXTI1_IRQHandler(void)\n\t{\n\tEXTI-&gt;PR |=EXTI_PR_PR1; \/*Clear pending interrupt*\/\n\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Code: <\/h2>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<p>Contains:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set the frequency to 64MHz.<\/li><li>I2C and OLED.<\/li><li>Output Compare interrupt.<\/li><li>External interrupt.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/IR_Remote_F407.zip\">IR_Remote_F407<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/08\/IR_Remote_F407.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In next part, we shall see how to develop the code required to decode the NEC protocol.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide, we took a look at the NEC IR protocol and setup STM32 to run at 64MHz. In this guide, we shall setup some required peripheral. In this guide, we shall cover the following: Hardware setup. I2C and OLED setup. Output Compare Interrupt. External Interrupt. 3. Hardware Setup: The following parts are [&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-1141","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\/1141"}],"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=1141"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions"}],"predecessor-version":[{"id":1145,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions\/1145"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}