All right.
Welcome back.
So we're going to try this next example.
We are going to create our own program to count deep carers of an event using timers.
And as we said we are going to detect the rise and edge off of a switch in order to count the occurrence
of this event.
So like always you create a new folder and you create a project inside that photo that I have done that
already I've created a new folder called event counter a new project called event contact inside a project
and I've done all the initialization that we do.
So I'll wait for you for a minute or so and then you catch up
go to the target's options and do this set up here as well.
I've done all of that so I'll dive in straight.
We stopped by for you to know I mean the CFO right click here.
An odd new item to group and its main see and we have the here and we are at the head of file for all
microcontroller.
Right here we go and we add one main function
right.
We are set a kid a skeleton is ready.
Now let's add the flesh if you may.
So let's start with it time initialization will create a new function and then we'll call this new function
time of three a rise an edge event in it this is going to be a function that initialize as the time
off for detecting rides and ages.
This war is going to look like and would stop by and enabling the time we use in time at three for this
example cause we are going to use P B2 and P B2 is connected to the general purpose time of three.
So we stopped by by activating the clock gate and register for time three and we can just use the hexadecimal
0 0 8.
As you know as your ex your age is 0 0 1 meaning beyond the bit number 3 when you count from zero bits.
Number three is time of three hence your extra eight thought out expand the air for you for the last
time.
Right.
The binary code here and there's going to be one two three four.
It's going to be one two three.
So this is it.
So this is it's zero for a time a zero.
It's wonderful time a one two four time two but three four times three.
When we convert this to hexadecimal code we get 0 x 0 8.
Excellent.
So the next thing we do is to activate the clock gate and register because GPI you'll be going to use
P B2 as the input pin that we read down that event from.
So we have to activate port B as well to would do that and then we set GPI or b been to set the pin
to an input pin and then we digitally enable it digitally enable it.
Then we would have to you have to configure the alternate to function automate select register as well.
For this example.
And thus the upscale and we would enable the alternate function register.
And then we have to set the p control register with its specified code.
We spoke about this in the very last lesson we just did to configure P B to us at times three C C piece
your pain.
We saw the table there we we saw the table with all of them listed and wall code should be useful.
What particular peanut port.
And this the code that corresponds to Peavey too.
And then we continue with the configuration of the piece to this last one.
And why is the GPL configuration as complete.
We continue with the the time configuration and we start by disabling the time off before we stop making
any changes to it.
So we start by access in the time of 3 control register and we set it to series you would disable it.
And after that we go to the time a 3 configuration register and we select periodic code mode.
We select 16 bits mode sorry 0 0 4 corresponds to enabling 16 bits mode in D C G register you can verify
from the data sheet.
After that we go to the timer a mode register with this register.
We decide whether we want a periodic mode one time one shot mode file capture mode or a trigger mode
so we select.
We use this particular hexadecimal code and this code set the timer a more courageous start to an app
counter timer an edge college course we're going to call the rise and edge and set it in cup mode.
If you take a look in the data sheet you realize that when you when you activate these particular bits
these are the parameters you set for what it timer a mode register.
The next thing will be to configure the timer a much register and timer much register.
This this register sets the current limit compared to t r to determine whether much defense hustle kid
the T will be the last register we configure.
But this register in effect tells us how large we can count how many times can we count before.
The limit is four right.
Meaning you cannot count a billion times this register that much register creates down limits and currently
is just it's just sixteen bits in size to expand to the size of how much or how many cards we can make.
We can optically time a priest Kayla register.
We can set up priest Kayla and this priest Kayla offices additional aid.
So the 8 bit with this sixteen bits keeps us 24 bits so we can count up to 24 bit size so us hand the
comment.
This particular register issues with a time on much registered to expand it to a twenty four bit as
the priest Kayla.
So I hope you understand again if there is something you do not understand please leave it in the comments
section.
Finally we we enable the day time after we've done all the changes.
We do this by access and the time of going to register and setting it one very straightforward.
Once this is done this creates another function for collecting the event.
We're going to call this function time of 3 a rise in each event to capture.
We're going to use this function to capture the event so it's just as simple as this.
This function is simply going to return what is in the TB register.
Remember we said that T.
Register is compared with a much register too determined to determine an event.
Right.
And as we wrote for the March register is usability compared to the TR to determine what only fantastic
cat.
So this is it.
This is what it looks like.
All the functions look like.
So what we have to do now is come over here.
We just place a prototype of this function up here and then place a prototype of the other function
up there as well.
The next thing will be to create a variable to store the counter so that we can fill in the debugger
and see the changes that occur.
What I'm gonna do is I'm going to create a variable called number of events going to make it global.
I'll keep it here and what I'm going to do finally is to store the return value of all of the time up
of the time through right rising edge events capture function in this variable.
This function returns to the number of events that are captured in fact since we've written this year
compared with TIME I'M MUCH TO DETERMINE much events.
And we have compared to t R2 too much events.
I'm just gonna clear this.
This comment is a bit confusing.
Oh it's a bit redundant.
So finally before we test out the code.
Yeah let's put in the wild as you can see it's not in the wild it's just in the main show we just opened
our wild one which is open a while loop open and then close.
So after we farm after we've written our while loop while one loop.
Yes.
Let's not forget to initialize the timer just copy and paste to her this visualization and say it like
this.
So we going to test this using the debug view.
We're going to do as we enter the debug view and observe the changes that occur with the number of event
as we press that push button.
So you click here to go to the debug few.
So I have a little setup here.
I have a push button.
It's got two legs of connected one day two to three point three forward to pin and the other leg to
B to what's going to happen is when I press that push button the rising edge should be detected by PBT
that the above fuse a very important part of the to chant.
If in a very conversant with the both of you please take a look at my other calls the cortex and bare
metal and better to see programming course with this course you learn the full range of uses of the
debug view.
You'll learn how to read what's going on in the memory in the registers.
You also learn how to read the machine language as well as the disassembly code.
So that being said for this very example we just need to observe what happens to the global variable
we've set.
And it is this global variable which we have named a number of events to do this.
You just highlighted by double click on it right click and then go to a number of events to watch window
1.
If you add it it would appear here in watch when the one and you run your core why you want to do is
come over here and click on to run.
But if I click yes you can see this but is increasing because this switch that push button has been
turned on.
Okay now turn it off and it stopped.
So its content five hundred and twenty one rise and just so far.
Remember the rising edge case course the young.
The switch was stand to click again and there and you can see it's continuing its current in the rise
in each course there is context.
Turn it off.
And so this is how you are.
You can't do your parents off on you fences and timers.
One way of doing this.
You can't use this to detect single presses as well rather than you know letting the columns humorously.
You can write various algorithms or very simple coding fat to detect that it's been pressed once or
not this is often known as the deep bounds and you can write very simple code for that in a way the
switch is not the perfect example for the application of event counting.
I'm using time as the best way to do this which would have been to get a signal generator and then we
generated the signal and plug the signal to two p B2 and then we would upset a number of fries and they
just die has been generated by the signal and compare the number of ways and ages we see to the number
that is detected over here but because not everyone has a signal generator.
We are using this switch to demonstrate this.
And if you do not understand please leave your comments or your questions below.
So to then see you in the next lesson.