{"id":1818,"date":"2023-06-10T13:54:25","date_gmt":"2023-06-10T13:54:25","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1818"},"modified":"2023-06-10T13:54:28","modified_gmt":"2023-06-10T13:54:28","slug":"working-with-stm32-and-hc12-part-2-led-control","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1818","title":{"rendered":"Working with STM32 and HC12 Part 2: LED Control"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"418\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/HC12.jpg\" alt=\"\" class=\"wp-image-1811\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/HC12.jpg 518w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/HC12-300x242.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/HC12-400x323.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/HC12-250x202.jpg 250w\" sizes=\"(max-width: 518px) 100vw, 518px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1810\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1810\" target=\"_blank\">here<\/a>), we took a look at HC-12 RF module and setup the environment in STM32CubeIDE. In this guide, we shall continue the environment setup by initializing the user push button as external interrupt and library for HC-12.<\/p>\n\n\n\n<p><\/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>Enable User button as external interrupt.<\/li><li>Developing the library.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Enable user button as external interrupt:<\/h2>\n\n\n\n<p>To have deeper understand how to configure external interrupt for any GPIO, refer to this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=482\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=482\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Create new source and header file with name of exti.c and exti.h respectively.<\/p>\n\n\n\n<p>Within header 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;}\">#ifndef EXTI_H_\n#define EXTI_H_\n#include &quot;stdint.h&quot;\n\nvoid PC13_EXTI_Init(void);\nuint8_t button_Pressed();\nvoid clear_pressed();\n\n\n\n#endif \/* EXTI_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>Source 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;exti.h&quot;\n\n#include &quot;stm32f4xx.h&quot;\n\nvolatile uint8_t pressed;\n\n\nvoid PC13_EXTI_Init(void)\n{\n\t\/*Disable global interrupts*\/\n\t__disable_irq();\n\n\tRCC-&gt;AHB1ENR |=RCC_AHB1ENR_GPIOCEN;\n\n\tGPIOC-&gt;MODER &amp;= ~GPIO_MODER_MODE13;\n\n\t\/*Enable clock access to syscnf*\/\n\n\tRCC-&gt;APB2ENR|= RCC_APB2ENR_SYSCFGEN;\n\n\t\/*Select PORTC for EXTI13*\/\n\tSYSCFG-&gt;EXTICR[3] |= SYSCFG_EXTICR4_EXTI13_PC;\n\n\t\/*Unmask EXTI13*\/\n\n\tEXTI-&gt;IMR|=EXTI_IMR_IM13;\n\n\t\/*Select falling edge*\/\n\n\tEXTI-&gt;FTSR|= EXTI_FTSR_TR13;\n\n\tNVIC_EnableIRQ(EXTI15_10_IRQn);\n\n\t\/*Enable global interrupts*\/\n\t__enable_irq();\n}\n\nuint8_t button_Pressed()\n{\n\treturn pressed;\n}\n\nvoid clear_pressed()\n{\n\tpressed=0;\n}\n\nvoid EXTI15_10_IRQHandler(void)\n{\n\tif (EXTI-&gt;PR &amp; EXTI_PR_PR13)\n\t{\n\t\tpressed=1;\n\n\t\tEXTI-&gt;PR|=EXTI_PR_PR13;\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Thats all for external interrupt.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Developing the library:<\/h2>\n\n\n\n<p>Now create new source and header file with name of hc_12.c and hc_12.h respectively.<\/p>\n\n\n\n<p>Within the the header file, include the stdint 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;}\">#include &quot;stdint.h&quot;<\/pre><\/div>\n\n\n\n<p>Since there are multiple levels for power transmission, we can create enums for them as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"195\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-1024x195.png\" alt=\"\" class=\"wp-image-1819\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-1024x195.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-300x57.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-768x147.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-1536x293.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-2048x391.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-1150x219.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-750x143.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-400x76.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-10-at-4.25.04-PM-250x48.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/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;}\">typedef enum\n{\n\tP_Minus_1dBm =1,\n\tP_2dBm =2,\n\tP_5dBm =3,\n\tP_8dBm =4,\n\tP_11dBm =5,\n\tP_14dBm =6,\n\tP_17dBm =7,\n\tP_20dBm =8,\n}HC_12_Power_Typedef;<\/pre><\/div>\n\n\n\n<p>There are ton of configuration for HC-12, but we are interested in three parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Buadrate.<\/li><li>Channel.<\/li><li>Power.<\/li><\/ul>\n\n\n\n<p>We can create data structure for the configuration 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;}\">typedef struct\n{\n\tuint32_t Buadrate;\n\tuint8_t Channel;\n\tuint8_t Power;\n\n}HC_12_Config_Typedef;<\/pre><\/div>\n\n\n\n<p>Also, we need the following two functions:<\/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 HC_12_Config(HC_12_Config_Typedef *config);\nvoid HC_12_SET_Pin_init(void);\n<\/pre><\/div>\n\n\n\n<p>First one to configure HC-12 and the second one initialize set pin.<\/p>\n\n\n\n<p>Hence, the entire header 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;}\">#ifndef HC_12_H_\n#define HC_12_H_\n\n#include &quot;stdint.h&quot;\n\ntypedef enum\n{\n\tP_Minus_1dBm =1,\n\tP_2dBm =2,\n\tP_5dBm =3,\n\tP_8dBm =4,\n\tP_11dBm =5,\n\tP_14dBm =6,\n\tP_17dBm =7,\n\tP_20dBm =8,\n}HC_12_Power_Typedef;\n\ntypedef struct\n{\n\tuint32_t Buadrate;\n\tuint8_t Channel;\n\tuint8_t Power;\n\n}HC_12_Config_Typedef;\n\n\nvoid HC_12_Config(HC_12_Config_Typedef *config);\nvoid HC_12_SET_Pin_init(void);\n\n\n#endif \/* HC_12_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>Now, in the source file:<\/p>\n\n\n\n<p>Include the following header files:<\/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;hc_12.h&quot;\n#include &quot;hc_12_uart.h&quot;\n#include &quot;stdio.h&quot;\n#include &quot;stm32f4xx.h&quot;\n#include &quot;led.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare character array of 40 to hold AT commands to be send:<\/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;}\">unsigned char at_data[40];<\/pre><\/div>\n\n\n\n<p>Volatile variable to store the received character:<\/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;}\">volatile unsigned char uart1_rec;<\/pre><\/div>\n\n\n\n<p>Initializing the set pin 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;}\">void HC_12_SET_Pin_init(void)\n{\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\n\tGPIOA-&gt;MODER|= GPIO_MODER_MODE0_0;\n\n\tGPIOA-&gt;MODER&amp;= ~GPIO_MODER_MODE0_1;\n\n\tGPIOA-&gt;BSRR = GPIO_BSRR_BS0;\n\n}<\/pre><\/div>\n\n\n\n<p>The configuration 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;}\">void HC_12_Config(HC_12_Config_Typedef *config)\n{\n\tGPIOA-&gt;BSRR = GPIO_BSRR_BR0;\n\n\tsprintf(at_data,&quot;AT+B%d\\r\\n&quot;,config-&gt;Buadrate);\n\thc12_write_at_command(at_data);\n\n\tif (config-&gt;Channel &gt;127)\n\t{\n\t\tconfig-&gt;Channel=127;\n\t}\n\n\tif (config-&gt;Channel==0)\n\t{\n\t\tconfig-&gt;Channel=1;\n\t}\n\n\tsprintf(at_data,&quot;AT+C%d\\r\\n&quot;,config-&gt;Channel);\n\thc12_write_at_command(at_data);\n\n\tsprintf(at_data,&quot;AT+P%d\\r\\n&quot;,config-&gt;Power);\n\thc12_write_at_command(at_data);\n\n\tGPIOA-&gt;BSRR = GPIO_BSRR_BS0;\n\n}<\/pre><\/div>\n\n\n\n<p>The function takes pointer to the structure of HC_12_Config_Typedef.<\/p>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set set pin to low to enter AT mode.<\/li><li>Set the baud rate to the desired baud rate.<\/li><li>Set the channel from 1 to 27 with condition check.<\/li><li>Set the power of transmitter.<\/li><li>Set the set pin to high to exit the AT command.<\/li><\/ul>\n\n\n\n<p>Uart1 interrupt and callback 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;}\">void HC_12_Callback(unsigned char ch)\n{\n\tif (ch == '1')\n\t{\n\t\tled_toggle();\n\t}\n}\n\nvoid USART1_IRQHandler (void)\n{\n\n\tif(USART1-&gt;SR &amp; USART_SR_RXNE){\n\tuart1_rec=USART1-&gt;DR;\n\tHC_12_Callback(uart1_rec);\n\n\tUSART1-&gt;SR &amp;= ~USART_SR_RXNE;}\n\n\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Within the callback function, we check if the received character is 1, if it is 1, toggle the LED.<\/p>\n\n\n\n<p>Hence, the entire source file 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;}\">#include &quot;hc_12.h&quot;\n#include &quot;hc_12_uart.h&quot;\n#include &quot;stdio.h&quot;\n#include &quot;stm32f4xx.h&quot;\n#include &quot;led.h&quot;\n\n\n\nunsigned char at_data[40];\n\nvolatile unsigned char uart1_rec;\n\nvoid HC_12_SET_Pin_init(void)\n{\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\n\tGPIOA-&gt;MODER|= GPIO_MODER_MODE0_0;\n\n\tGPIOA-&gt;MODER&amp;= ~GPIO_MODER_MODE0_1;\n\n\tGPIOA-&gt;BSRR = GPIO_BSRR_BS0;\n\n}\n\n\nvoid HC_12_Config(HC_12_Config_Typedef *config)\n{\n\tGPIOA-&gt;BSRR = GPIO_BSRR_BR0;\n\n\tsprintf(at_data,&quot;AT+B%d\\r\\n&quot;,config-&gt;Buadrate);\n\thc12_write_at_command(at_data);\n\n\tif (config-&gt;Channel &gt;127)\n\t{\n\t\tconfig-&gt;Channel=127;\n\t}\n\n\tif (config-&gt;Channel==0)\n\t{\n\t\tconfig-&gt;Channel=1;\n\t}\n\n\tsprintf(at_data,&quot;AT+C%d\\r\\n&quot;,config-&gt;Channel);\n\thc12_write_at_command(at_data);\n\n\tsprintf(at_data,&quot;AT+P%d\\r\\n&quot;,config-&gt;Power);\n\thc12_write_at_command(at_data);\n\n\tGPIOA-&gt;BSRR = GPIO_BSRR_BS0;\n\n}\n\n\nvoid HC_12_Callback(unsigned char ch)\n{\n\tif (ch == '1')\n\t{\n\t\tled_toggle();\n\t}\n}\n\nvoid USART1_IRQHandler (void)\n{\n\n\tif(USART1-&gt;SR &amp; USART_SR_RXNE){\n\tuart1_rec=USART1-&gt;DR;\n\tHC_12_Callback(uart1_rec);\n\n\tUSART1-&gt;SR &amp;= ~USART_SR_RXNE;}\n\n\n}\n<\/pre><\/div>\n\n\n\n<p>Also, we need to update the hc_12 serial with two functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Send AT command:<\/li><\/ul>\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 hc12_write_at_command(unsigned char * ch)\n{\n\twhile(*ch)\n\t{\n\t\thc12_write_char(*ch);\n\t\tch++;\n\t}\n}<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Send String:<\/li><\/ul>\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 hc12_write_string(unsigned char * ch)\n{\n\twhile(*ch)\n\t{\n\t\thc12_write_char(*ch);\n\t\tch++;\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Hence the updated header 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;}\">#ifndef HC_12_UART_H_\n#define HC_12_UART_H_\n\n#include &quot;stdint.h&quot;\n\nvoid hc12_uart_init(uint32_t baud,uint32_t freq);\n\nvoid hc12_write_char(unsigned char ch);\n\nvoid hc12_write_at_command(unsigned char * ch);\n\nvoid hc12_write_string(unsigned char * ch);\n\n\n#endif \/* HC_12_UART_H_ *\/\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Within main.c:<\/p>\n\n\n\n<p>Include the following header files:<\/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;hc_12.h&quot;\n#include &quot;hc_12_uart.h&quot;\n#include &quot;led.h&quot;\n#include &quot;time_base.h&quot;\n#include &quot;exti.h&quot;<\/pre><\/div>\n\n\n\n<p>declare the configuration structure 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;}\">HC_12_Config_Typedef hc12Config;<\/pre><\/div>\n\n\n\n<p>in main function:<\/p>\n\n\n\n<p>Initialize everything:<\/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;}\">\tTicks_Init(16000000);\n\tPC13_EXTI_Init();\n\tled_init();\n\tHC_12_SET_Pin_init();\n\thc12_uart_init(9600, 16000000);<\/pre><\/div>\n\n\n\n<p>Configure HC-12:<\/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;}\">\thc12Config.Buadrate=9600;\n\thc12Config.Channel=1;\n\thc12Config.Power=P_20dBm;\n\tHC_12_Config(&amp;hc12Config);<\/pre><\/div>\n\n\n\n<p>In while 1 loop:<\/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;}\">while(1)\n\t{\n\t\tif (button_Pressed())\n\t\t{\n\t\t\tclear_pressed();\n\t\t\thc12_write_char('1');\n\t\t}\n\t}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the main.c 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;}\">#include &quot;hc_12.h&quot;\n#include &quot;hc_12_uart.h&quot;\n#include &quot;led.h&quot;\n#include &quot;time_base.h&quot;\n#include &quot;exti.h&quot;\n\nHC_12_Config_Typedef hc12Config;\n\nint main()\n{\n\n\tTicks_Init(16000000);\n\tPC13_EXTI_Init();\n\tled_init();\n\tHC_12_SET_Pin_init();\n\thc12_uart_init(9600, 16000000);\n\n\n\thc12Config.Buadrate=9600;\n\thc12Config.Channel=1;\n\thc12Config.Power=P_20dBm;\n\tHC_12_Config(&amp;hc12Config);\n\n\n\n\twhile(1)\n\t{\n\t\tif (button_Pressed())\n\t\t{\n\t\t\tclear_pressed();\n\t\t\thc12_write_char('1');\n\t\t}\n\t}\n\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\">9. Code:<\/h2>\n\n\n\n<p>You may download the entire source code from here:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/HC-12_STM32-2.zip\">HC-12_STM32-2<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/06\/HC-12_STM32-2.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\">10. Results:<\/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=\"HC-12 with STM32F4xx\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/SNll2gAM5H8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at HC-12 RF module and setup the environment in STM32CubeIDE. In this guide, we shall continue the environment setup by initializing the user push button as external interrupt and library for HC-12. In this guide, we shall cover the following: Enable User button as external interrupt. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,12],"tags":[],"class_list":["post-1818","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1818"}],"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=1818"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1818\/revisions"}],"predecessor-version":[{"id":1821,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1818\/revisions\/1821"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}