HAL #8: HowTo - Timer PWM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to web learning chronology shared in today's video show a quick tutorial how to use the timer in the stm32 as always i'm using the stm32 l 0:53 you can use this example with any of the m32 devices so let's start so in this tutorial i'll show a quick how to set up the timer with the PWM how to generate the code with cuba mix and how to configure the timer to PWM one of the LEDs on the board if you don't know what PWM is it's best to stop this video and go to other videos that are online that explains exactly what is PWM that is pulse width modulation in this stm32 the internal oscillator can run up to 32 megahertz now we can decide that one second is 32 megahertz so this is 32 million so one second equals the oscillator that were running equals thirty-two megahertz equals 32 million in the device in the timer to use registered called prescaler the prescaler is a divider that we can put any value in this example i've decided this will be 32,000 another register that we have is a counter period i'm going to set it to 1000 and that will be equal one second and then we have a pause of 500 so let's recap this really quickly to understand what are the registers and what did i do so every second the oscillator oscillates at 32 megahertz that 32 million this clock goes in prescaler that divides the 32 million by 32,000 that means that the output of the prescaler will be 1,000 so every second will have one thousand process because the 32 million is divided by 32,000 that means that we have one thousand process for every second now in order to get an exact one second we can say that our counter period will be one thousand in other situation we can say that every second will be 500 pulses now because we've the cited that our counter period will be 1000 we need to decide on when will the change will happen this is this register that is called pose the pose I will set it to 500 so every 500 poses it will change now let's see how this works from the reference manual in the reference manual you can see how the timer is built this is timer 2 so first of all we decide that we're using an internal clock to drive the trigger control the trigger control will output the clock to this line into the prescaler so the prescaler is not doing anything except for dividing the amount of poses that we're getting from the trigger control so putting 32,000 in the prescaler what we'll get at output is 1000 this counter will run to 1000 and we get out of reload to 1000 and this counter is visible to the output capture compare all these outputs any setting that you'll put here will get compared to the counter and will change every time it will have a match so setting the channel 1 to 500 every time the counter will reach 500 you will see a change on the output in the cuba mix way to do this is start a new project select timer clock source internal clock this is the clock that will drive the timer and then select the channel 1 as PWM generator channel 1 we can do this also to the other channels in the parameter setting of the timer will put the prescaler put the pre skelter 32,000 who put the counter period to 1000 and will put the post to 500 now there is counter mode up or down and there is into clock division and there are outer real period all these things are disabled and you don't need to change them so how do we start the process of the timer there is not much to do this it's only peripheral initialization that goes that comes from the hall and then we only need to run one command of how time repeat William start with the timer that we want and that's it so the way it works is how timer PD will start we need to put which timer and which channel we want to start is how Tim PWM stop that does the same thing so we're going to toggle the lead of LD to the function that we're going to use is how Tim PWM start H team two and Tim channel one so we're back in the Kuban mix if you haven't used this software before I have your recommend to go and view the tutorial on how to use the cuba mix and then come back and continue so quickly I said that them awarded from using I don't have the oscillator of the 32 and high-speed so I'm going to disable them here's the timer to clock source internal clock channel 1 PWM generation on channel 1 let's go figure this so a prescaler this is how much we want to divide it before it goes through the timer 32,000 counter moved up or down our counter period so for every 1 second we're going to count 1,000 clocks because this is 32 megahertz divided by 32 it means that we have 1,000 clocks for every second and I want to do a PWM for one second so this is 1,000 clocks and every 500 clocks I want to have a change you can see that the timer one was set to PA zero and the LED is on PA five timer 2 can be also outputted to PA five so timer to channel one and you can see it moved from p 0 to P 5 and the LED is connected to PA 5 next clock figuration again this is really important because the timer itself is getting the internal clock this is the UM this is the clock that's being generated to the timer so 32 megahertz if this will change this also will change the timer itself I'll give it a name PWM as always I'm going to use a to do and I'll generate code open project a quick recap on the code user code begin to hard time our PWM start HT team we only need to do it once and again it depends on the code that you're going to do how team PWM and team channel 1 let's compile debug in order to view this I'm using my USB oscilloscope and I click resume and now we can see the polls that we have this is 500 millisecond thank you for watching I hope you enjoyed this don't forget to subscribe and click the notification bill so you know when I'm uploading a new video thank you
Info
Channel: Web learning
Views: 42,580
Rating: undefined out of 5
Keywords: Web Learning, Webleraning, DIY, arduino, arm cortex-m, atolic, diy, diy projects, electronics, stm32, stm32 cubemx, stm32cube, stm32cubemx, stm32f0, stm32f1, stm32f4, stm32f7, stm32g0, stm32h7, stm32l5, stm32wb, truestudio, v5, ver 5, web learning, HAL, TIM, Timer, STM32 Timer, stm32 timer tutorial, hal timer start, timer how it works, how timer in microcontroller works, STM32 TIM
Id: quZWGSEMyD0
Channel Id: undefined
Length: 7min 48sec (468 seconds)
Published: Sat Jan 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.