{"id":2981,"date":"2024-09-18T15:32:14","date_gmt":"2024-09-18T15:32:14","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2981"},"modified":"2024-09-18T17:39:39","modified_gmt":"2024-09-18T17:39:39","slug":"exit-simple-ir-data-decoder","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2981","title":{"rendered":"EXIT : Simple IR Data Decoder"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-0c69187b04ca698c8b368d598c3c7a0c wp-block-paragraph\" style=\"color:#5c5c5c\">Sometime we want to turn on\/off some load using a remote. Audio, TV, AC remote available in market, which works on AM modulated signal. That when any key pressed, a burst of AM modulated signal send through IR transmitter. In AM modulated signal the carrier signal may be 30kHz\/36kHz\/38kHz\/56kHz. In market may IR Decoder IC available, but in our case we use TSOP1738. The IR receiver sensor works at active low, means if any IR signal of 38K&nbsp; AM modulated wave found the output of the IR receiver is GND otherwise it high all time. When any key pressed a brunch of data is send in form of 38K AM modulated signal. Following is the data send from remote.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"192\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function2-1024x192.jpg\" alt=\"\" class=\"wp-image-2984\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function2-1024x192.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function2-300x56.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function2-768x144.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function2-1536x289.jpg 1536w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function2.jpg 1580w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"204\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function3-1024x204.jpg\" alt=\"\" class=\"wp-image-2988\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function3-1024x204.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function3-300x60.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function3-768x153.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function3.jpg 1289w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-90cd56d21003d79da5b12aba226e90e9 wp-block-paragraph\" style=\"color:#5c5c5c\">Here L defines as line code and 0 and 1 represented as low pulse and high pulse.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f1a085670f8dd5aa731b9087a2944455 wp-block-paragraph\" style=\"color:#5c5c5c\">We have learn some basic idea now let\u2019s start with a remote. In my house I find an old TV remote. Let\u2019s start with this remote, from the IR decoding principle our first step is enable a GPIO pin with EXIT with rising edge &amp; falling edge detection with interrupt. In our example we use 2 GPIO pin for output pin. If 1 key is pressed GPIO pin toggle &amp; if 2 key is pressed then another GPIO pin will toggle. That every time 1\/2 key pressed a corresponding load on\/off. For detecting pulse we need a timer. Our logic is-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"635\" height=\"107\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function4.jpg\" alt=\"\" class=\"wp-image-2991\" style=\"width:421px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function4.jpg 635w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function4-300x51.jpg 300w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8bb81d693bd70a5188cea56e231c6b8b wp-block-paragraph\" style=\"color:#5c5c5c\">In our previous article we learn some basic timer function. We will enable a timer with 1us clocking. Let\u2019s look at some settings-<\/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<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"743\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function5.jpg\" alt=\"\" class=\"wp-image-2995\" style=\"width:385px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function5.jpg 741w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function5-300x300.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function5-150x150.jpg 150w\" sizes=\"(max-width: 741px) 100vw, 741px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"747\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function6.jpg\" alt=\"\" class=\"wp-image-2996\" style=\"width:358px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function6.jpg 683w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function6-274x300.jpg 274w\" sizes=\"(max-width: 683px) 100vw, 683px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-20f45f047783530f0595b10ea900ad6d wp-block-paragraph\" style=\"color:#5c5c5c\">Here the APB1 timer clock is 48MHz. Enable the global interrupt for EXIT with rising\/falling edge and we will programmer file is Interrupt generated c file. No program in main file. The interrupt file is something like stm32f0xx_it.c. We separate our program in 2 steps. First we detect the line code pulse, 1 pulse &amp; 0 pulse duration. For our simplicity we use large duration pulse as 1 pulses &amp; short duration pulse as 0 pulses. 1<sup>st<\/sup> program \u2013<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nextern TIM_HandleTypeDef htim17;\nuint32_t pulse_width;\nuint8_t flag=1,counter;\nuint16_t data_packet&#x5B;96];\nif(flag)\n  {\n    pulse_width=TIM17 -&gt; CNT ; \/\/ counter value\n\tdata_packet&#x5B;counter++]=pulse_width;\n\tTIM17-&gt;CNT=0;              \/\/ restart counter(optional) \n\tflag=0;\n  }\n  else\n  {\n   HAL_TIM_Base_Start(&amp;htim17); \/\/ start timer\n   flag=1;\n   TIM17-&gt;CNT=0;                \/\/ start counter with value 0\n  }\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"353\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/IR-Decoder-1024x353.jpg\" alt=\"\" class=\"wp-image-3025\" style=\"width:775px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/IR-Decoder-1024x353.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/IR-Decoder-300x103.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/IR-Decoder-768x265.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/IR-Decoder.jpg 1468w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-b8192b125ef7b06d7c9d8cd6c99f92b1 wp-block-paragraph\" style=\"color:#5c5c5c\">Here we use timer 17 as pulse duration and void EXTI4_15_IRQHandler(void) function for code detection. In our case maximum 96 pulses will count in any 36kHz IR signal. It very simple program for detect pulse wide and number of pulses. In my case I found header code around 4520us, digital 1 signal around 1645us, a digital 0 signal around 540us. Let\u2019s modify our program with header code and 1&amp;0 bit with the help of pulse duration.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define HEADER_CODE_START 4500    \/\/ Change according to IR Line Code\n#define HEADER_CODE_END   4600    \/\/ Change according to IR Line Code\n#define LOGIC_HIGH_START  1600    \/\/ Change according to BIT 1 Code\n#define LOGIC_HIGH_END    1700    \/\/ Change according to BIT 1 Code\n#define LOGIC_LOW_START   500     \/\/ Change according to BIT 0 Code\n#define LOGIC_LOW_END     600     \/\/ Change according to BIT 0 Code\n#define MAX_BIT           32      \/\/ Change according to Number of bit received\nuint16_t pulse_width;\nuint8_t flag=1,u8_header,IR_Code&#x5B;MAX_BIT]; \nint counter;\nif(flag)\n  {\n        pulse_width=TIM17 -&gt; CNT ;\n\t\tif(pulse_width&gt;HEADER_CODE_START &amp;&amp; pulse_width&lt;HEADER_CODE_END) {u8_header=1;counter=-1;}\n\t\tif(u8_header){\n\t\tif(pulse_width&gt;LOGIC_HIGH_START &amp;&amp; pulse_width&lt;LOGIC_HIGH_END)IR_Code&#x5B;counter]=1;\n\t\telse if(pulse_width&gt;LOGIC_LOW_START &amp;&amp; pulse_width&lt;LOGIC_LOW_END)IR_Code&#x5B;counter]=0;\n\t\tcounter++;\n\t\tif(counter&gt;(MAX_BIT-1)){\n\t\t\t\/\/ Your control logic here\n\t\t\tu8_header=0;\n\t\t              }\n\t\t}\n\t\tTIM17-&gt;CNT=0;\n\t\tflag=0;\n  }\n  else\n  {\n   HAL_TIM_Base_Start(&amp;htim17);\n   flag=1;\n   TIM17-&gt;CNT=0;\n  }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-bab09d160d69114c0a86bd34b53661a9 wp-block-paragraph\" style=\"color:#5c5c5c\">Change the parameter in the defined section. The remote transmit only 1 line code and 32bits of 1&amp;0. Please note that IR also transmit footer code\/ end code. In 32 bit there will be some header code &amp; checksum bits. Every key has its unique 32bit code. It\u2019s very difficult to set logic of 32bits, so we have to experiment the data in the debug section and note down every key pressed data. &nbsp;In my measurement I found a bit pattern in 0 to 9 key pressed. The data I found-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"376\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function7-1024x376.jpg\" alt=\"\" class=\"wp-image-3020\" style=\"width:722px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function7-1024x376.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function7-300x110.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function7-768x282.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function7.jpg 1090w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-d6730ffcf6ee6d8d0aa7534c172a00cb wp-block-paragraph\" style=\"color:#5c5c5c\">In my case I use PC10 as EXIT pin, PC11 &amp; PC12 as GPIO Outputs pins. If key 1 is pressed PC11 will toggle &amp; if key 2 is pressed PC12 will toggle. In 2<sup>nd<\/sup> part of program we introduce a comment for your logic, use it for your load control 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\/*****                       load condition             **********************\/\n\tif(IR_Code&#x5B;19] ==0 &amp;&amp; IR_Code&#x5B;18]==0 &amp;&amp; IR_Code&#x5B;17] ==0 &amp;&amp; IR_Code&#x5B;16]==1) \n\tHAL_GPIO_TogglePin(GPIOC,GPIO_PIN_11); \/\/key 1 pressed\n\tif(IR_Code&#x5B;19] ==0 &amp;&amp; IR_Code&#x5B;18]==0 &amp;&amp; IR_Code&#x5B;17] ==1 &amp;&amp; IR_Code&#x5B;16]==0) \n\tHAL_GPIO_TogglePin(GPIOC,GPIO_PIN_12); \/\/key 2 pressed\n\/*****************************************************************************\/\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-f9012f38f19bf446169d194708adfe14 wp-block-paragraph\" style=\"color:#5c5c5c\">The final program is based on the first program experimental data. The STM32 has strong debug feature, so that no need an external oscilloscope. In any remote first detect the signal code then make your changes. An AC remote is an organized remote with different function like fan, temperature, ionization, load control etc. In this article we are not decode all the function of a remote, just use it as a simple load control.<\/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 IR Decode\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/hv5jFE6Xt08?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<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<div class=\"wp-block-buttons is-layout-flex 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\/09\/1st-Phase.c\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--50)\" target=\"_blank\" rel=\"noreferrer noopener\">download 1<sup>st<\/sup> step.c<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex 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\/09\/2nd-Phase.c\" target=\"_blank\" rel=\"noreferrer noopener\">download 2<sup>nd<\/sup> step.c<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/IR-Remode-Control-STM32F0308.rar\">IR Remote Control STM32F0308.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometime we want to turn on\/off some load using a remote. Audio, TV, AC remote available in market, which works on AM modulated signal. That when any key pressed, a burst of AM modulated signal send through IR transmitter. In AM modulated signal the carrier signal may be 30kHz\/36kHz\/38kHz\/56kHz. In market may IR Decoder IC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-2981","post","type-post","status-publish","format-standard","hentry","category-stm-arm-tutorials"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2981","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=2981"}],"version-history":[{"count":34,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2981\/revisions"}],"predecessor-version":[{"id":3041,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2981\/revisions\/3041"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}