All right.
We are going to do it a project to demonstrate how timer interrupt occurs.
So what we're going to do here is create to interrupt one that's one hertz and another one that's 10
hertz 1 Hertz means the interrupt is going to care one second.
And what I mean by all care wants the second means we're going to place a piece of program a piece of
code in the interrupt service routine and that code what our program is going to run once every second
and ten has interrupt it's going to run its interrupt service routine 10 times a second.
Therefore 10 hertz.
So let's see how it works.
So I've created a new food already and I've called it timer interrupt here and I've created a new project
which I've also called a time interrupt.
I've done the normal boiler plate stuff.
I've already set this up and I'll wait for you two to just create a new projects post a video create
a new project select the board when you get here.
When you get here we set a target options together.
You're used to setting this on your own by now.
Then the bunker we select distilleries I CDI.
And over here we can take that reset and run like this and like this.
Right.
So the first and first we are at the head of file for what microcontroller.
This is this and then we would create on main function which is into me and forward comment.
And we create symbolic names we're going to use for three LCD here.
And what we're going to do is we're going to run one LCD color in one interrupt service routine another
one in another interrupt service routine.
And the third one indium in the main function.
So I'm going to define green red blue like this all at once like this.
Right.
So after we've created of symbolic names we stop by creating the functions that would initialize to
time interrupt for us.
Let's start with the end.
The first time I interrupt.
We want to create this is the on the one hertz interrupt and I've chosen time a 1 for this and we're
going to call this function time A 1 A 1 has in it.
Yeah I chose a descriptive name.
And we start like we always do we are we activate the quote gates and register and this is the time
of one then we disable the phone the time on before making any changes to it.
After the We Are we go to the time a configuration register and then choose 16 bits mode.
We're going to use a pre-schooler here.
So we go to the time a mortgage register and sporadic mode and don't come into mode then go to the timer
pre-schooler register and set our pre scalar which is 250 course we want to create one hurts one hurts
implies one second and you notice sixteen bit time it's not large enough to provide one second functionality
to then week after using a preschooler when we calculate we find out that we need to load sixty four
thousand in the time load interval register we aren't we learnt how to calculate this volume in the
very early lessons of this series after that we go to the interrupt control register the ICR and then
we cleared a time out FLAC like this and then this is a new register we're going to add that I am sorry
to interrupt mosque register we've never used it this the first time we're using this register and with
this we enable that time a 1 interrupt with this register here.
So this new thing for this course we need with time a one a timeout interrupt by setting the first bit
too high.
Indeed time one interrupt must register after that we we go to the time control register then enable
the timer
like this because we finish all the configuration of the timer then to enable to interrupt we go to
the ethic and select the interrupt number and enable it from that.
And this how we do it we use this don't worry much about this very last line of code there is a table
there's a tabulation in the data sheet that shows this which interrupts a number corresponds to which
peripheral.
Right.
So because we are creating a new course here is a cookbook for all interrupt time as cystic creepy I
all interrupt on the cortex M we we're going to go into that further.
So don't worry much about this line here.
This line here enables interrupt request 21 which corresponds to time a 1 a.
So that is the meaning of this.
But how this is derived were treated in another course.
And we'll be right in an article on that on cortex and that column as well.
But like I always say if you're in a hurry and you want questions you want quick answers.
Do you leave your questions in the comments section or in the questions section.
Yeah.
So this is all we need to do for the initialization of the timer interrupt function.
So let's go and initialize the young to 10 years time I interrupt as well.
I hope you understand this is very straightforward.
The addition is this and this.
And yeah.
And you already know all these registers with use all of them already.
So we're on to the next one.
This is going to be a 10 hertz and we're going to use time up to a for this and we're going to call
it the same as the name implies it's a 10 hertz timer and we are going to activate the clock gate and
register of this particular timer and then go into disable the timer to configure it then we're going
to go to the configuration register to set it to its 16 bits mode right after this.
We are which set the timer mode.
Register as well and we want this timer like the first one to be periodic and a down counter and we
do that by using the hexadecimal code to execute it corresponds to the the appropriate speed bits as
defined in the data sheet.
The next one or BD timer pre-schooler register and we said the preschooler to 250.
And given that preschooler fuck you we have the correspondence value for the young time it'll interrupt
the timer interval load register.
It keeps us six thousand four hundred.
As you can see it's different from this here and I'll leave the calculation to you for homework.
I've indicated here but how this is derived is going to be a special homework leave your honor in the
comments section or in the question section and I'll reply what I got right or wrong.
In fact if you get it right send me a message I'll send you a coupon for one of our courses free of
charge.
Yeah.
So as you can see here we loaded sixty four thousand in here we loaded six thousand four hundred.
Yes I know it is very easy.
But yeah.
It.
Yeah.
And we go to the next register.
This time I control the interrupt control register with Claire the time to a time off flag.
And then we go to the new register.
I am sorry to interrupt.
Mosque register.
And what we go into do is we're going to enable time to a mosque into a mosque register and then we
neighborhood at timer time unto itself like this.
Right.
Then finally we enable the interrupt and we're going to access the I S E R register in the end flick
the end Vic.
This one is this one stands for the nested vector interrupt controller and we're going to access this
to enable the timer to a and this number corresponds to our time of two ways interrupt and it's interrupted
a question number is 23.
So that is it.
That's all there is for the initialization of this.
Remember as we said earlier the interrupt ever interrupts has an interrupt service routine.
Right.
Or an interrupt handler.
And when the interrupt occurs that code that exist in the interrupt handler is what it's run by the
CPE you remember the difference between polling and interrupt.
So the interrupt handler is voice invoked when an interrupt.
OK.
So we're right the interrupt handler later before we do that let's just put the prototypes of our functions
up here.
Let's not forget to do the and go to a turn that is function
protests and call on
and let's call our new function it's in the main the main function.
And basically what we want to do in the main function is to also initialize the points that we are using.
Remember we use input F and puts F and the D.
So you can initialize this another way of initialized in right and unused in the bits shift.
I could just for the hexadecimal number here and this number corresponds to on put f then we can.
We can do this as well.
I'm just mixing it up for those with the grant for me with my bare metal embedded c course I should.
Different ways of initializing we could use the bits shift modal we could use to direct hexadecimal
code.
This one also corresponds to all three D so desperate this here and I could just put this here and I
could just use the same I've seen red or green or blue.
We could just type all of that here.
Or we could just leave this hexadecimal code to this just another way of doing it.
And we um we initialize our time I interrupt
and then we initialize the second one as well.
Right.
I think you might need to slow down the video to to try it or you might need to watch first and then
try later.
So now it's looking good.
And finally what we have to do is enable interrupts globally.
This function is function under this function underscore underscore and enable underscore underscore
enable and anyone we want is interrupts this function already exists.
This setup files.
This function enables global interrupts without this function and wouldn't work without calling this.
There there'll be no interrupt in the program.
No we can invoke while 1 Loop infinite loop like this.
And we can guess since GPI f date to 2 Bluett we let the domain domain thread run include LCD.
So one thing I should tell you is you've created your first pseudo real time system here.
What we have is we have a system with three different threats.
This is the main thread and this the initialization of the background threat 1 and then the background
threat to what's going to happen is all these threats are going to exist and run on the same as you.
And what we're going to do next is right the content of the background threat 1 and 2 and the content
of the background threads exist in the interrupt handler also known as the interrupt service routine
so let's write the interrupt service routine of time a one a a remember the interrupt service routine
should be written this way is the standard way of writing it function should it written this way.
It's not very flexible with a function name with interrupt handlers.
Right.
So when every year the she was 10 to 1 8 one heard every second every second the content of this function
will be run by the CPSU and we don't need to use an if statement a while statement or nothing.
And the CPSU does not need to pool for it when the time when time out runs when a time runs out.
In this function it would automatically invoke that content in this function her right.
So that is the interrupt search routine and what we want to do is we want to want to check on one to
access the M S register to interrupt the most interrupt stages register and see if their time out tussle
care might interrupt status register time out one time auto flight we see if the time auto care by running
this operation.
And if the time I'll toss OK then we can turn on the red oldie and this could be anything right.
You could activate the ADC and take a sense of value from here.
You could put different code here.
We are just using entity.
You could drive a DC motor several motor.
You could take analog readings.
You could read a sense or you could send a message or you could just ping another wireless device.
At this rate right.
So this is just what we want to run within this background thread.
So we want to just turn on the red LRT and we've done that we want to clear the interrupt flag so that
it can detect the next one and run it as well.
At the time a one interrupt and one other precaution we can take as we can create a local variable to
force it to clear.
Sometimes it doesn't clear.
Yeah so we use this local variable.
We read read forces clearing up the interim flag.
So if the flag doesn't clear we can just read it and then it would declare.
So we put this here as a precaution and we have another precaution we run in here.
We interrupt right.
We have an LS This is if if the mast interrupts status register.
If there's a time out do this.
What if there's no time out which rarely rarely happen it doesn't happen unless there's some sort of
fault.
But if this doesn't happen that's what you want to do want to clear the flag and not just the arm with
clear or the flags.
If if the mast misinterpreted to stretch that time out that's not OK.
Clear all flags.
And then we read once again to force the clear to okay.
You can meet this side of the court the else statement and it could work quite well quite excellently.
You wouldn't see any difference by said these these into Ellis block and this line here is just precautionary
to make sure the interrupt works in all conditions.
Even if you write lousy code it should still be able to invoke the interrupt.
But basically what you want is to make sure the timeout hustle cared and did run a particular code.
You want to run and include a flag.
Yes.
So this the background threat number one or the interrupt handler the one that runs every one second.
I'll put a comment here
runs once the second
right.
And see the the art the outcome looks beautiful you see nice blinking.
D So now.
The interrupt handler or a background threat.
Number two the one that runs ten times a second and you should know you can increase this.
You can write an interrupt handler that would run 100 times a second or a thousand times a second.
And so on and so forth.
Right.
You can just increase the frequency of the interrupt by increasing the value here by increasing the
volume India.
Yeah and the interrupt load register on so we just.
Yeah it's just the same declaration like we did above.
Only this time we are going to we are going to deal with a different LCD.
We're going to use the green ulti to declare the queen a little easier with toggle and then we clear
and we force claret.
And if the timeout does not occur we still have the precautionary if else statement that can be run
and then I'm gonna keep it down her like this.
Yeah.
Looks good.
Oh looks good so let's see this.
Let's compile the code rebuild.
Let's download onto a boat.
Or not connected my boat.
And yes it's work.
And so that's where we get all three threads running the main thread is running continuously and that's
the one that Tokyo's beyond the blue LCD.
And once a second once every second the the one heads thread runs and that toll goes down the red LCD
and 10 times a second.
The background thread number two which is running at 10 hertz Tokyo was the green entity.
And you can see that this is the color mixture we get.
So as I said you can drive different outputs using time I interrupt.
This is just one example of the uses of time I interrupt and.
Yeah.
So I hope you enjoyed this course and I hope the content helps you as you become a better embedded systems
developer.
Look out for our other series in the cortex and cookbook series.
We have the young.
We have the cookbook on interrupts coming out.
We have one on pedo beam coming out.
And please do comment and send me a message on the topic you want us to create a cookbook on.
But stay updated and keep practicing.
David.