Welcome back.
Let's continue where we left off in the previous lesson we created the initialization of function for
our time like zero which is in charge of detecting the echo and then measuring the time.
In this lesson we're going to stop by create an not delay microsecond and then go on to write the rest
of the driver.
So let's stop by writing the function it takes one argument.
It takes a you end argument and this is the time delay we want to program and then we start by unlocking
it with a clock source by activating the clock gate and mechanism for timer what your 0 uptime on what
you're 1 Sorry we use in time at 1 year we use time of zero up there and then we stop by disabling the
time up before making any changes by access in that time a control register.
After that we go to the timeout configuration register and set it to the sixteen bits mode right.
The next thing will be to go to that timer a mode register and choose it's us a periodic timer.
We'll do this by writing two X two or two into this register.
The next step will be to load the interval file into the Interpol load register and we've already said
in order to get it microsecond delete.
We have to load 16 into the interval a load register and minus one cause it begins content from zero.
And also because 16 because we're running at 16 megahertz.
If we were to run a 24 make hour load twenty four hundred megahertz we load a hundred.
The next thing will be to access the ICR register and set it high.
And then the next one would be to activate the time by going back to the time control register like
this said to me.
Finally we pull and wait for the time out to occur.
We used to loop to pull to create our delay like we did before and we forgot to to up this local variable
declaration.
We can just declare the int I up here like this right and our delete functions set and then next thing
as we initialize the port and write the final function which will be the function that actually measures
the distance of the obstacle in front of the ultrasonic sensor.
So this next function we're going to call it a port in it and this function is just going to perform
basic initialization of the pins and ports.
We are using for this particular project so we stopped by initialize in the cheapest I O port a course
we've connected a trig up into Port a then we can also initialize to keep an eye O port F test to initialize
the blue only to make the project more fun provide some outputs on the c we don't need to initialize
points bu cos we've initialized it over here it's working with a timer set trigger pin US output it's
like this because we've already defined trigger remember defined it so the code knows what trigger means
and then we've set the direction of the blue which is also an output thing
then we can digitally enabled the
condition enabled the pin.
We don't need this here this echo echo is connected to p p p.
So that's a mistake over there.
We need to do is enable this then enable that to wrap up this function we need to enable that then for
the blue early D like this.
Right.
So the third function is complete the next function is going to be the function that does the calculation
and outputs that distance of the object on the object close to the ultrasonic essential.
And this function is going to return a new end 32 and it's going to take no argument.
This what it looks like.
I'm going to call this function measure D measured distance and stuff like this.
Right.
So the first thing to do is to disable the trigger pin.
Remember when we spoke about the steps to detect the distance from the ultrasonic sense or we said you
first disable the trigger pin just to make sure it is disabled.
And then after that we give at least a 10 millisecond delay.
So let's do the first to disable the trigger pin like this this one here disables is just the trigger
pin and then we use our new delay function that delay microsecond and it says at least 10 milliseconds
so we can give twelve milliseconds to find and then after that we enable the trigger pin right and then
we delay at least 10 milliseconds again 10 microseconds sorry delay at least 10 microseconds again which
is 12.
Then finally we disable a trigger pin
right.
So this how we we send out the ultrasonic burst remember it sends aid 40 hertz or sonic bursts to this
how we send it refers first to say would trigger pin.
We wait for about 10 microseconds 10 microseconds and then we enable the trigger pin and then we wait
for another 10 microseconds and disable it.
Then the next thing we have to do is capture the return of the signal on the equipment.
We have to clear a time a capture flag by right and a one to the ICR register.
If you check the data sheet it says when you write a one two bits two of the ICR register you cleared
a time of capture flag and this base is known as the CCI entry bit.
It's an data sheet and we have to poll just like we did for the delay function.
We have to pull over.
We have to pull the raw interrupt the raw interrupt status register the right s and see if the second
bit is high and I've put a comment here.
I said Wait till captured by checking the C E R S which is the bit to check in.
That too is high.
That shows that an interrupt hustle can mean and capture has occurred.
And when that capture occurs or you have to do is stored it timestamp in a variable so we can store
it in this variable here known as high edge.
And we've not declared its variable.
That's why we we get getting this red marks.
But let's declare the variable.
Now we are going to have two of such variables one is high end and the other is low high edge sorry.
And the other is little H.
So yeah.
So we pull to see whether the B2 is high which indicates that there's been a capture and then we store
it in the T R and A T R is basically this statement basically means that time at which the last edge
event took place should be stored in high edge.
Right.
Because that timestamp is stored in our in a t r register because we've with the course we've configured
the timer to edge time mode.
It stores the timestamp when we did the edge count mode.
It stores the number of H counted in the T R because we can't forget earlier on to each time it stores
the timestamp.
And now we have timestamp stored over here in this variable.
So that's what we.
We want to do is get the next edge.
Remember when a rise in your cast the next one is at fault an age when a fallen angel cast the next
one is a rise in age.
So we just need to do the same thing basically.
And then we'll get food and age.
So in fact I'll post a video and see if you can do that.
Could you post a video and try it out and see whether you can write the rest of the code to detect the
full edge.
All right.
Welcome back.
OK let's do it together.
I trust you tried your best.
It's very straightforward and I'm sure most of you got it.
So the next day and I'll just put a comment to a cop just take an edge which
like this to fall in edge.
And what we have to do is exactly the same thing first.
We cleared a capture flag like we did above after we've cleared a couple to flag we've pulled the low
interrupt status register to see whether the second bit is high.
Good to see Coach hustle kid right.
It's the same thing then.
If it is high because we have it wild appear the program is going to freeze onto the cultural cars.
If it's high we store it in the other variable which is the low edge.
Now we have the.
We have.
We have detected the high edge and the low edge.
The next thing we have to do is declare a variable to store the difference between the high edge and
the low edge and then declared a variable that would return the results to what I'm going to do next
is declare some constants and some yeah some new variables.
So let's declare a new variable.
I'm going to call this variable D distance.
You can call it whatever you want to call it the distance.
I don't know why that would be easier but could the distance.
This one is going to start a distance in centimeters.
We're going to create a constant and this constant is going to hold our period in the period of our
run and frequency is sixty two point five.
None of seconds and this is how you write.
You're right sixty two point five E minus nine sixty two point five nanoseconds and then we need and
multiply a constant.
So this number here was derived from the speed of sound.
We need this to multiply our timestamp and our run frequency in order to get a distance in centimeters.
Right.
So we have these new variables.
Let's see what we use them for too.
The next thing would be to store the difference between the low edge and high edge in the distance.
Right.
Since the low edge bulkhead latest you can just look up the H minus high edge like this.
And then we have the difference of the two timestamps stand here and to get a distance.
Finally we just multiply the distance by the multiplier and then by 0 60 megahertz one clock.
Then we have the distance and because the function returns in that two would just return the distance.
This is a straightforward yes.
This function is complete.
This function would return the distance of an object right in front of the ultrasonic sensor.
And this is able and this program is able to do this by measuring the time between two events that rise
in age in the fall and age.
This one application of time is was used in designing drivers and signal processing.
So I will pause here and continue.
Later we continue the next lesson that we write the application to test this.
We just write we basically complete the main function in the next lesson.
So see you in the next lesson.