{"id":679,"date":"2022-01-07T04:17:26","date_gmt":"2022-01-07T04:17:26","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=679"},"modified":"2022-01-07T04:17:28","modified_gmt":"2022-01-07T04:17:28","slug":"working-with-stm32f7-and-timers-pwm-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=679","title":{"rendered":"Working with STM32F7 and Timers: 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>In the previous guide (<a href=\"https:\/\/blog.embeddedexpert.io\/?p=629\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>), we discussed how to use the timer to generate accurate delay. In this guide, we shall look at another application for timer which is to generate PWM signal on two channels and fading two leds.<\/p>\n\n\n\n<p>In this guide, we will 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&nbsp;<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\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 STM32F7).<\/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>)\u00a0<\/p>\n\n\n\n<p><\/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><\/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=\"590\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-1024x590.png\" alt=\"\" class=\"wp-image-680\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-1024x590.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-300x173.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-768x443.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-1536x885.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-2048x1181.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-1150x663.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-750x432.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-400x231.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.39.33-AM-250x144.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From the datasheet, we can see that TIM2_CH1 is connected to PA0 and CH2 is connected to PA1.<\/p>\n\n\n\n<p>Hence, we can start configuring the timer and GPIO<\/p>\n\n\n\n<p>We start off by setting GPIO<\/p>\n\n\n\n<p>First thing first, we need to enable clock access to GPIOA as following: (for enabling clock access, check <a href=\"https:\/\/blog.embeddedexpert.io\/?p=514\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=514\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a> topic)<\/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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Then setting the pins in alternate mode 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;}\">GPIOA-&gt;MODER&amp;=~\tGPIO_MODER_MODER0|GPIO_MODER_MODER1; \/\/reset PA0 and PA1\nGPIOA-&gt;MODER|=\tGPIO_MODER_MODER0_1|GPIO_MODER_MODER1_1; \/\/set PA0 and PA1 as alternate mode<\/pre><\/div>\n\n\n\n<p>Then select which alternate function, in our case it is AF1<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"605\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-1024x605.png\" alt=\"\" class=\"wp-image-681\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-1024x605.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-300x177.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-768x454.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-1536x907.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-2048x1210.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-1150x679.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-750x443.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-400x236.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.48.25-AM-250x148.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;}\">#define AF01 0x01\nGPIOA-&gt;AFR[0]|=(AF01&lt;&lt;0)|(AF01&lt;&lt;4);<\/pre><\/div>\n\n\n\n<p>Now we can configure the timer to generate PWM signal <\/p>\n\n\n\n<p>We start off by enabling clock access to TIMER2 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;}\">RCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN; \/\/enable clock access to tim2<\/pre><\/div>\n\n\n\n<p>Set the prescaller to 0 (no divider)<\/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;PSC=0; \/\/set prescaller to 0 (no divider)<\/pre><\/div>\n\n\n\n<p>Set the maximum value to 255 ( this will give frequency of 62.5KHz)<\/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;ARR=255; \/\/set the maximum count value<\/pre><\/div>\n\n\n\n<p>Reset the current counter<\/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;CNT=0; \/\/reset the current count<\/pre><\/div>\n\n\n\n<p>Now, we configure the mode to be PWM mode as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"820\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-1024x820.png\" alt=\"\" class=\"wp-image-682\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-1024x820.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-300x240.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-768x615.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-1536x1230.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-2048x1640.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-1150x921.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-750x601.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-400x320.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.54.50-AM-250x200.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=\"820\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-1024x820.png\" alt=\"\" class=\"wp-image-683\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-1024x820.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-300x240.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-768x615.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-1536x1230.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-2048x1640.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-1150x921.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-750x601.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-400x320.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.56.18-AM-250x200.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;CCMR1=TIM_CCMR1_OC1M_1|TIM_CCMR1_OC1M_2|TIM_CCMR1_OC2M_1|TIM_CCMR1_OC2M_2; \/\/configure the pins as PWM<\/pre><\/div>\n\n\n\n<p>And enable the  channel as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"209\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-1024x209.png\" alt=\"\" class=\"wp-image-684\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-1024x209.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-300x61.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-768x157.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-1536x314.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-2048x419.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-1150x235.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-750x153.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-400x82.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-6.58.21-AM-250x51.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;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E; \/\/enbale channel1 and channel2<\/pre><\/div>\n\n\n\n<p>Finally, we enable the timer 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;}\">TIM2-&gt;CR1=TIM_CR1_CEN; \/\/enable timer<\/pre><\/div>\n\n\n\n<p>hence, the initializing function shall be this<\/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 GPIO_Init(void)\n{\n#define AF01 0x01\nRCC-&gt;AHB1ENR|=\tRCC_AHB1ENR_GPIOAEN;\/\/ enable clock access\nGPIOA-&gt;MODER&amp;=~\tGPIO_MODER_MODER0|GPIO_MODER_MODER1; \/\/reset PA0 and PA1\nGPIOA-&gt;MODER|=\tGPIO_MODER_MODER0_1|GPIO_MODER_MODER1_1; \/\/set PA0 and PA1 as alternate mode\n\t\nGPIOA-&gt;AFR[0]|=(AF01&lt;&lt;0)|(AF01&lt;&lt;4);\n\n\n}\nvoid  Timer2_init(void){\nRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN; \/\/enable clock access to tim2\nTIM2-&gt;PSC=0; \/\/set prescaller to 0 (no divider)\nTIM2-&gt;ARR=255; \/\/set the maximum count value\nTIM2-&gt;CNT=0; \/\/reset the current count\nTIM2-&gt;CCMR1=TIM_CCMR1_OC1M_1|TIM_CCMR1_OC1M_2|TIM_CCMR1_OC2M_1|TIM_CCMR1_OC2M_2; \/\/configure the pins as PWM\nTIM2-&gt;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E; \/\/enbale channel1 and channel2\nTIM2-&gt;CR1=TIM_CR1_CEN; \/\/enable timer\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Connection:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"712\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-1024x712.png\" alt=\"\" class=\"wp-image-685\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-1024x712.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-300x208.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-768x534.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-1536x1067.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-2048x1423.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-1150x799.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-750x521.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-400x278.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-07-at-7.07.34-AM-250x174.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\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;stm32f7xx.h&quot;                  \/\/ Device header\nvoid GPIO_Init(void);\nvoid Timer2_init(void);\nvoid delay(int ms );\n#define rate 255\nint main(void)\n{\n\nGPIO_Init();\nTimer2_init();\nwhile(1)\n{\nfor (int i=0;i&lt;rate;i++){\nTIM2-&gt;CCR1=i;\nTIM2-&gt;CCR2=rate-i;\ndelay(2);\n}\nfor (int i=255;i&gt;0;i--){\nTIM2-&gt;CCR1=i;\nTIM2-&gt;CCR2=rate-i;\ndelay(2);\n}\n\n}\n}\n\nvoid GPIO_Init(void)\n{\n#define AF01 0x01\nRCC-&gt;AHB1ENR|=\tRCC_AHB1ENR_GPIOAEN;\/\/ enable clock access\nGPIOA-&gt;MODER&amp;=~\tGPIO_MODER_MODER0|GPIO_MODER_MODER1; \/\/reset PA0 and PA1\nGPIOA-&gt;MODER|=\tGPIO_MODER_MODER0_1|GPIO_MODER_MODER1_1; \/\/set PA0 and PA1 as alternate mode\n\t\nGPIOA-&gt;AFR[0]|=(AF01&lt;&lt;0)|(AF01&lt;&lt;4);\n\n\n}\nvoid  Timer2_init(void){\nRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN; \/\/enable clock access to tim2\nTIM2-&gt;PSC=0; \/\/set prescaller to 0 (no divider)\nTIM2-&gt;ARR=255; \/\/set the maximum count value\nTIM2-&gt;CNT=0; \/\/reset the current count\nTIM2-&gt;CCMR1=TIM_CCMR1_OC1M_1|TIM_CCMR1_OC1M_2|TIM_CCMR1_OC2M_1|TIM_CCMR1_OC2M_2; \/\/configure the pins as PWM\nTIM2-&gt;CCER|=TIM_CCER_CC1E|TIM_CCER_CC2E; \/\/enbale channel1 and channel2\nTIM2-&gt;CR1=TIM_CR1_CEN; \/\/enable timer\n}\n\n\nvoid delay(int ms)\n\n{\n\nvolatile int i;\n\nfor(; ms&gt;0 ;ms--)\n\n{\n\nfor(i =0; i&lt;3195;i++);\n\n}\n\n}\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Demo:<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/IMG_7654.mp4\"><\/video><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude00 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we discussed how to use the timer to generate accurate delay. In this guide, we shall look at another application for timer which is to generate PWM signal on two channels and fading two leds. In this guide, we will cover the following: What is PWM Configure the timer and [&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-679","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\/679"}],"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=679"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/679\/revisions"}],"predecessor-version":[{"id":687,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/679\/revisions\/687"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}