1
00:00:03,490 --> 00:00:04,170
All right.

2
00:00:04,270 --> 00:00:11,770
We are going to do it a project to demonstrate how timer interrupt occurs.

3
00:00:12,280 --> 00:00:18,850
So what we're going to do here is create to interrupt one that's one hertz and another one that's 10

4
00:00:18,850 --> 00:00:24,230
hertz 1 Hertz means the interrupt is going to care one second.

5
00:00:24,480 --> 00:00:29,650
And what I mean by all care wants the second means we're going to place a piece of program a piece of

6
00:00:29,650 --> 00:00:36,700
code in the interrupt service routine and that code what our program is going to run once every second

7
00:00:37,360 --> 00:00:43,870
and ten has interrupt it's going to run its interrupt service routine 10 times a second.

8
00:00:43,870 --> 00:00:45,460
Therefore 10 hertz.

9
00:00:45,540 --> 00:00:46,610
So let's see how it works.

10
00:00:46,960 --> 00:00:55,870
So I've created a new food already and I've called it timer interrupt here and I've created a new project

11
00:00:55,960 --> 00:00:58,520
which I've also called a time interrupt.

12
00:00:58,570 --> 00:01:00,700
I've done the normal boiler plate stuff.

13
00:01:00,700 --> 00:01:07,960
I've already set this up and I'll wait for you two to just create a new projects post a video create

14
00:01:07,970 --> 00:01:11,820
a new project select the board when you get here.

15
00:01:11,950 --> 00:01:16,510
When you get here we set a target options together.

16
00:01:16,510 --> 00:01:19,660
You're used to setting this on your own by now.

17
00:01:19,670 --> 00:01:22,600
Then the bunker we select distilleries I CDI.

18
00:01:22,720 --> 00:01:28,320
And over here we can take that reset and run like this and like this.

19
00:01:28,530 --> 00:01:29,140
Right.

20
00:01:29,170 --> 00:01:34,030
So the first and first we are at the head of file for what microcontroller.

21
00:01:34,030 --> 00:01:47,290
This is this and then we would create on main function which is into me and forward comment.

22
00:01:48,380 --> 00:01:53,170
And we create symbolic names we're going to use for three LCD here.

23
00:01:53,180 --> 00:01:59,720
And what we're going to do is we're going to run one LCD color in one interrupt service routine another

24
00:01:59,720 --> 00:02:02,630
one in another interrupt service routine.

25
00:02:02,630 --> 00:02:06,380
And the third one indium in the main function.

26
00:02:06,380 --> 00:02:14,680
So I'm going to define green red blue like this all at once like this.

27
00:02:14,730 --> 00:02:15,740
Right.

28
00:02:15,840 --> 00:02:22,440
So after we've created of symbolic names we stop by creating the functions that would initialize to

29
00:02:22,440 --> 00:02:23,990
time interrupt for us.

30
00:02:24,540 --> 00:02:25,570
Let's start with the end.

31
00:02:25,590 --> 00:02:28,270
The first time I interrupt.

32
00:02:28,280 --> 00:02:36,240
We want to create this is the on the one hertz interrupt and I've chosen time a 1 for this and we're

33
00:02:36,240 --> 00:02:42,860
going to call this function time A 1 A 1 has in it.

34
00:02:43,460 --> 00:02:46,380
Yeah I chose a descriptive name.

35
00:02:47,640 --> 00:02:57,220
And we start like we always do we are we activate the quote gates and register and this is the time

36
00:02:57,220 --> 00:03:04,960
of one then we disable the phone the time on before making any changes to it.

37
00:03:05,950 --> 00:03:14,350
After the We Are we go to the time a configuration register and then choose 16 bits mode.

38
00:03:14,350 --> 00:03:15,880
We're going to use a pre-schooler here.

39
00:03:15,910 --> 00:03:25,840
So we go to the time a mortgage register and sporadic mode and don't come into mode then go to the timer

40
00:03:25,990 --> 00:03:35,260
pre-schooler register and set our pre scalar which is 250 course we want to create one hurts one hurts

41
00:03:35,260 --> 00:03:43,900
implies one second and you notice sixteen bit time it's not large enough to provide one second functionality

42
00:03:44,300 --> 00:03:51,660
to then week after using a preschooler when we calculate we find out that we need to load sixty four

43
00:03:51,660 --> 00:03:57,730
thousand in the time load interval register we aren't we learnt how to calculate this volume in the

44
00:03:57,730 --> 00:04:08,230
very early lessons of this series after that we go to the interrupt control register the ICR and then

45
00:04:08,970 --> 00:04:17,000
we cleared a time out FLAC like this and then this is a new register we're going to add that I am sorry

46
00:04:17,020 --> 00:04:23,710
to interrupt mosque register we've never used it this the first time we're using this register and with

47
00:04:23,710 --> 00:04:28,510
this we enable that time a 1 interrupt with this register here.

48
00:04:28,540 --> 00:04:36,940
So this new thing for this course we need with time a one a timeout interrupt by setting the first bit

49
00:04:37,240 --> 00:04:37,880
too high.

50
00:04:37,890 --> 00:04:48,900
Indeed time one interrupt must register after that we we go to the time control register then enable

51
00:04:48,900 --> 00:04:49,510
the timer

52
00:04:52,450 --> 00:05:00,840
like this because we finish all the configuration of the timer then to enable to interrupt we go to

53
00:05:00,840 --> 00:05:05,090
the ethic and select the interrupt number and enable it from that.

54
00:05:05,140 --> 00:05:14,200
And this how we do it we use this don't worry much about this very last line of code there is a table

55
00:05:14,200 --> 00:05:21,280
there's a tabulation in the data sheet that shows this which interrupts a number corresponds to which

56
00:05:21,280 --> 00:05:22,190
peripheral.

57
00:05:22,360 --> 00:05:23,150
Right.

58
00:05:23,170 --> 00:05:31,800
So because we are creating a new course here is a cookbook for all interrupt time as cystic creepy I

59
00:05:32,130 --> 00:05:36,550
all interrupt on the cortex M we we're going to go into that further.

60
00:05:36,850 --> 00:05:39,580
So don't worry much about this line here.

61
00:05:39,700 --> 00:05:46,900
This line here enables interrupt request 21 which corresponds to time a 1 a.

62
00:05:46,900 --> 00:05:48,530
So that is the meaning of this.

63
00:05:48,570 --> 00:05:51,450
But how this is derived were treated in another course.

64
00:05:51,850 --> 00:05:56,280
And we'll be right in an article on that on cortex and that column as well.

65
00:05:56,680 --> 00:06:02,860
But like I always say if you're in a hurry and you want questions you want quick answers.

66
00:06:02,970 --> 00:06:07,390
Do you leave your questions in the comments section or in the questions section.

67
00:06:07,680 --> 00:06:07,920
Yeah.

68
00:06:09,160 --> 00:06:15,340
So this is all we need to do for the initialization of the timer interrupt function.

69
00:06:15,740 --> 00:06:22,590
So let's go and initialize the young to 10 years time I interrupt as well.

70
00:06:22,600 --> 00:06:25,820
I hope you understand this is very straightforward.

71
00:06:25,870 --> 00:06:28,580
The addition is this and this.

72
00:06:28,660 --> 00:06:30,070
And yeah.

73
00:06:30,280 --> 00:06:34,490
And you already know all these registers with use all of them already.

74
00:06:34,570 --> 00:06:38,530
So we're on to the next one.

75
00:06:38,620 --> 00:06:46,090
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

76
00:06:46,090 --> 00:06:57,310
it the same as the name implies it's a 10 hertz timer and we are going to activate the clock gate and

77
00:06:57,310 --> 00:07:09,490
register of this particular timer and then go into disable the timer to configure it then we're going

78
00:07:09,490 --> 00:07:18,550
to go to the configuration register to set it to its 16 bits mode right after this.

79
00:07:18,580 --> 00:07:22,820
We are which set the timer mode.

80
00:07:22,840 --> 00:07:30,370
Register as well and we want this timer like the first one to be periodic and a down counter and we

81
00:07:30,370 --> 00:07:39,700
do that by using the hexadecimal code to execute it corresponds to the the appropriate speed bits as

82
00:07:39,700 --> 00:07:42,930
defined in the data sheet.

83
00:07:43,090 --> 00:07:51,290
The next one or BD timer pre-schooler register and we said the preschooler to 250.

84
00:07:51,370 --> 00:07:57,640
And given that preschooler fuck you we have the correspondence value for the young time it'll interrupt

85
00:07:57,900 --> 00:08:01,200
the timer interval load register.

86
00:08:01,300 --> 00:08:04,920
It keeps us six thousand four hundred.

87
00:08:05,540 --> 00:08:11,590
As you can see it's different from this here and I'll leave the calculation to you for homework.

88
00:08:11,590 --> 00:08:18,150
I've indicated here but how this is derived is going to be a special homework leave your honor in the

89
00:08:18,160 --> 00:08:23,620
comments section or in the question section and I'll reply what I got right or wrong.

90
00:08:23,620 --> 00:08:29,650
In fact if you get it right send me a message I'll send you a coupon for one of our courses free of

91
00:08:29,650 --> 00:08:30,130
charge.

92
00:08:30,760 --> 00:08:31,990
Yeah.

93
00:08:32,560 --> 00:08:38,560
So as you can see here we loaded sixty four thousand in here we loaded six thousand four hundred.

94
00:08:38,590 --> 00:08:40,780
Yes I know it is very easy.

95
00:08:41,440 --> 00:08:42,720
But yeah.

96
00:08:42,880 --> 00:08:43,280
It.

97
00:08:43,590 --> 00:08:44,370
Yeah.

98
00:08:44,740 --> 00:08:46,990
And we go to the next register.

99
00:08:47,000 --> 00:08:55,090
This time I control the interrupt control register with Claire the time to a time off flag.

100
00:08:55,090 --> 00:08:57,520
And then we go to the new register.

101
00:08:58,150 --> 00:08:59,680
I am sorry to interrupt.

102
00:08:59,690 --> 00:09:01,030
Mosque register.

103
00:09:01,190 --> 00:09:09,930
And what we go into do is we're going to enable time to a mosque into a mosque register and then we

104
00:09:10,060 --> 00:09:15,010
neighborhood at timer time unto itself like this.

105
00:09:15,210 --> 00:09:16,620
Right.

106
00:09:16,830 --> 00:09:26,100
Then finally we enable the interrupt and we're going to access the I S E R register in the end flick

107
00:09:28,880 --> 00:09:29,900
the end Vic.

108
00:09:29,910 --> 00:09:36,360
This one is this one stands for the nested vector interrupt controller and we're going to access this

109
00:09:36,360 --> 00:09:43,870
to enable the timer to a and this number corresponds to our time of two ways interrupt and it's interrupted

110
00:09:43,870 --> 00:09:46,830
a question number is 23.

111
00:09:47,100 --> 00:09:48,330
So that is it.

112
00:09:48,630 --> 00:09:52,030
That's all there is for the initialization of this.

113
00:09:52,590 --> 00:09:59,040
Remember as we said earlier the interrupt ever interrupts has an interrupt service routine.

114
00:09:59,070 --> 00:09:59,590
Right.

115
00:09:59,600 --> 00:10:01,060
Or an interrupt handler.

116
00:10:01,110 --> 00:10:07,630
And when the interrupt occurs that code that exist in the interrupt handler is what it's run by the

117
00:10:07,630 --> 00:10:12,430
CPE you remember the difference between polling and interrupt.

118
00:10:12,480 --> 00:10:15,840
So the interrupt handler is voice invoked when an interrupt.

119
00:10:15,860 --> 00:10:16,350
OK.

120
00:10:16,620 --> 00:10:24,030
So we're right the interrupt handler later before we do that let's just put the prototypes of our functions

121
00:10:24,120 --> 00:10:26,300
up here.

122
00:10:26,310 --> 00:10:33,380
Let's not forget to do the and go to a turn that is function

123
00:10:41,870 --> 00:10:42,950
protests and call on

124
00:10:46,050 --> 00:10:51,220
and let's call our new function it's in the main the main function.

125
00:10:51,390 --> 00:10:59,820
And basically what we want to do in the main function is to also initialize the points that we are using.

126
00:10:59,820 --> 00:11:03,990
Remember we use input F and puts F and the D.

127
00:11:03,990 --> 00:11:10,780
So you can initialize this another way of initialized in right and unused in the bits shift.

128
00:11:10,850 --> 00:11:18,920
I could just for the hexadecimal number here and this number corresponds to on put f then we can.

129
00:11:19,150 --> 00:11:20,580
We can do this as well.

130
00:11:20,580 --> 00:11:28,030
I'm just mixing it up for those with the grant for me with my bare metal embedded c course I should.

131
00:11:28,050 --> 00:11:36,200
Different ways of initializing we could use the bits shift modal we could use to direct hexadecimal

132
00:11:36,200 --> 00:11:36,710
code.

133
00:11:36,840 --> 00:11:46,680
This one also corresponds to all three D so desperate this here and I could just put this here and I

134
00:11:46,680 --> 00:11:51,960
could just use the same I've seen red or green or blue.

135
00:11:51,960 --> 00:11:53,510
We could just type all of that here.

136
00:11:54,620 --> 00:11:59,220
Or we could just leave this hexadecimal code to this just another way of doing it.

137
00:11:59,340 --> 00:12:04,130
And we um we initialize our time I interrupt

138
00:12:08,470 --> 00:12:11,300
and then we initialize the second one as well.

139
00:12:13,210 --> 00:12:13,820
Right.

140
00:12:13,840 --> 00:12:20,530
I think you might need to slow down the video to to try it or you might need to watch first and then

141
00:12:20,530 --> 00:12:21,520
try later.

142
00:12:24,550 --> 00:12:26,110
So now it's looking good.

143
00:12:26,140 --> 00:12:31,370
And finally what we have to do is enable interrupts globally.

144
00:12:31,420 --> 00:12:38,080
This function is function under this function underscore underscore and enable underscore underscore

145
00:12:38,350 --> 00:12:44,360
enable and anyone we want is interrupts this function already exists.

146
00:12:44,400 --> 00:12:45,940
This setup files.

147
00:12:46,290 --> 00:12:51,970
This function enables global interrupts without this function and wouldn't work without calling this.

148
00:12:51,970 --> 00:12:54,340
There there'll be no interrupt in the program.

149
00:12:54,340 --> 00:13:01,760
No we can invoke while 1 Loop infinite loop like this.

150
00:13:02,950 --> 00:13:15,360
And we can guess since GPI f date to 2 Bluett we let the domain domain thread run include LCD.

151
00:13:15,940 --> 00:13:24,160
So one thing I should tell you is you've created your first pseudo real time system here.

152
00:13:24,160 --> 00:13:29,430
What we have is we have a system with three different threats.

153
00:13:29,440 --> 00:13:36,910
This is the main thread and this the initialization of the background threat 1 and then the background

154
00:13:37,030 --> 00:13:43,910
threat to what's going to happen is all these threats are going to exist and run on the same as you.

155
00:13:43,960 --> 00:13:50,650
And what we're going to do next is right the content of the background threat 1 and 2 and the content

156
00:13:50,650 --> 00:13:58,120
of the background threads exist in the interrupt handler also known as the interrupt service routine

157
00:13:59,620 --> 00:14:06,100
so let's write the interrupt service routine of time a one a a remember the interrupt service routine

158
00:14:06,910 --> 00:14:12,980
should be written this way is the standard way of writing it function should it written this way.

159
00:14:13,060 --> 00:14:17,320
It's not very flexible with a function name with interrupt handlers.

160
00:14:17,320 --> 00:14:17,740
Right.

161
00:14:17,950 --> 00:14:29,710
So when every year the she was 10 to 1 8 one heard every second every second the content of this function

162
00:14:29,950 --> 00:14:35,330
will be run by the CPSU and we don't need to use an if statement a while statement or nothing.

163
00:14:35,330 --> 00:14:43,250
And the CPSU does not need to pool for it when the time when time out runs when a time runs out.

164
00:14:43,250 --> 00:14:49,720
In this function it would automatically invoke that content in this function her right.

165
00:14:49,780 --> 00:15:00,720
So that is the interrupt search routine and what we want to do is we want to want to check on one to

166
00:15:00,810 --> 00:15:09,390
access the M S register to interrupt the most interrupt stages register and see if their time out tussle

167
00:15:09,390 --> 00:15:16,840
care might interrupt status register time out one time auto flight we see if the time auto care by running

168
00:15:16,860 --> 00:15:17,920
this operation.

169
00:15:18,390 --> 00:15:26,490
And if the time I'll toss OK then we can turn on the red oldie and this could be anything right.

170
00:15:26,490 --> 00:15:31,140
You could activate the ADC and take a sense of value from here.

171
00:15:31,320 --> 00:15:32,960
You could put different code here.

172
00:15:32,980 --> 00:15:34,440
We are just using entity.

173
00:15:34,620 --> 00:15:38,270
You could drive a DC motor several motor.

174
00:15:38,550 --> 00:15:40,410
You could take analog readings.

175
00:15:40,410 --> 00:15:47,700
You could read a sense or you could send a message or you could just ping another wireless device.

176
00:15:47,700 --> 00:15:49,350
At this rate right.

177
00:15:49,440 --> 00:15:55,490
So this is just what we want to run within this background thread.

178
00:15:56,170 --> 00:16:04,260
So we want to just turn on the red LRT and we've done that we want to clear the interrupt flag so that

179
00:16:04,280 --> 00:16:06,990
it can detect the next one and run it as well.

180
00:16:07,190 --> 00:16:15,100
At the time a one interrupt and one other precaution we can take as we can create a local variable to

181
00:16:15,100 --> 00:16:16,120
force it to clear.

182
00:16:16,120 --> 00:16:19,100
Sometimes it doesn't clear.

183
00:16:21,980 --> 00:16:26,020
Yeah so we use this local variable.

184
00:16:26,290 --> 00:16:29,500
We read read forces clearing up the interim flag.

185
00:16:29,520 --> 00:16:34,000
So if the flag doesn't clear we can just read it and then it would declare.

186
00:16:34,030 --> 00:16:39,510
So we put this here as a precaution and we have another precaution we run in here.

187
00:16:39,510 --> 00:16:41,840
We interrupt right.

188
00:16:41,920 --> 00:16:46,220
We have an LS This is if if the mast interrupts status register.

189
00:16:46,300 --> 00:16:49,250
If there's a time out do this.

190
00:16:49,330 --> 00:16:57,130
What if there's no time out which rarely rarely happen it doesn't happen unless there's some sort of

191
00:16:57,280 --> 00:16:58,430
fault.

192
00:16:58,590 --> 00:17:07,880
But if this doesn't happen that's what you want to do want to clear the flag and not just the arm with

193
00:17:07,900 --> 00:17:08,980
clear or the flags.

194
00:17:09,070 --> 00:17:14,000
If if the mast misinterpreted to stretch that time out that's not OK.

195
00:17:14,120 --> 00:17:15,250
Clear all flags.

196
00:17:15,490 --> 00:17:19,700
And then we read once again to force the clear to okay.

197
00:17:20,140 --> 00:17:26,470
You can meet this side of the court the else statement and it could work quite well quite excellently.

198
00:17:26,470 --> 00:17:35,470
You wouldn't see any difference by said these these into Ellis block and this line here is just precautionary

199
00:17:36,100 --> 00:17:38,820
to make sure the interrupt works in all conditions.

200
00:17:38,830 --> 00:17:44,330
Even if you write lousy code it should still be able to invoke the interrupt.

201
00:17:44,370 --> 00:17:51,870
But basically what you want is to make sure the timeout hustle cared and did run a particular code.

202
00:17:51,870 --> 00:17:53,940
You want to run and include a flag.

203
00:17:54,670 --> 00:17:55,160
Yes.

204
00:17:55,900 --> 00:18:03,630
So this the background threat number one or the interrupt handler the one that runs every one second.

205
00:18:03,830 --> 00:18:05,020
I'll put a comment here

206
00:18:08,030 --> 00:18:10,660
runs once the second

207
00:18:15,470 --> 00:18:16,050
right.

208
00:18:17,000 --> 00:18:22,870
And see the the art the outcome looks beautiful you see nice blinking.

209
00:18:22,970 --> 00:18:24,690
D So now.

210
00:18:25,080 --> 00:18:28,320
The interrupt handler or a background threat.

211
00:18:28,320 --> 00:18:34,560
Number two the one that runs ten times a second and you should know you can increase this.

212
00:18:34,550 --> 00:18:40,590
You can write an interrupt handler that would run 100 times a second or a thousand times a second.

213
00:18:41,160 --> 00:18:42,320
And so on and so forth.

214
00:18:42,330 --> 00:18:43,100
Right.

215
00:18:43,140 --> 00:18:50,860
You can just increase the frequency of the interrupt by increasing the value here by increasing the

216
00:18:50,860 --> 00:18:51,780
volume India.

217
00:18:52,170 --> 00:18:58,290
Yeah and the interrupt load register on so we just.

218
00:18:58,380 --> 00:19:03,900
Yeah it's just the same declaration like we did above.

219
00:19:03,900 --> 00:19:10,550
Only this time we are going to we are going to deal with a different LCD.

220
00:19:10,700 --> 00:19:18,230
We're going to use the green ulti to declare the queen a little easier with toggle and then we clear

221
00:19:18,670 --> 00:19:20,510
and we force claret.

222
00:19:20,680 --> 00:19:30,920
And if the timeout does not occur we still have the precautionary if else statement that can be run

223
00:19:31,370 --> 00:19:36,670
and then I'm gonna keep it down her like this.

224
00:19:37,670 --> 00:19:38,410
Yeah.

225
00:19:38,520 --> 00:19:39,240
Looks good.

226
00:19:40,840 --> 00:19:46,720
Oh looks good so let's see this.

227
00:19:48,030 --> 00:19:50,570
Let's compile the code rebuild.

228
00:19:52,510 --> 00:19:54,160
Let's download onto a boat.

229
00:19:56,120 --> 00:19:58,730
Or not connected my boat.

230
00:19:59,990 --> 00:20:01,210
And yes it's work.

231
00:20:01,220 --> 00:20:08,100
And so that's where we get all three threads running the main thread is running continuously and that's

232
00:20:08,100 --> 00:20:10,930
the one that Tokyo's beyond the blue LCD.

233
00:20:11,100 --> 00:20:21,120
And once a second once every second the the one heads thread runs and that toll goes down the red LCD

234
00:20:21,620 --> 00:20:24,030
and 10 times a second.

235
00:20:24,660 --> 00:20:30,830
The background thread number two which is running at 10 hertz Tokyo was the green entity.

236
00:20:30,840 --> 00:20:34,830
And you can see that this is the color mixture we get.

237
00:20:34,830 --> 00:20:38,800
So as I said you can drive different outputs using time I interrupt.

238
00:20:38,820 --> 00:20:43,420
This is just one example of the uses of time I interrupt and.

239
00:20:43,880 --> 00:20:44,330
Yeah.

240
00:20:44,910 --> 00:20:52,020
So I hope you enjoyed this course and I hope the content helps you as you become a better embedded systems

241
00:20:52,020 --> 00:20:52,890
developer.

242
00:20:53,060 --> 00:20:56,960
Look out for our other series in the cortex and cookbook series.

243
00:20:56,970 --> 00:20:58,080
We have the young.

244
00:20:59,100 --> 00:21:01,080
We have the cookbook on interrupts coming out.

245
00:21:01,110 --> 00:21:03,230
We have one on pedo beam coming out.

246
00:21:03,660 --> 00:21:10,500
And please do comment and send me a message on the topic you want us to create a cookbook on.

247
00:21:11,040 --> 00:21:14,830
But stay updated and keep practicing.

248
00:21:14,840 --> 00:21:15,150
David.
