[Music] hello everyone welcome to controllers tech this is second video in the stm 32 timer series and today we will see how to use the pwm input mode i covered the pwm output in my previous video about the timers and that's why i decided to go with the pwm input today we will see how to measure the input frequency and the duty cycle using the pwm input mode so let's start by creating the project in cube id i am using stm32f446re give some name to the project and click finish so first of all i am selecting the external crystal for the clock let's see the clock configuration type in the crystal frequency of your controller choose this as per your board don't just use what i am using my board have eight megahertz crystal choose the hse for external crystal choose pclk for pll clock type the frequency you want to run the controller at and hit enter that's all about the clock configuration now let's configure the timer i am choosing timer 1 to provide the pwm signal pwm output has already been covered and you can watch the video on the top right corner timer 1 is connected to a pb 2 clock which is running at 180 megahertz i am choosing the auto reload period of 1800 so the output frequency will be 100 kilohertz this is it for the timer 1. now timer 2 is going to be used for the pwm input choose the clock source as internal clock and choose the combined channels as pwm input timer 2 channel 1 will be our main channel where we will provide the input clock you can see here the pin p-a-0 got selected now comes the parameters here i am keeping the prescaler zero so the timer clock will be same as a-p-b-1 and that is 90 megahertz the auto reload is set to maximum value this is a 32-bit register and that's why this value is very high if you have 16-bit register this will be 65535 so leave it to default next is the internal clock division to understand this we need to check the reference manual here in the control register 1 we have the clock division this clock division basically sets up the dead time and sampling clock the dts clock decides how fast we want to sample the input signal here are the settings for the dts clock i am keeping it to no division and that means the dts clock will be same as the internal clock we can skip the rest and come to the channel configuration here the input trigger is t i 1 f p 1 which means that the input from channel 1 after the filter and polarity selection will be connected to the capture 1. next we have is the parameters for channel 1. the polarity is set to rising edge which means that this channel is going to measure the rising edges of the signal i-c selection is direct and we will connect the input signal directly to this channel this channel is our main channel and it will measure the frequency now for the prescaler division ratio let's see the reference manual again here we are interested in the capture compare register we have the input capture prescaler and as you can see it basically controls the capture frequency these bits controls how often we want to do the capture let me explain this in detail let's assume that this is the input clock and we want to capture the rising edges a rising edge would be counted as an event now suppose we use the prescaler division 8 that would mean that interrupt will trigger after the 8 events and that means here this will keep happening after every eight events remember that if we do the captures at very high frequency then the interrupts will trigger very often and this will leave the rest of the code useless so this is an important parameters i am going with the highest possible value and that means the capture will be done once every eight events but the issue is that the cube m-x is not letting me choose this one don't worry about this we can set it in the code itself next comes the filter i am not going to use it but let me explain it anyway here we have the input capture filter the filter configures the frequency at which the input signal will be sampled it is also used as low pass filter but i couldn't find more information on the topic for now we will keep the filter zero and that means the sampling frequency will be d t s and that is the internal clock i will update you if i find more information on how to use these filters now comes the parameters for channel 2. the polarity should be opposite to the first channel and that's why it's falling edge the ic selection is indirect and this means we don't need to give the input to this channel it's internally connected to the channel 1 and it is used to calculate the duty cycle all right let's enable the interrupt for the timer 2. this is it for the setup click save to generate the code now let's create few variables where we can store the results now in the main function start the timer in input capture mode for channel 1. channel 2 can be used in the normal mode since we only need the interrupt from channel 1. after the input capture has been started we will start the pwm for timer 1. and finally set the value of capture compare register for the duty cycle everything related to pwm output has been covered in the previous video so if you don't understand pwm output please watch the video on the top right corner once the input capture actually does the capture an interrupt will be triggered and this input capture callback will be called now we will write the rest of the code inside this callback function here we will check if the interrupt was triggered by the channel 1 that is due to the rising edge of the input signal if it is then we will read the input capture value for the channel 1. then we will read the capture value for the channel 2 and use it to calculate the duty cycle i will explain this in a minute next calculate the frequency this 90 megahertz is my timer 2 clock now let's understand this calculation let's see the pwm input mode in the reference manual well we are mainly interested in this figure here you can see when the first rising edge is captured the counter is reset and so do the captures values when the second rising edge gets captures the capture value read this value is actually this time difference between the first and second edges so the period of the signal in the callback we also read the capture value for the falling edge using that value we can determine the pulse high time as a percentage of the total time this will be the duty cycle for the signal similarly using the clock frequency we can determine the frequency of the input signal i am just giving this delay in the while loop to test if the control enters the loop or not all right let's test this i have added all three variables to the live expression ok you can see the frequency is around 100 kilohertz and the duty is around 50 percent also note that the input capture value is pretty consistent let's put a break point in the while loop to check if the control enters the loop so the loop is also working all right since it's working fine for now let's test the higher frequencies now the auto reload is 450 making the output frequency 400 kilohertz and let's change the capture compare to 225 making the duty cycle 50. and there we have it around 400 kilohertz frequency and 50 duty cycle but the while loop isn't running anymore this is because the interrupts are getting triggered at very high rate making the rest of the code impossible to run this is where the input capture prescaler comes in right now it's set to division 1 but we will change it to highest and this will make the capture to be performed every 8 events let's test this and now you can see the while loop is running pretty well let's see how high air can we measure with this setup this time the auto reload is 180 making the frequency equal to 1 megahertz let's change the capture compare to 60 so the duty cycle will be 33 percent i would say the result is approximately correct the frequency is around 1 megahertz and the duty is 31 the while loop is still running so we can test even higher range now the frequency will be 2 megahertz and the capture compare is still 60 making the duty cycle equal to 66 percent this is still working all right surprisingly the while loop is still running honestly i wasn't expecting it but let's go even higher now the auto reload is 60 making the frequency equal to 3 megahertz and i am keeping the auto reload at 30 so the duty would be 50 percent here we do have approximate to what we need but the while loop is not running anymore i guess this is the limit for the while loop with the current setup even though it was able to measure the 2 megahertz clocks i would suggest that you keep the measurement below 1 megahertz for those frequencies the rest of the code can run pretty well too if you want to measure high frequencies well then you won't be able to run the rest of the code you can play with some other settings to improve the accuracy of the frequency measure before finishing this video i want to share one more thing it's quite possible to measure the high frequencies also in fact i did some tests and you can see the result in the picture the accuracy even at 10 megahertz frequency is quite phenomenal this is a different method by the way i was able to measure up to 18 megahertz input frequencies and even then the while loop was still able to run i was going to make the video on that method but as i was about to start i saw the cube mx has deprecated some settings that are needed for it anyway i will post the video on that method soon and if the cube mx allows you to do the setup you can use it this is it for the today's video i hope you understood the process and its explanation 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