STM32 TIMERS #4. INPUT CAPTURE || Frequency and Width

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to controllers tech this is fourth video in the timer series and today we will see how to use the input capture mode i have already covered few other timer videos and before watching this video i would suggest that you go through at least the pwm output and input videos you can check the first two videos in the timer playlist so let's start with this video and we will create a new project in cube id i am using stm32f446re controller give some name to the project and click finish here is our cube mx the first thing we will do is select the external high speed crystal for the clock i have eight megahertz crystal on the board and i want the controller to run at maximum 180 megahertz frequency make sure you input the correct crystal frequency as per your board now i am going to use the timer 1 for the pwm output which i will then measure with the timer 2 using the input capture enable the pwm generation with timer 1. if we check the data sheet of the controller here it's shown that the timer 1 is connected to the a-p-b-2 clock as per our clock setup the apb2 timer clock is running at 180 megahertz also the timer 2 is connected to the a-p-b-1 clock which is running at the 90 megahertz just keep this in mind while we set up the parameters for the timers let's start with the timer 1. right now the timer clock is running at 180 megahertz and if we use the prescaler of 180 this clock will come down to 1 megahertz i am further using the auto reload of 100 which would bring the pwm frequency to 10 kilohertz this is already explained in the pwm output video and if you don't understand it just watch that video in the timer playlist that's it for the pwm output now we will configure the timer to select the clock source as internal clock enable the input capture direct mode if you remember the apb1 clock is running at 90 megahertz so using a prescaler of 90 will bring it down to 1 megahertz this will be our timer clock frequency i am leaving everything else to default here if you want to know more about these parameters watch the pwm input video i have explained everything in that one note here that the polarity selection is set to rising edge by default also i am not using any prescaler division now the last thing we need to do is enable the timer to interrupt this is it for the setup click save to generate the project here is our main file timer 1 will be responsible for producing the pwm signal and we have the auto reload period of 100 here i am using the capture compare value of 50 so the duty will be 50 let's start the timer 1 in pwm mode now we will start the input capture in the interrupt mode for timer 2. once the rising edge is detected the interrupt will be triggered and the input capture callback will be called let's define few variables that we are going to use in this tutorial first i am going to measure the frequency this is the code for the same let's go through it first we will check if the interrupt is triggered by the channel 1. if the first reading hasn't been captured we will capture the first value this is basically the counter reading for the first rising edge now we will set the first captured to 1. when the second rising edge comes the interrupt will be triggered and this time it will capture the reading for the second rising edge now we will calculate the difference between the two readings if there is overflow we will get the difference backwards the timer 2 in my case is 32-bit timer and that's why i am using 32-bit value here next step is to calculate the reference clock this is something you need to define yourself timer clock is the frequency of the timer 2 which in my case is 90 megahertz we also need to define the prescaler i have set it to 90 during the setup process you can check it here so it will calculate the reference clock based on the parameters and finally the frequency will be reference clock divided by the difference value in the end we will reset the counter let's build it once to check for any errors all right we are good to go the timer 1 is producing the pwm signal at a frequency of 10 kilohertz and here we got the result of 10 kilohertz from the input capture the while loop also runs pretty well i am going to add some delay here to test the while loop a little better let's modify the timer 1 prescaler and now the output frequency will be 100 kilohertz let's see if the input capture is able to measure it all right here you can see the 100 kilohertz and the while loop is still running pretty well all right we will go a little more higher and this time the output frequency will be 200 kilohertz you can see it's able to measure it pretty well our while loop is still operational fine we will increase it a bit more now the output frequency is 300 kilohertz now it have started showing errors so this particular method is good for measuring frequencies up to few hundred kilohertz if you want to measure higher frequencies i will release another video using the dma so we were able to measure the input frequency using the rising edge triggers now we will see how to measure the width of the signal i will comment out this frequency code and we will write another one for measuring the width most of it will be similar to the frequency 1 so i am just using the same code and i will edit it in a while to measure the width of the signal we have to measure the time between the rising edge and the falling edge so here we want the interrupt to trigger in both cases to do so we will change the polarity to both edges when the rising edge will trigger the interrupt the counter value will be stored in the i c value 1. and similarly i c value 2 will store the counter value for the falling edge then we will measure the time difference between the two values this difference in time will depend on the input capture timer configuration this is why we need to add a little code here to change it to microseconds irrespective of the timer configuration you are using now this microsecond width will always show the width of the signal in microseconds let's test it if you notice here our pwm timer is running at 1 megahertz clock this means each count of the counter will take 1 microsecond if we are using the capture compare value of 20 the signal should be high for 20 microseconds here you can see we got the 20 microseconds width for it let's modify the timer prescaler and now it's running at 10 megahertz clock now each count in the counter takes 0.1 micro microsecond and we still have the capture compare value of 20. so the signal high time should be 2 micro seconds and you can see it here that's exactly what we got so the input capture works for measuring the frequency and the signal width you should use it to measure lower frequencies only up to few hundred kilohertz if you want to measure very high frequencies i will post another video using the dma if you want to measure both frequency and width together watch the previous videos in the timer playlist the one with pwm input mode you can download the code from the link in the description leave comments in case of any doubt keep watching and have a nice day ahead
Info
Channel: ControllersTech
Views: 40,572
Rating: undefined out of 5
Keywords: stm32, stm32f4, f103, discovery, nucleo, timer, pwm, example, tutorial, hal, input, output, duty, frequency, capture, interrpt, width, signal
Id: rh4pdNWKLJY
Channel Id: undefined
Length: 13min 56sec (836 seconds)
Published: Sat Sep 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.