{"id":1012,"date":"2022-06-12T04:15:20","date_gmt":"2022-06-12T04:15:20","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1012"},"modified":"2022-06-12T04:19:26","modified_gmt":"2022-06-12T04:19:26","slug":"getting-started-with-stm32l053-timer-in-pwm-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1012","title":{"rendered":"Getting started with STM32L053: Timer in PWM Mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"681\" height=\"323\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/PWM-signal-with-its-two-basic-time-periods.png\" alt=\"\" class=\"wp-image-364\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/PWM-signal-with-its-two-basic-time-periods.png 681w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/PWM-signal-with-its-two-basic-time-periods-300x142.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/PWM-signal-with-its-two-basic-time-periods-400x190.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/PWM-signal-with-its-two-basic-time-periods-250x119.png 250w\" sizes=\"(max-width: 681px) 100vw, 681px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guid (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1005\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1005\" target=\"_blank\">here<\/a>), the timer interrupt was introduced with rate of 1Hz to blink the LED.In this guide, it shall present the usage of timer in PWM mode. This guide shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is PWM.<\/li><li>Configure the timer and GPIO to generate PWM signal.<\/li><li>Connection.<\/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. What is PWM:<\/h2>\n\n\n\n<p>PWM is a technique used to emulate \u201canalog signal\u201d by rapidly turning on-off the pin. This allow the mcu to vary the power delivered to the load such as motor (will be covered later). The PWM has three main characteristics;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Frequency:<\/li><\/ul>\n\n\n\n<p>which describe the duration time of the entire signal<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Duty cycle<\/li><\/ul>\n\n\n\n<p>The term duty cycle describes the proportion of \u2018on\u2019 time to the regular interval or \u2018period\u2019 of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Amplitude&nbsp;<\/li><\/ul>\n\n\n\n<p>Which is the voltage level of the PWM (3.3v for STM32L0).<\/p>\n\n\n\n<p>For more details, please check this wikipedia article (<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation\" target=\"_blank\">here<\/a>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure the timer and GPIO to generate PWM Signal:<\/h2>\n\n\n\n<p>First we need to locate which pins connected to TIMER2_CH1 and TIMER2_CH2.<\/p>\n\n\n\n<p>From the datasheet, we can find the related timer channels:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"291\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-1024x291.png\" alt=\"\" class=\"wp-image-1013\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-1024x291.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-300x85.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-768x218.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-1536x436.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-2048x581.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-1150x326.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-750x213.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-400x114.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.51.26-AM-250x71.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From the datasheet, the pins are related to timer1 are PA0 and PA1 and also the alternative function is AF2 and we can create a symbolic name 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;}\">#define TIM2_AF 0x02<\/pre><\/div>\n\n\n\n<p>Then we can enabled clock access to GPIOA as following:<\/p>\n\n\n\n<p>In order to enable clock access to the GPIOA, we need to go to reference manual and check the register named GPIO clock enable register.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"416\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-1024x416.png\" alt=\"\" class=\"wp-image-838\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-1024x416.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-300x122.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-768x312.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-1536x624.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-2048x832.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-1150x467.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-750x305.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-400x163.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-18-at-4.53.24-PM-250x102.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, we need to set bit0 to 1 and we can use CMSIS macros to do it 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;}\">\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Then we shall set the mode of the pin to be alternate fuction:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"598\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-1024x598.png\" alt=\"\" class=\"wp-image-1014\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-1024x598.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-300x175.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-768x449.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-1536x898.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-2048x1197.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-1150x672.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-750x438.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-400x234.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-6.57.01-AM-250x146.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\t\/*Set PA0 and PA1 to alternate function*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1;\n\tGPIOA-&gt;MODER&amp;=~(GPIO_MODER_MODE0_0|GPIO_MODER_MODE1_0);<\/pre><\/div>\n\n\n\n<p>Then set which alternate function shall be used from GPIO alternate function low register (AFR[0]):<\/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;}\">\t\/*Select which alternate function*\/\n\tGPIOA-&gt;AFR[0]|=(TIM2_AF&lt;&lt;0)|(TIM2_AF&lt;&lt;4);<\/pre><\/div>\n\n\n\n<p>Now we can configure the timer to generate PWM signal&nbsp;<\/p>\n\n\n\n<p>We start off by enabling clock access to TIMER2 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-759x1024.png\" alt=\"\" class=\"wp-image-997\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-759x1024.png 759w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-222x300.png 222w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-768x1037.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-750x1012.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-400x540.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM-250x337.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.22.23-AM.png 858w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/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;}\">\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;<\/pre><\/div>\n\n\n\n<p>Set the prescaler and ARR to 0 &amp; 100 respectively:<\/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;}\">\tTIM2-&gt;PSC=0;\n\tTIM2-&gt;ARR=100;<\/pre><\/div>\n\n\n\n<p>In order to configure the timer to work in PWM, this where TIM capture\/compare mode register 1 will come to play, this register responsible to CH1 and CH2.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"402\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-1024x402.png\" alt=\"\" class=\"wp-image-1015\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-1024x402.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-300x118.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-768x302.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-1536x603.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-2048x804.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-1150x452.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-750x295.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-400x157.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.04-AM-250x98.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"632\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-1024x632.png\" alt=\"\" class=\"wp-image-1016\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-1024x632.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-300x185.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-768x474.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-1536x948.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-2048x1263.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-1150x709.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-750x463.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-400x247.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.03.39-AM-250x154.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence, bit 5 and 6 for CH1 to be in PWM mode, bit 13 and 14 for CH2 to be in PWM mode.<\/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;}\">\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_2|TIM_CCMR1_OC1M_1|TIM_CCMR1_OC2M_2|TIM_CCMR1_OC2M_1;<\/pre><\/div>\n\n\n\n<p>Then, capture compare shall be enabled:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"234\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-1024x234.png\" alt=\"\" class=\"wp-image-1017\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-1024x234.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-300x69.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-768x175.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-1536x351.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-2048x468.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-1150x263.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-750x171.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-400x91.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-12-at-7.07.54-AM-250x57.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\tTIM2-&gt;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E;<\/pre><\/div>\n\n\n\n<p>Finally enable the timer:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"239\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1024x239.png\" alt=\"\" class=\"wp-image-998\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1024x239.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-300x70.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-768x179.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1536x358.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-2048x478.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-1150x268.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-750x175.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-400x93.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-05-at-7.31.56-AM-250x58.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">TIM2-&gt;CR1|=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p>In the header file, we can define an enum to distinguish the 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;}\">typedef enum\n\t{\n\n\tCH1=0,\n\tCH2=1\n\t}PWM_CH;\n<\/pre><\/div>\n\n\n\n<p>In order to update each channel separately:<\/p>\n\n\n\n<p>We shall use dedicated 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 update_duty_cycle(PWM_CH ch, uint16_t duty )\n\t{\n\tuint16_t max_duty=TIM2-&gt;ARR; \/*Limit the Duty cycle to ARR maximum value*\/\n\tif(duty&gt;max_duty){duty=max_duty;}\n\tswitch (ch)\n\t{\n\t\tcase CH1: TIM2-&gt;CCR1=duty; break;\n\t\tcase CH2: TIM2-&gt;CCR2=duty; break;\n\t\tdefault: break;\n\n\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>Hence, the header 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;}\">#ifndef TIM_H_\n#define TIM_H_\n\n#include &quot;stdint.h&quot;\n\ntypedef enum\n\t{\n\n\tCH1=0,\n\tCH2=1\n\t}PWM_CH;\n\n\nvoid tim2_pwm();\n\nvoid update_duty_cycle(PWM_CH ch, uint16_t duty );\n\n\n#endif \/* TIM_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>The source file 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;tim.h&quot;\n\n#include &quot;stm32l0xx.h&quot;\n\n#define TIM2_AF 0x02\n\n\nvoid tim2_pwm()\n\t{\n\n\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;\n\t\/*Set PA0 and PA1 to alternate function*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1;\n\tGPIOA-&gt;MODER&amp;=~(GPIO_MODER_MODE0_0|GPIO_MODER_MODE1_0);\n\t\/*Select which alternate function*\/\n\tGPIOA-&gt;AFR[0]|=(TIM2_AF&lt;&lt;0)|(TIM2_AF&lt;&lt;4);\n\n\t\/**Configure timer 2 in PWM mode *\/\n\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;\n\tTIM2-&gt;PSC=0;\n\tTIM2-&gt;ARR=100;\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_2|TIM_CCMR1_OC1M_1|TIM_CCMR1_OC2M_2|TIM_CCMR1_OC2M_1;\n\tTIM2-&gt;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E;\n\tTIM2-&gt;CR1|=TIM_CR1_CEN;\n\n\t}\n\nvoid update_duty_cycle(PWM_CH ch, uint16_t duty )\n\t{\n\tuint16_t max_duty=TIM2-&gt;ARR; \/*Limit the Duty cycle to ARR maximum value*\/\n\tif(duty&gt;max_duty){duty=max_duty;}\n\tswitch (ch)\n\t{\n\t\tcase CH1: TIM2-&gt;CCR1=duty; break;\n\t\tcase CH2: TIM2-&gt;CCR2=duty; break;\n\t\tdefault: break;\n\n\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>The main 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;}\">#include &quot;tim.h&quot;\n#include &quot;stm32l0xx.h&quot;\nint main(void)\n{\n\ttim2_pwm();\n\n\twhile(1)\n\t{\n\n\t\t\tfor (int i=0;i&lt;=100;i++)\n\t\t\t\t{\n\t\t\t\t\tupdate_duty_cycle(CH1,i);\n\t\t\t\t\tupdate_duty_cycle(CH2,100-i);\n\t\t\t\t\tfor (int j=0;j&lt;1000;j++);\n\n\t\t\t\t}\n\n\t\t\tfor (int i=100;i&gt;=0;i--)\n\t\t\t\t\t{\n\t\t\t\t\t\tupdate_duty_cycle(CH1,i);\n\t\t\t\t\t\tupdate_duty_cycle(CH2,100-i);\n\t\t\t\t\t\tfor (int j=0;j&lt;1000;j++);\n\n\t\t\t\t\t}\n\n\t}\n\n}\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/TIMER_PWM.zip\">TIMER_PWM<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/TIMER_PWM.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo:<\/h2>\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=\"STM32L053 in PWM mode\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/iCs-KiczlyE?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 guid (here), the timer interrupt was introduced with rate of 1Hz to blink the LED.In this guide, it shall present the usage of timer in PWM mode. This guide shall cover the following: What is PWM. Configure the timer and GPIO to generate PWM signal. Connection. Code. Demo. 1. What is 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":[3,2,11,12],"tags":[],"class_list":["post-1012","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\/1012"}],"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=1012"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1012\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1012\/revisions\/1021"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}