{"id":363,"date":"2021-08-30T06:13:51","date_gmt":"2021-08-30T06:13:51","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=363"},"modified":"2021-08-30T14:56:45","modified_gmt":"2021-08-30T14:56:45","slug":"working-with-stm32-and-timers-pwm-mode-fading-2-leds","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=363","title":{"rendered":"Working with STM32 and Timers part 2: PWM mode, fading 2 LEDs"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><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 rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=356\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=356\" target=\"_blank\">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 <\/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 &#8220;analog signal&#8221; 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 &#8216;on&#8217; time to the regular interval or &#8216;period&#8217; 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 <\/li><\/ul>\n\n\n\n<p>Which is the voltage level of the PWM (3.3v for STM32f4).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For more details, please check this wikipedia article (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation\" target=\"_blank\" rel=\"noreferrer noopener\">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>According to the datasheet of STM32F411, PA0 and PA1 are connected to TIM2_CH1 and TIM2_CH2. Hence, we need to enable clock access to GPIOA as following: (for details refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=329\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=329\" 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>The we need to configure PA0 and PA1 as alternate function 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|=(1&lt;&lt;1)|(1&lt;&lt;3);<\/pre><\/div>\n\n\n\n<p>After that,  wee need to select AF1 for PA0 and PA1 since it is responsible to TIM2 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;AFR[0]|=(1&lt;&lt;0)|(1&lt;&lt;4);<\/pre><\/div>\n\n\n\n<p>After that, we enable 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>Hence we determine the prescaller and the ARR <\/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)\nTIM2-&gt;ARR=255; \/\/set the maximum count value\nTIM2-&gt;CNT=0; \/\/seset the current count<\/pre><\/div>\n\n\n\n<p>with prescaler of 0 and ARR of 255 we will get 62.5KHz (16000000\/255)<\/p>\n\n\n\n<p> Then we need to configure capture\/compare mode register (CCMR)<\/p>\n\n\n\n<p>The capture\/compare mode register has two register (CCMR1 and CCMR2)<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>CCMR1 is for channel 1 and channel 2 of the timer<\/li><li>CCMR2 is for channel 3 and channel 4 of the timer<\/li><\/ul>\n\n\n\n<p>Hence we need CCMR1<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"455\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-1024x455.png\" alt=\"\" class=\"wp-image-366\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-1024x455.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-300x133.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-768x341.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-1536x683.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-2048x910.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-1150x511.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-750x333.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-400x178.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.44.35-AM-250x111.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>For OC1M and OC2M we need to set them as PWM <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-1024x570.png\" alt=\"\" class=\"wp-image-367\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-1024x570.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-300x167.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-768x427.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-1536x854.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-1150x640.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-750x417.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-400x223.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM-250x139.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.46.08-AM.png 1758w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\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=(1&lt;&lt;5)|(1&lt;&lt;6)|(1&lt;&lt;13)|(1&lt;&lt;14); \/\/configure the pins as PWM<\/pre><\/div>\n\n\n\n<p>From capture\/compare enable register we need to enable channel1 and channel2<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"270\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-1024x270.png\" alt=\"\" class=\"wp-image-368\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-1024x270.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-300x79.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-768x203.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-1536x405.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-1150x304.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-750x198.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-400x106.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM-250x66.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.47.46-AM.png 1758w\" 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|=0x11; \/\/enbale channel1 and channel2<\/pre><\/div>\n\n\n\n<p>Finally, we enable the timer from control register 1 (CR1)<\/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=1; \/\/enable timer<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Connection<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"839\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-1024x839.png\" alt=\"\" class=\"wp-image-369\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-1024x839.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-300x246.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-768x629.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-1536x1259.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-1150x943.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-750x615.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-400x328.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM-250x205.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-30-at-8.19.34-AM.png 1896w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Connection<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-1024x768.jpg\" alt=\"\" class=\"wp-image-370\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-1024x768.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-300x225.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-768x576.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-1536x1152.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-2048x1536.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-1150x863.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-750x563.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-400x300.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6320-250x188.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>actual connection<\/figcaption><\/figure><\/div>\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;stm32f4xx.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(5);\n}\nfor (int i=255;i&gt;0;i--){\nTIM2-&gt;CCR1=i;\nTIM2-&gt;CCR2=rate-i;\ndelay(5);\n}\n\n}\n}\n\nvoid GPIO_Init(void)\n{\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\nGPIOA-&gt;AFR[0]|=(1&lt;&lt;0)|(1&lt;&lt;4);\nGPIOA-&gt;MODER|=(1&lt;&lt;1)|(1&lt;&lt;3);\n\n}\nvoid  Timer2_init(void){\nRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN; \/\/enable clock access tto tim2\nTIM2-&gt;PSC=0; \/\/set prescaller to 0 (no divider)\nTIM2-&gt;ARR=255; \/\/set the maximum count value\nTIM2-&gt;CNT=0; \/\/seset the current count\nTIM2-&gt;CCMR1=(1&lt;&lt;5)|(1&lt;&lt;6)|(1&lt;&lt;13)|(1&lt;&lt;14); \/\/configure the pins as PWM\nTIM2-&gt;CCER|=0x11; \/\/enbale channel1 and channel2\nTIM2-&gt;CR1=1; \/\/enable timer\n}\n\n\nvoid delay(int ms)\n\n{\n\nint i;\n\nfor(; ms&gt;0 ;ms--)\n\n{\n\nfor(i =0; i&lt;3195;i++);\n\n}\n\n}\n\n\n<\/pre><\/div>\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\/2021\/08\/IMG_6321-2.mov\"><\/video><\/figure>\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-363","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\/363"}],"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=363"}],"version-history":[{"count":4,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/363\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/363\/revisions\/377"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}