In this lesson we're going to demonstrate how to create an input capture firmware using the Cuban mix.
For those of you who are not familiar with input capture input capture is used to capture the timestamp
or final clearance of event.
Let's say you want to capture the such time erase an edge or a fallen off a digital sensor or curse.
Use inputs capture for such a case and I'm going to demonstrate how to quickly do input capture with
timers using a Cuban mix.
And if you want to learn more about input capture and then you want to you want further elaboration
do send me a message.
So let's kick off.
Let's just use the the items provided on the Discovery board which is a button in the LCD to demonstrate
this experiment as well.
So I'll come to my Cuban mics and create a new project so I just minimize this and then I'll come over
here for our new project.
I have the f 4 and this is my microcontroller and what I want to do is I want to connect the push button
of the discovery board to an input capture timer so that whenever I press that push button the the timestamp
of the event is stored so you can.
You can think of this as the exact time a particular event external event occurs.
This is actually how we program things like ultrasonic sensor or in the case of the ultrasonic sensor.
You want to detect exactly when the signal bounces back and it's detected and I have a tutorial of programming
ultrasonic sensor on cortex.
Dot com you can check that article out input capture is used to detect deal care and social events and
it becomes very useful when you're doing signal processing and when you're doing things that require
high accuracy and all of that.
So moving on I've just Claire pains by clicking over here and what I want to do is connect my push button
which is connected to key issue to a particular time.
So I could just click here like this and I've got options to connect it to time to channel one or a
time or five channel one.
I'd just like the timer to turn a one over here and as you can see like I said when you see this orange
color it means the configuration is incomplete.
We have to complete somewhere else.
So I have to go to timer two and continue the configuration.
What I want to do is enable Channel 1 Cause this one is Channel One as an input capture direct mode
like this so once that is done we need to come to the configuration tab over here and click on timer
to to set a timer configuration and what we want is we want to set it time pre-schooler like we did
in the other examples to the millisecond unit and to do that I'm going to put forty eight thousand here
and once I do that I'm going to set the time count more as an up counter and account period I'm going
to plan on enormous time contribution here meaning a one day time I count for five hundred seconds before
it reloads.
So this the size of my time just like a day has 24 hours.
My time has five hundred five hundred seconds.
So after 500 seconds the time I reset.
So yeah once that's set we have to come over here for polarity set and do we want to detect a rising
edge or the fallen edge of both edges of the signal.
I choose the raisin edge here I mean as soon as I press the button it's going to be detected and I have
to make sure the I see selection over here is direct and the pre-schooler division no division and I
can add a low pass filter here with a value 10 the maximum value over here is you to 15 that's the maximum
15.
So then I have to set these parameters and yet so once this is done we move on to these other settings
are going to erupt over here for a time or two so I'm just going to click here to enable.
Once we've done that I think we can go ahead and generate our code I'll click over her give my project
a name I call it input capture and then select my I.D. to call you vision 5 click Okay.
Once that is done I open the project like this.
And I can just expand and inspect and I'll just do it to bind everything.
And it's done rebuilding and before I move on I'll just I want to just update my debug here to reset
and run.
Okay and then okay.
So what I want is like like we did before in the other example we want to start the input copter timer
and this time we want to start it.
We interrupt not just start.
So I'm just going to come over here to Main and I'm going to come to the function stop to look for the
particular function for there and I'll just click the time I hear the timer module here to see the list
of API stuff with a timer and this one here gives me the entire list.
What I want is the timer inputs capture start which interrupts.
So it's overhead time ISIS starts interrupt so I just click it to see it and then I just copy this and
go and call this function in my main file or copy it like this.
Then take it to mean then come over here.
Then what I want to do is this is a pointer to my time a structure score.
HDI and to us is defined upstairs here up here like this and the channel this China one.
So I could just come over here.
It's a time on the school channel 1 and then we've started the interrupt next where we have to do it.
We have to declare the callback function for the interrupt.
Meaning what code.
What instructions do we want to run when the event occurs.
So we have to create a callback function for this and then declare the piece of code in that function.
Let's do that now so we can come back here and still look for the inputs capture callback whole.
This is a hard time I cup to call back.
We can just click over here and then copy it.
This is how we treat callbacks.
We copy it and then would declare the same.
We write the same implementation without the weak object here.
So I'm just going to copy this and take it to my main file and place it down here somewhere.
Just put it over here and then delete the week for her.
So in our callback function we can get the timestamp by using the whole time I get.
Counter API and this one is basically on the score on the score or a timer.
I am
gets count to this one here like this and it takes time out and starts here as an argument which is
H I T N H T I am sorry.
This one here returns the young timestamp which is an integer or want to do is start a timestamp in
a variable.
So I'm going to declare a global variable here.
I'm going to declare that you intend to follow it over you into that to underscore t volatile and I'll
call this timestamp
and what I'm going to do is I'm going to assign a timestamp going to start a return value in the timestamp
just gonna see timestamp
because this like this we are not quite complete.
The problem with this callback function is the works with all timers as you can see from the name it
didn't indicate what I'm using time or module one two three four five works with over time.
So one other thing we have to do is we have to tell this function to verify if the if the event is occurring
at the time or two and then if that is the case then gets the timestamp story to know variable to do
that we can use a simple if clause if we know the time or instance if h t i m we're not a timer instance
and then instance because time to do this now we get a timestamp we started timestamp here right.
So anytime you press the button you would store the timestamp the timestamp basically is the time at
which the events occurred or we might want to do also is to put a counter variable here and count any
time we pressed a button should tell us which press that button was and then issued increment.
So I can just pull you in 32 32 on the school put you in.
To underscore t auto counter here.
So this one.
Tell us the number of times you press the button.
Every time we press is just going to grant you increment this.
So I have to put this in a block.
Now
then like this.
So yeah our code looks complete.
We compile when when we press the button the timestamp is going to be stored in this for Apple.
This ball is going to store the number of count.
Remember the time is going to be moving forward.
Let's rebuild and test on our board.
See I download onto my board and then I just run the debugger and what I want to do is pull my global
variables in a watch window or right click over here at this to watch window one then to the counter
variable to watch window one as well.
Has to watch window one as well.
I'm going to run the code let's see I'm going to disable hexadecimal few here the code is running and
I'm going to press my button magic and see as I press the button this recorded a time which are pressed
a button and then press the wires.
So that time is still moving forward.
Let's see.
I'll press my button again and press twice this time.
Remember the arrow of time moves forward.
We are not resetting the time yet.
Time is still moving forward.
It's going to reload when it's reached.
Five hundred seconds.
Like we said we know what time my set is.
So I'm going to press again.
Press again I can press and you can see our low pass filter is work into.
Yeah.
There is no deep answer to this is good.
So this is how we we create an input capture timer using a Cuban makes.