{"id":2750,"date":"2024-08-22T14:58:57","date_gmt":"2024-08-22T14:58:57","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2750"},"modified":"2024-08-22T14:58:59","modified_gmt":"2024-08-22T14:58:59","slug":"getting-started-with-stm32g0-and-stm32cubeide-generate-pwm-with-its-complementary-signal","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2750","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE: Generate PWM with it&#8217;s complementary signal"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"400\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1.webp\" alt=\"\" class=\"wp-image-2751\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-1-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this continuous guide of PWM with DMA on STM32G0, we shall add the complementary signal with the generated PWM.<\/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>What is complementary signal of PWM.<\/li><li>STM32CubeMX Configuration.<\/li><li>Driver development.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is Complementary Signal of PWM:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>A&nbsp;<strong>Complementary Signal of PWM<\/strong>&nbsp;refers to two pulse-width modulation (PWM) signals that are inverses of each other. These signals are often used in power electronics, such as in the control of H-bridges, full-bridge converters, or other switching circuits where pairs of transistors are used.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Characteristics:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Duty Cycle Inversion<\/strong>: When one signal is high, the complementary signal is low, and vice versa. For example, if the primary PWM signal has a duty cycle of 40%, the complementary signal will have a duty cycle of 60%.<\/li><li><strong>Dead Time<\/strong>: A small, controlled delay (dead time) is typically introduced between the transitions of the two signals to prevent both switches (e.g., transistors or MOSFETs) from being on simultaneously, which could cause a short circuit.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Applications:<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Motor Control<\/strong>: In H-bridge configurations for driving motors, complementary PWM signals are used to control the direction and speed of the motor.<\/li><li><strong>DC-DC Converters<\/strong>: Complementary PWM signals are used in synchronous buck converters and other types of converters to control the switching of transistors.<\/li><li><strong>Inverters<\/strong>: In inverters, complementary PWM signals are used to create AC waveforms from DC sources.<\/li><\/ul>\n\n\n\n<p>Complementary PWM signals are crucial for efficient and safe operation in many power electronic circuits.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. STM32CubeMX Configuration:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Continue from the previous guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2744\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2744\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Open PWM_DMA.ioc file as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"888\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06-888x1024.jpg\" alt=\"\" class=\"wp-image-2753\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06-888x1024.jpg 888w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06-260x300.jpg 260w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06-768x885.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06-750x864.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06-400x461.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06-250x288.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-43-06.jpg 1022w\" sizes=\"(max-width: 888px) 100vw, 888px\" \/><\/figure>\n\n\n\n<p>From timers, select TIM1 and configure it as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"582\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-1024x582.jpg\" alt=\"\" class=\"wp-image-2754\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-1024x582.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-300x171.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-768x437.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-1536x874.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-2048x1165.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-1150x654.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-750x427.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-400x228.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/2024-08-22_17-22-20-250x141.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Channel 1 as PWM Generation CH1 CH1N. In similar manner, for channel 2.<\/p>\n\n\n\n<p>You can noticed that PA7 has been selected as TIM1_CH1N and PB0 as TIM1_CH2N.<\/p>\n\n\n\n<p>Save the project and this will generate the new code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Driver Development:<\/h2>\n\n\n\n<p>In main.c.<\/p>\n\n\n\n<p>In user code begin 2 section:<\/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;}\">  HAL_TIM_PWM_Start_DMA(&amp;htim1, TIM_CHANNEL_1, lookUp1, 200);\n  HAL_TIMEx_PWMN_Start(&amp;htim1, TIM_CHANNEL_1);<\/pre><\/div>\n\n\n\n<p>First function is explained in the previous guide.<\/p>\n\n\n\n<p>The function HAL_TIMEx_PWM_Start, it will start the PWM generate on the complementary pin of the PWM.<\/p>\n\n\n\n<p>The function takes two arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Pointer to the timer which is timer1 as htim1.<\/li><li>Timer channel which is timer channel 1.<\/li><\/ul>\n\n\n\n<p>For the second channel:<\/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;}\">  HAL_TIM_PWM_Start_DMA(&amp;htim1, TIM_CHANNEL_2, lookUp2, 200);\n  HAL_TIMEx_PWMN_Start(&amp;htim1, TIM_CHANNEL_2);<\/pre><\/div>\n\n\n\n<p>Thats it for the code.<\/p>\n\n\n\n<p>Save the project, build it and run it on your STM32G070.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>By using logic analyzer, probe PA7 and PA8 which they are TIM1_CH1N and TIM1_CH1 respectively.<\/p>\n\n\n\n<p>You should get the following for both channel:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"211\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-1024x211.png\" alt=\"\" class=\"wp-image-2755\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-1024x211.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-300x62.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-768x158.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-1536x316.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-2048x421.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-1150x236.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-750x154.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-400x82.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-22-at-5.41.07\u202fPM-250x51.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note that when one channel is high, the other is low and vice versa.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this continuous guide of PWM with DMA on STM32G0, we shall add the complementary signal with the generated PWM. In this guide, we shall cover the following: What is complementary signal of PWM. STM32CubeMX Configuration. Driver development. Results. 1. What is Complementary Signal of PWM: A&nbsp;Complementary Signal of PWM&nbsp;refers to two pulse-width modulation (PWM) [&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,11,12],"tags":[],"class_list":["post-2750","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2750"}],"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=2750"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2750\/revisions"}],"predecessor-version":[{"id":2756,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2750\/revisions\/2756"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}