1
00:00:03,930 --> 00:00:04,400
All right.

2
00:00:04,410 --> 00:00:05,220
Welcome back.

3
00:00:05,820 --> 00:00:09,510
So we're going to try this next example.

4
00:00:09,510 --> 00:00:16,300
We are going to create our own program to count deep carers of an event using timers.

5
00:00:16,370 --> 00:00:25,080
And as we said we are going to detect the rise and edge off of a switch in order to count the occurrence

6
00:00:25,080 --> 00:00:26,300
of this event.

7
00:00:26,310 --> 00:00:33,210
So like always you create a new folder and you create a project inside that photo that I have done that

8
00:00:33,210 --> 00:00:40,800
already I've created a new folder called event counter a new project called event contact inside a project

9
00:00:41,250 --> 00:00:44,120
and I've done all the initialization that we do.

10
00:00:44,130 --> 00:00:47,640
So I'll wait for you for a minute or so and then you catch up

11
00:00:50,850 --> 00:00:55,480
go to the target's options and do this set up here as well.

12
00:00:55,480 --> 00:00:59,980
I've done all of that so I'll dive in straight.

13
00:01:00,340 --> 00:01:05,380
We stopped by for you to know I mean the CFO right click here.

14
00:01:05,380 --> 00:01:15,960
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

15
00:01:15,970 --> 00:01:17,550
microcontroller.

16
00:01:17,600 --> 00:01:23,200
Right here we go and we add one main function

17
00:01:26,560 --> 00:01:26,880
right.

18
00:01:26,890 --> 00:01:29,740
We are set a kid a skeleton is ready.

19
00:01:29,740 --> 00:01:32,790
Now let's add the flesh if you may.

20
00:01:32,860 --> 00:01:40,960
So let's start with it time initialization will create a new function and then we'll call this new function

21
00:01:41,150 --> 00:01:49,300
time of three a rise an edge event in it this is going to be a function that initialize as the time

22
00:01:49,300 --> 00:01:54,090
off for detecting rides and ages.

23
00:01:54,220 --> 00:02:01,600
This war is going to look like and would stop by and enabling the time we use in time at three for this

24
00:02:01,600 --> 00:02:09,090
example cause we are going to use P B2 and P B2 is connected to the general purpose time of three.

25
00:02:09,280 --> 00:02:16,870
So we stopped by by activating the clock gate and register for time three and we can just use the hexadecimal

26
00:02:16,900 --> 00:02:18,700
0 0 8.

27
00:02:18,790 --> 00:02:27,290
As you know as your ex your age is 0 0 1 meaning beyond the bit number 3 when you count from zero bits.

28
00:02:27,300 --> 00:02:34,900
Number three is time of three hence your extra eight thought out expand the air for you for the last

29
00:02:34,900 --> 00:02:35,520
time.

30
00:02:35,790 --> 00:02:36,120
Right.

31
00:02:36,130 --> 00:02:41,100
The binary code here and there's going to be one two three four.

32
00:02:41,530 --> 00:02:43,510
It's going to be one two three.

33
00:02:43,570 --> 00:02:46,510
So this is it.

34
00:02:46,840 --> 00:02:50,230
So this is it's zero for a time a zero.

35
00:02:50,350 --> 00:02:56,190
It's wonderful time a one two four time two but three four times three.

36
00:02:56,440 --> 00:03:01,690
When we convert this to hexadecimal code we get 0 x 0 8.

37
00:03:01,690 --> 00:03:03,020
Excellent.

38
00:03:03,040 --> 00:03:10,780
So the next thing we do is to activate the clock gate and register because GPI you'll be going to use

39
00:03:10,780 --> 00:03:16,120
P B2 as the input pin that we read down that event from.

40
00:03:16,120 --> 00:03:26,520
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

41
00:03:26,520 --> 00:03:35,130
to an input pin and then we digitally enable it digitally enable it.

42
00:03:37,900 --> 00:03:47,830
Then we would have to you have to configure the alternate to function automate select register as well.

43
00:03:47,830 --> 00:03:49,270
For this example.

44
00:03:49,460 --> 00:03:57,780
And thus the upscale and we would enable the alternate function register.

45
00:03:57,970 --> 00:04:03,670
And then we have to set the p control register with its specified code.

46
00:04:03,670 --> 00:04:12,640
We spoke about this in the very last lesson we just did to configure P B to us at times three C C piece

47
00:04:12,640 --> 00:04:13,630
your pain.

48
00:04:13,780 --> 00:04:21,950
We saw the table there we we saw the table with all of them listed and wall code should be useful.

49
00:04:21,950 --> 00:04:23,670
What particular peanut port.

50
00:04:24,010 --> 00:04:27,520
And this the code that corresponds to Peavey too.

51
00:04:27,660 --> 00:04:34,710
And then we continue with the configuration of the piece to this last one.

52
00:04:35,680 --> 00:04:40,090
And why is the GPL configuration as complete.

53
00:04:40,150 --> 00:04:47,290
We continue with the the time configuration and we start by disabling the time off before we stop making

54
00:04:47,350 --> 00:04:49,180
any changes to it.

55
00:04:49,240 --> 00:04:58,570
So we start by access in the time of 3 control register and we set it to series you would disable it.

56
00:05:00,130 --> 00:05:08,780
And after that we go to the time a 3 configuration register and we select periodic code mode.

57
00:05:08,830 --> 00:05:19,360
We select 16 bits mode sorry 0 0 4 corresponds to enabling 16 bits mode in D C G register you can verify

58
00:05:19,360 --> 00:05:21,480
from the data sheet.

59
00:05:21,640 --> 00:05:26,130
After that we go to the timer a mode register with this register.

60
00:05:26,320 --> 00:05:35,170
We decide whether we want a periodic mode one time one shot mode file capture mode or a trigger mode

61
00:05:36,250 --> 00:05:37,170
so we select.

62
00:05:37,180 --> 00:05:45,170
We use this particular hexadecimal code and this code set the timer a more courageous start to an app

63
00:05:45,180 --> 00:05:52,240
counter timer an edge college course we're going to call the rise and edge and set it in cup mode.

64
00:05:52,450 --> 00:06:00,240
If you take a look in the data sheet you realize that when you when you activate these particular bits

65
00:06:00,850 --> 00:06:05,810
these are the parameters you set for what it timer a mode register.

66
00:06:06,040 --> 00:06:14,470
The next thing will be to configure the timer a much register and timer much register.

67
00:06:14,500 --> 00:06:21,500
This this register sets the current limit compared to t r to determine whether much defense hustle kid

68
00:06:21,840 --> 00:06:24,840
the T will be the last register we configure.

69
00:06:24,870 --> 00:06:34,700
But this register in effect tells us how large we can count how many times can we count before.

70
00:06:35,200 --> 00:06:37,200
The limit is four right.

71
00:06:37,210 --> 00:06:44,450
Meaning you cannot count a billion times this register that much register creates down limits and currently

72
00:06:44,520 --> 00:06:52,710
is just it's just sixteen bits in size to expand to the size of how much or how many cards we can make.

73
00:06:52,720 --> 00:06:58,630
We can optically time a priest Kayla register.

74
00:06:58,630 --> 00:07:04,810
We can set up priest Kayla and this priest Kayla offices additional aid.

75
00:07:04,870 --> 00:07:15,690
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

76
00:07:15,700 --> 00:07:16,600
comment.

77
00:07:16,720 --> 00:07:24,020
This particular register issues with a time on much registered to expand it to a twenty four bit as

78
00:07:24,060 --> 00:07:25,440
the priest Kayla.

79
00:07:25,660 --> 00:07:31,540
So I hope you understand again if there is something you do not understand please leave it in the comments

80
00:07:31,540 --> 00:07:32,470
section.

81
00:07:33,670 --> 00:07:40,080
Finally we we enable the day time after we've done all the changes.

82
00:07:40,090 --> 00:07:48,820
We do this by access and the time of going to register and setting it one very straightforward.

83
00:07:48,820 --> 00:07:55,590
Once this is done this creates another function for collecting the event.

84
00:07:55,720 --> 00:08:00,810
We're going to call this function time of 3 a rise in each event to capture.

85
00:08:00,970 --> 00:08:08,050
We're going to use this function to capture the event so it's just as simple as this.

86
00:08:08,050 --> 00:08:15,000
This function is simply going to return what is in the TB register.

87
00:08:15,070 --> 00:08:16,630
Remember we said that T.

88
00:08:16,810 --> 00:08:21,640
Register is compared with a much register too determined to determine an event.

89
00:08:21,700 --> 00:08:22,270
Right.

90
00:08:22,270 --> 00:08:29,800
And as we wrote for the March register is usability compared to the TR to determine what only fantastic

91
00:08:29,820 --> 00:08:30,530
cat.

92
00:08:30,550 --> 00:08:31,350
So this is it.

93
00:08:31,360 --> 00:08:33,870
This is what it looks like.

94
00:08:33,870 --> 00:08:36,310
All the functions look like.

95
00:08:36,420 --> 00:08:40,000
So what we have to do now is come over here.

96
00:08:40,110 --> 00:08:50,220
We just place a prototype of this function up here and then place a prototype of the other function

97
00:08:50,220 --> 00:08:51,150
up there as well.

98
00:08:53,160 --> 00:08:59,280
The next thing will be to create a variable to store the counter so that we can fill in the debugger

99
00:08:59,340 --> 00:09:02,310
and see the changes that occur.

100
00:09:02,310 --> 00:09:09,890
What I'm gonna do is I'm going to create a variable called number of events going to make it global.

101
00:09:09,950 --> 00:09:17,780
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

102
00:09:18,240 --> 00:09:23,680
of the time through right rising edge events capture function in this variable.

103
00:09:23,690 --> 00:09:31,230
This function returns to the number of events that are captured in fact since we've written this year

104
00:09:31,440 --> 00:09:34,690
compared with TIME I'M MUCH TO DETERMINE much events.

105
00:09:34,930 --> 00:09:38,120
And we have compared to t R2 too much events.

106
00:09:38,370 --> 00:09:40,120
I'm just gonna clear this.

107
00:09:40,500 --> 00:09:42,420
This comment is a bit confusing.

108
00:09:42,630 --> 00:09:45,460
Oh it's a bit redundant.

109
00:09:45,570 --> 00:09:48,390
So finally before we test out the code.

110
00:09:48,780 --> 00:09:57,760
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

111
00:09:57,770 --> 00:10:05,920
our wild one which is open a while loop open and then close.

112
00:10:06,720 --> 00:10:12,930
So after we farm after we've written our while loop while one loop.

113
00:10:13,200 --> 00:10:13,490
Yes.

114
00:10:13,500 --> 00:10:24,580
Let's not forget to initialize the timer just copy and paste to her this visualization and say it like

115
00:10:24,590 --> 00:10:25,060
this.

116
00:10:27,690 --> 00:10:31,120
So we going to test this using the debug view.

117
00:10:31,350 --> 00:10:39,510
We're going to do as we enter the debug view and observe the changes that occur with the number of event

118
00:10:39,600 --> 00:10:41,890
as we press that push button.

119
00:10:41,990 --> 00:10:44,400
So you click here to go to the debug few.

120
00:10:44,520 --> 00:10:46,250
So I have a little setup here.

121
00:10:46,260 --> 00:10:47,970
I have a push button.

122
00:10:48,060 --> 00:10:53,840
It's got two legs of connected one day two to three point three forward to pin and the other leg to

123
00:10:54,150 --> 00:11:01,640
B to what's going to happen is when I press that push button the rising edge should be detected by PBT

124
00:11:01,780 --> 00:11:05,180
that the above fuse a very important part of the to chant.

125
00:11:05,190 --> 00:11:11,960
If in a very conversant with the both of you please take a look at my other calls the cortex and bare

126
00:11:11,970 --> 00:11:18,930
metal and better to see programming course with this course you learn the full range of uses of the

127
00:11:18,930 --> 00:11:19,990
debug view.

128
00:11:20,040 --> 00:11:24,430
You'll learn how to read what's going on in the memory in the registers.

129
00:11:24,450 --> 00:11:30,870
You also learn how to read the machine language as well as the disassembly code.

130
00:11:30,870 --> 00:11:39,060
So that being said for this very example we just need to observe what happens to the global variable

131
00:11:39,060 --> 00:11:40,190
we've set.

132
00:11:40,200 --> 00:11:45,690
And it is this global variable which we have named a number of events to do this.

133
00:11:45,690 --> 00:11:52,850
You just highlighted by double click on it right click and then go to a number of events to watch window

134
00:11:52,850 --> 00:11:53,320
1.

135
00:11:53,340 --> 00:12:00,540
If you add it it would appear here in watch when the one and you run your core why you want to do is

136
00:12:00,540 --> 00:12:02,720
come over here and click on to run.

137
00:12:02,730 --> 00:12:10,230
But if I click yes you can see this but is increasing because this switch that push button has been

138
00:12:10,230 --> 00:12:11,790
turned on.

139
00:12:11,870 --> 00:12:16,230
Okay now turn it off and it stopped.

140
00:12:16,230 --> 00:12:21,660
So its content five hundred and twenty one rise and just so far.

141
00:12:21,900 --> 00:12:24,810
Remember the rising edge case course the young.

142
00:12:24,810 --> 00:12:31,850
The switch was stand to click again and there and you can see it's continuing its current in the rise

143
00:12:31,860 --> 00:12:34,600
in each course there is context.

144
00:12:35,270 --> 00:12:36,080
Turn it off.

145
00:12:36,130 --> 00:12:39,780
And so this is how you are.

146
00:12:39,840 --> 00:12:43,890
You can't do your parents off on you fences and timers.

147
00:12:44,730 --> 00:12:46,000
One way of doing this.

148
00:12:46,030 --> 00:12:52,860
You can't use this to detect single presses as well rather than you know letting the columns humorously.

149
00:12:52,890 --> 00:12:59,940
You can write various algorithms or very simple coding fat to detect that it's been pressed once or

150
00:12:59,940 --> 00:13:06,570
not this is often known as the deep bounds and you can write very simple code for that in a way the

151
00:13:06,570 --> 00:13:12,810
switch is not the perfect example for the application of event counting.

152
00:13:12,840 --> 00:13:19,590
I'm using time as the best way to do this which would have been to get a signal generator and then we

153
00:13:19,590 --> 00:13:26,100
generated the signal and plug the signal to two p B2 and then we would upset a number of fries and they

154
00:13:26,100 --> 00:13:32,870
just die has been generated by the signal and compare the number of ways and ages we see to the number

155
00:13:32,870 --> 00:13:38,160
that is detected over here but because not everyone has a signal generator.

156
00:13:38,160 --> 00:13:40,840
We are using this switch to demonstrate this.

157
00:13:41,010 --> 00:13:46,680
And if you do not understand please leave your comments or your questions below.

158
00:13:46,680 --> 00:13:48,740
So to then see you in the next lesson.
