Hello.
Welcome back.
So in this lesson like we said we are going to implement a war stick delay subroutines so I'm gonna
create a function here called cystic weight.
I'm given the word function and subroutine here interchangeably.
Yeah it's called the cystic weight and first of all we um we go to the reload value.
Um so what is going to happen is we going to subtract one from the content of the reload register.
So we push we put our rate node value into our cystic reload register this a reload value we put into
the reload register and we would keep subtracting one from this value until it reaches zero.
That is where what time out would okay.
And a unit here is Andy the clock cycles.
And what I mean by this is um.
Because we.
Yeah.
Okay.
What I mean by clock psychosis my microcontroller its default clock frequency is 16 megahertz.
And what this means is 16 million cycles.
Okay.
In one second.
So it's execute 16 million cycles in one second.
Does the meaning of 16 mega hit it simple to remember 80 megahertz means 80 million cycles executed
in a single seconds it's like 80 million hits mega means million.
So 80 mega hit equals 80 million hits.
And this means 80 million cycles in a second.
So my default frequency is 16 megahertz.
In other words 16 million Hertz meaning 16 million psychos executed in a single second.
So if we know 16 million cycles are executed in a single second.
Then how long does it take to execute a single cycle.
And this value is sixty two point five nanoseconds.
So the unit here is sixty two point five nanoseconds which is equal to one cycle for this hour cystic
wait subroutine that we right over here.
So we're going to pass this argument.
How how many cycles we want to delay for we're going to pass this argument to register our issue.
So if I pass five I want to delay for five cycles which essentially equals five multiplied by sixty
two point five nanoseconds.
But yeah we would later only see how to have our delays in you know seconds in units such as seconds
or milliseconds.
So the argument is gonna pass into rage the RS zero.
So by the time we get to the subroutine we are assuming we already have our zero which are what DeLay
amount.
What I'm gonna do now is get into the value that is in the system load or the system reload register
so to invoke s t reload underscore Ah over here
and then I'm simply going to subtract one or two sub one from our zero hour zero.
So what DeLay amount and then I'm going to start a new value after I perform the subtraction what is
left I'm going to store it.
So after I perform this subtraction I'm going to start a resort in the reload register by using the
store instruction and then our zero hour one over here.
Okay.
So once that is done I'm gonna do load R 1
because and fake cystic control register underscore Ah right.
And what I'm gonna do next is load.
I'll use a different register.
Our three are one over here and then I'm going to check whether our count flag is set or 2 and s and
then are 3 0 3 0 3 or 3 and then we created a symbolic name for the count.
This one here to perform on an operation of the content of our three course um off the content of our
three.
There's a mistake here of the contents of our three with the count flag.
Remember our three would have what is located in the register.
This is the control register we've put in our three and we're doing what is in our three and it on end
operation and then we start a resort back into R3 I'm using three options here is the same as using
two appearance it's fine.
It should work the same way.
Right.
Once that is done we want to check if the count flag is set.
If it is not set we'll branch to lessee or create a loop RC LP one here.
We keep checking if they can't flag is no set.
Come over here.
Right.
And the reason we are using b q is when we perform the N operation we append it s over here.
Whenever you see me or whenever you see on instructions such as move s or sub S O at s in fact whenever
you see an instruction which s it means you want to update the AP s r registers when you perform that
particular instruction and the EPA s registered s um the you want to update the AP flags.
Yes some flags in the AP s I registered I get updated based on the results of a particular um execution.
So we have the Zero flag.
We have the overflow flag we have the carry flag we up the negative flag.
This shows you um that the last operation that you performed returned a negative value that last we
appreciate that you performed a return to zero.
So that is why we appended it here so it just checks whether after our subtraction the um the answer
is zero.
If it is not we come back to the top over here.
Um this checks.
Um what uh.
After our N operation I should say course with the form and an and over here.
So we keep returning to the top here until the flag is set right.
The current flag is set.
Okay.
So once that is done we can return from the subroutine bookseller like this.
Okay.
So this is our a cystic wait subroutine.
No we gonna create another the subroutine to um to um to allow us to be able to delay in this a milliseconds.
We going to create 10 milliseconds delay such that we gonna make such that when you call the subroutine
it's gonna delay for 10 millisecond if you pass on documents of one and if you pass on arguments of
hundred it's going to delay hundred multiply by ten EMIs which is equal to one second something of that
nature.
So I'm gonna come over here to create a new subroutine call this I call this mistake wait 10 EMIs and
then I'm gonna take the argument.
So we're going to pass our dollar amount into register.
Iris 0 so I'm gonna move it from our 4 0.
I'm gonna move it into our four or do move s..
And as you can see I've added as to this operation because I need the EPA as our flux to update on this
move.
What is in our zero into our four.
And I'm going to say BQ if if after this operation if if the um if the argument is zero then branch
to a place that I'm going to name.
Done.
Meaning if you pass zero for the dollar amount.
There is no need to execute anything the subroutine should simply return.
So I'm gonna put a label here underscore underscore.
Done.
And this simply going to return from the subroutine be set low so low over here right.
Okay.
So if this is not a case then I'm going to um I'm gonna perform the actual delay
so I'm gonna create uh constants somewhere.
Um I'll put it up here or create delay.
Let's see 10.
Call this 10 image delay.
How about this.
And then I'm gonna assign the number this is based on my clock frequency.
You call you.
I'm gonna give 160 thousand
I'm gonna give 160 thousand.
Right.
So that's the explanation for the one sixty thousand we have here.
Right.
Like I mentioned earlier 16 million equals one hurts my microcontroller is running at sixteen megahertz
meaning sixteen million cycles in a second and in another way of expressing this as 16 million equals
one hertz and we know a thousand milliseconds equals one hertz as well.
Then we want to create 10 seconds we want to find.
How many how many cycles do we get with 10 milliseconds then.
Cause we say we create in 10 milliseconds.
Well for 10 we do want 10 divided by 1000 10 milliseconds divided by 1000 milliseconds times 1 hertz
which equals one over 100 to see how many psychos we get for one over 100 heads.
We simply more to play one over one hundred times.
I was 16 million or sixty million cycles which is equal to one hertz and then we get one sixty thousand
to this um this perhaps I don't know if any of you could explain this in a simpler way but yeah I'm
sure there are simple explanations out there and then you can share it in the comment area if you can
explain this to your fellow students in a simpler way.
So basically um 16 million cycles of course one hertz and this equals one hundred m is it.
Uh this was a thousand M S so then the question is we want a hundred m s equivalent in psychos.
And one could say you are you can't so two zeros here you can't so two zeros here you are left with
160 thousand cycles has the this constant is 160 thousand right.
You know what.
Continuing.
Right.
So we've created a constant.
Let's continue to create a 10 m s delay.
Um so we said f 0 is past this argument the D subroutine is going to return if that's not the case then
we're going to subtract we're gonna subtract something from a while constant or delay amounts when a
load or what delay amounts here.
I'm going to load.
I'm going to use RC reload r 0 with our delay amount which is this our 10 m s and then I'm gonna call
our cystic wait
be a cystic reach over here.
Right.
And then once cystic weight has done it s thin once you know it's returned I'm going to subtract one
from our daily amount by doing the sub as sub subs as UBS and then remember our phone still has a copy
of the argument.
What the uh the argument we passed cause the argument is are it's a no show but we copied it into our
four.
So we still have the original argument.
Yeah we reused.
We used our sue later on for our 10 m s constant so our 4 has the argument we passed initially.
No.
So we say our four our four minus one.
And then we are going to subtract one and if we subtract and it s not equal to zero.
If the result is not zero we are going to branch.
If it's higher we're going to branch.
If the resorts in our fall is higher than zero I'm gonna use P H I instruction here and then I'm gonna
branch to the spot LP 2 and I'm going to put LP 2 here.
Right.
So this is it.
I think yeah it looks good.
No.
Okay.
So we can a line and and there s the loss of 15
right.
Okay.
So now let's test our program.
I'm gonna create two new subroutines here for LGB on an LCD of when I have a lady own.
And this is simply going to turn on the red LCD.
I'm gonna do load R one and then GPL you f data register
and then move into our 0
the lady read and then store our as you are 1 and then be SLR return from subroutine.
Now we're gonna have a lady of
and then this is implemented the same way.
Load our 1 GPL you f data register move into our 0 this time read off
and then store our 0 are 1 and then be slr.
Okay.
So now in our main loop over here
um we can um we can test out our code.
I'm gonna come over here and load our 0
A C or load our show across our storage the register.
Doc likes the arguments of our subroutine.
I'm going to load it with the number.
But this one hundred and then I'm gonna do a two hour wait 10 m s
and then I'm gonna call the lady on bill on a score a lady on and then I'm gonna load our issue again
with one hundred so one hundred multiply by ten m s of course a thousand m s of course one second and
I'm going to be able
to wait to m s and then be L and a score early d off right and I'm going to keep this in a loop and
I'll put a loop here.
Right.
We're looking good.
Okay so we can try out all code see what we have.
Gonna click here to build
this picture that Roy says but register symbol
probably have a partridge somewhere.
Okay this first type will we get to build
look at that.
I was supposed to write a line that I ended up typing and
Turbo the Okay we've got two more.
So.
Whoa.
Yeah.
They shall use using the load.
You don't use this.
This is how to load.
We could have done move.
Okay so there's a typo with regards to this.
And Vic is the current register are over here.
Okay let's set our target options debugger I'm using these the libraries I CDI come over here reset
and run.
Okay.
And then.
Okay.
So I'm gonna quicker to download onto my board to download in my early days.
Not Blinken.
Let's see.
Right.
The reason we have in this issue is we need to push.
Register our 4 and L 4 We need to save them.
Um yeah.
I'm gonna push
our file and a link register
and then before I return from the subroutine I'm gonna pop them pop
R for and then the link register.
Okay let's build and see if this solves so what is you gonna click here to build.
I'm going to click here to download onto the board.
S can see my LCD is blinking.
So this brings us to the end of this lesson.
If you have any questions at all just let me know and I'll see you later.
Have a nice day.