Hello fellas.
Welcome back.
So in this very lesson we agreed to write code for the third most common use of timers which is measuring
the time between events and we're going to use this to program an ultrasonic stencil.
We're going to take the sensor and write it at drivers for it using what we've learned from timers.
And then we're going to test it out.
We're going to output the distance an obstacle in front of the ultrasonic sense or centimeters.
So let's get on with it.
We've seen that theory.
If you did not understand that bit don't worry we are going to write the actual code here.
And once again if there is still something you do not understand please live it in the comments section
or just send me a message.
So I have created a new forward or already calling this particular for the measuring time between event
and I've created a new project inside this folder and I'm calling this project or trust on essential.
I've done all of this just to shorten the the length of the video so I'll wait for you to do that once
you're done.
You come here the social group you rename it to the name of the microcontroller and then you said this
side to the target is Tim 4C One two three and then the source group is the court just one resource
group one folder created which is the application for that.
Then you come here.
You set your target options you change X to 16 and then your debugger you drop down here select still
areas a CDI and don't forget to take this right.
That being said let's create our main dot see far right click at new item that see and then the main
is the name of the file.
So.
So yes let's crack on.
We start by adding the head of file or file microcontroller by right click in and select in this and
then to improve readability or to increase readability we can just start by using symbolic names.
Remember we have four pillars for our ultrasonic module.
It's called The Echo pin.
The trigger pin the FEC and the G and D the echo and trigger pins digital pins that we have to use and
we are going to connect the echo pin to P P 6 and we are going to connect the trigger pin to be for.
So we define it as a symbolic names like this and then we're going to use the blue D just to create
some output to less as defined a plurality as well.
And there we have it.
Long let's open our main function into me
and let's just keep it open.
No we need to create a couple of functions we need to create actually for individual functions you have
to create one function to initialize the timer that we're going to use for detecting the edges in order
to measure the time between the the edges we're going to create another function to create a microsecond
delay for us and then we're going to fact to go in and create another function to initialize outputs
rather than initialize and in the main this time let's just create a new function for Port initialization
and we're going to create the final function to calculate to measure and calculate the distance of the
object in front of the truss on except so let's start with the first function the time must zero in
that we're going to use time as 0 for the arm for the the edge capture we're gonna use two timers like
we said in the previous lesson the first time will be used to detect the urges in order to measure the
time between them.
So we're going to use timers here of this for this and I'm calling this function time as you were in
it.
So we start by by providing them with the access to clock and then that time a series is going to be
used with the Echo pin you want to detect how long it takes the signal to return to the Echo pin we
want to detect when the equipping tens Hi.
So just to recap on what we supposed to do with the code we going to write the code to perform this
for us first before we set it off the ultra sonic module Hi wait for this signal to bounce back to the
echo we started timer when the Echo is detected and then when we stopped the time or when the echo goes
slow so this timer is the time of zero that we write in.
And so as we've defined above here the echo spin is connected to p B6 in order to detect the signal
changes are p B6 we need to use time of zero cause P B6 is connected to time as you are right.
So what we want to do like we saw in the theoretical part of the lesson is to start a timer when PBS
6 goes high and then stop the timer when P B6 goes low cost P B6 is the echo spin if you're forgotten
or are a bit confused just go to the previous listen and see how we initialize the the ultra sonic central
drivers but this the code and leave your questions below.
If you have any show we activate the clock for time on module 0 right and then we would provide clock
access to port B since P B6 is on point B after this we set p B6 we set previous which is the echo pin
us inputs begin by writing this to the direction register and then we digitally enable the echo pin
like this next will be to set the P C T O and the upscale the alternate function select to register
which we have to enable for the Echo pin because we're using the alternate function of P B6 after we
enable the alternate function we have to set the p CTO and we set it this way is a two step approach.
First right this particular hexadecimal code into the register and then write this.
Remember there is a table that tells us what to write and we treated this the previous lesson and it's
also available in the data sheet.
This code overhead.
Now let's go ahead to program the time a module 0 itself.
So as we said before you couldn't figure out a time might you have to first disable it and would do
this by accessing the control register.
And this this is a new way of showing you how to disable using this on this unsigned over here and until
the sets bit sets the first bit disabled is the same as type in equal to zero x zero.
Right.
But this is a friendly way of disabling it.
This one disables just the first bit whereas this disables or bits in the register.
So after that we go to the time a serial configuration register and then set it to 16 bits mode.
We know this already we've been dealing with this in the other examples to set it to 16 bits mode.
You just have to write for into this.
You have to register and we know why that is.
We can verify from the data sheet and this form is the same as X.
So for like this the next thing will be to go to the timer a mode register and with the mode register
we said we are setting characteristics of the timer.
We enable it as an edge time mode capture mode and an up counter.
That's why we have this hexadecimal code 2 x 1 7.
If you expand it this is what it is in binary and these two bits enables us a capture mode and then
this one bit.
This one here it's number two here enables it as an edge time mode and then bit over here sets as an
upper contact this one sets it as an up counter.
In fact we can verify this from the data sheet.
I'm gonna go to the data sheets and just show you how I came up with this code how I derive this.
So we could just open a data sheet to help mine here it's just a portion of it.
So you can just type and such.
Keep team Cabinet papers timer TMR here like we did when compute your credit you'd realize over here.
So this is it.
We use the first two bits and let's see where the first two bit does two in one voice bit two in one
useful bits you're in one it's defined us.
This is it it's known as the T.
And our values are defined this way.
This the value in that description if you set it to 0 x 3.
It's a capture mode and 0 x3 is 1 1 Cimino.
So the first 2 one sets it to the capture mode.
And then let's see the M with us the bits.
Number two the course this is bit 0 1 but to be 2 Let's see what the data sheet says about bits 2 over
here we should have been to number two is the capture mode so we set a too if we said the value one
is each time mode and that is why it's to set one course we wanted to get the edge time not edge count
so Beta 2 set to 1 and is the same today and our register has two sets one.
Now let's see bits to number four what is.
Number four two bits.
Number four is the current direction.
And it says when you set it to one the timer count up when counting up the time I started from fighting
off to 0 hands.
We have these by these.
That's why we have this here.
Each time mode capture mode up counter and this what it means.
I hope you understand.
Yes that's how it is derived.
So the next thing we have to do is to access the time I could register again and set more of the parameters
we require.
And this is it the time control register gives us the ability to set where we want to detect rise in
age only fall in error on the or both edges and then to set it for both ages you have to change the
timer eight events bit to 1 1 and this is fine in this register.
So just to do it for you and show you how it's derived a school to the data sheet and see what happens
when we set this bit to 1 1.
So I'm just going to go to the data sheet here.
I'll just search comeback here like this and now enter.
And this The Register we're looking for and
what.
Which bits are we looking for.
We just checked out again.
OK we're looking for the first four bits.
Right
so the first full bit the first bit is use to enable it rights.
The second bit is used to set a stall in this one.
It's two and three I use for the event.
So let's see it's two and three it's 2 3 2 3.
Here is it and distant description assess says when you set to see you X you you want to detect positive
edge which is res image.
We said to two X one you wanted to take the negative x mean and the fallen edge where you said to zero
x to me you want to detect both edges.
So when you compare this to x 3 where you converted and it's position you end up with a code 0 x 0 c
we convert to x 3 and I put this code cause this 2 x 3 is not at the beginning right.
Because this you X to me is the fourth.
Let's see it's the third and second bits of this register.
So if you're serious you in fact when you do the calculation you see it like it did.
I explained it to her form is what it looks like right.
This is why we have zero XY.
And if you combat this binary code to hexadecimal you will get this.
Finally what we have to do is to enable the timer by access in the quantum register then looks.
So this is all the risk for the time I see your initialization function with initialize it with these
parameters.
The next thing would be to write a delay function to provide us with a microsecond delay.
So that's what we are going to do next.
So the lesson is getting a bit long.
So let's continue the next lesson.
See you.