{"id":1024,"date":"2022-06-16T04:49:02","date_gmt":"2022-06-16T04:49:02","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1024"},"modified":"2022-06-16T04:53:16","modified_gmt":"2022-06-16T04:53:16","slug":"getting-started-with-stm32l053-timer-in-output-compare-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1024","title":{"rendered":"Getting started with STM32L053: Timer in Output Compare Mode"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/embedds.com\/wp-content\/uploads\/2010\/12\/250counts_CTCmode.png?ssl=1\" alt=\"\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1012\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1012\">here<\/a>), we took a look at the PWM mode in timer2 of STM32L053. In this guide, we shall take a look at the output compare where the the output will be toggled each time the timer overflow.<\/p>\n\n\n\n<p>In this guide we cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is output compare mode.<\/li><li>Configure the timer and GPIO for output compare mode.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is Output Compare Mode:<\/h2>\n\n\n\n<p>In this mode, the timer shall count to the maximum level (set by the ARR), the following will happen:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Assigns the corresponding output pin to a programmable value defined by the output compare mode.<\/li><li>Sets a flag in the interrupt status register.<\/li><li>Generates an interrupt if the corresponding interrupt mask is set.<\/li><li>Sends a DMA request if the corresponding enable bit is set.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"578\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-1024x578.png\" alt=\"\" class=\"wp-image-1025\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-1024x578.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-300x169.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-768x433.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-1536x867.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-2048x1156.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-1150x649.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-750x422.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-400x225.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.29.40-AM-250x141.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In our case, we will toggle PA0(TIM2_CH1) and PA1 (TIM2_CH2) based on out setting.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure the timer and GPIO for output compare mode:<\/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\"><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><\/p>\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>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;}\">\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p><\/p>\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\"><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><\/p>\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 2097 &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=2097;\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<figure class=\"wp-block-image\"><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<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"523\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-1024x523.png\" alt=\"\" class=\"wp-image-1026\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-1024x523.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-300x153.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-768x392.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-1536x785.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-2048x1047.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-1150x588.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-750x383.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-400x204.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-16-at-7.37.50-AM-250x128.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence, bit 4 and 5 for CH1 to be in toggle, bit 12 and 13 for CH2 to be in toggle.<\/p>\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;}\">TIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_0|TIM_CCMR1_OC1M_1|TIM_CCMR1_OC2M_0|TIM_CCMR1_OC2M_1;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Then, capture compare shall be enabled and invert the CH1:<\/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;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E|TIM_CCER_CC2P;<\/pre><\/div>\n\n\n\n<p>Set both CCR1 and CCR2 to zero:<\/p>\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;}\">\tTIM2-&gt;CCR1=0;\n\tTIM2-&gt;CCR2=0;<\/pre><\/div>\n\n\n\n<p>Finally, enable the timer:<\/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;CR1|=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>Hence the entire code 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;}\">\/*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\n\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=2097;\n\tTIM2-&gt;ARR=1000;\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_0|TIM_CCMR1_OC1M_1|TIM_CCMR1_OC2M_0|TIM_CCMR1_OC2M_1;\n\tTIM2-&gt;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E|TIM_CCER_CC2P;\n\tTIM2-&gt;CCR1=0;\n\tTIM2-&gt;CCR2=0;\n\tTIM2-&gt;CR1|=TIM_CR1_CEN;\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo:<\/h2>\n\n\n\n<p><\/p>\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=\"Output Compare Unit on STM32L053 Nucloe-64\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/d5_v2gokZ_U?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 guide (here), we took a look at the PWM mode in timer2 of STM32L053. In this guide, we shall take a look at the output compare where the the output will be toggled each time the timer overflow. In this guide we cover the following: What is output compare mode. Configure the [&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-1024","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\/1024"}],"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=1024"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1024\/revisions"}],"predecessor-version":[{"id":1029,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1024\/revisions\/1029"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}