1
00:00:03,180 --> 00:00:04,320
Hello fellas.

2
00:00:04,320 --> 00:00:05,070
Welcome back.

3
00:00:05,670 --> 00:00:14,490
So in this very lesson we agreed to write code for the third most common use of timers which is measuring

4
00:00:14,580 --> 00:00:21,320
the time between events and we're going to use this to program an ultrasonic stencil.

5
00:00:21,390 --> 00:00:27,190
We're going to take the sensor and write it at drivers for it using what we've learned from timers.

6
00:00:27,240 --> 00:00:29,030
And then we're going to test it out.

7
00:00:29,040 --> 00:00:35,880
We're going to output the distance an obstacle in front of the ultrasonic sense or centimeters.

8
00:00:35,940 --> 00:00:37,740
So let's get on with it.

9
00:00:37,770 --> 00:00:39,340
We've seen that theory.

10
00:00:39,540 --> 00:00:45,450
If you did not understand that bit don't worry we are going to write the actual code here.

11
00:00:45,450 --> 00:00:51,180
And once again if there is still something you do not understand please live it in the comments section

12
00:00:51,720 --> 00:00:53,940
or just send me a message.

13
00:00:53,940 --> 00:01:00,630
So I have created a new forward or already calling this particular for the measuring time between event

14
00:01:00,750 --> 00:01:07,630
and I've created a new project inside this folder and I'm calling this project or trust on essential.

15
00:01:08,040 --> 00:01:16,940
I've done all of this just to shorten the the length of the video so I'll wait for you to do that once

16
00:01:16,940 --> 00:01:17,480
you're done.

17
00:01:17,510 --> 00:01:23,820
You come here the social group you rename it to the name of the microcontroller and then you said this

18
00:01:23,820 --> 00:01:30,220
side to the target is Tim 4C One two three and then the source group is the court just one resource

19
00:01:30,230 --> 00:01:34,510
group one folder created which is the application for that.

20
00:01:34,670 --> 00:01:35,560
Then you come here.

21
00:01:35,570 --> 00:01:43,750
You set your target options you change X to 16 and then your debugger you drop down here select still

22
00:01:43,780 --> 00:01:48,400
areas a CDI and don't forget to take this right.

23
00:01:48,410 --> 00:01:58,390
That being said let's create our main dot see far right click at new item that see and then the main

24
00:01:58,500 --> 00:02:00,410
is the name of the file.

25
00:02:00,850 --> 00:02:01,440
So.

26
00:02:01,510 --> 00:02:04,930
So yes let's crack on.

27
00:02:05,140 --> 00:02:12,100
We start by adding the head of file or file microcontroller by right click in and select in this and

28
00:02:12,100 --> 00:02:21,010
then to improve readability or to increase readability we can just start by using symbolic names.

29
00:02:21,010 --> 00:02:25,660
Remember we have four pillars for our ultrasonic module.

30
00:02:25,780 --> 00:02:27,090
It's called The Echo pin.

31
00:02:27,130 --> 00:02:35,830
The trigger pin the FEC and the G and D the echo and trigger pins digital pins that we have to use and

32
00:02:35,860 --> 00:02:47,400
we are going to connect the echo pin to P P 6 and we are going to connect the trigger pin to be for.

33
00:02:47,410 --> 00:02:55,870
So we define it as a symbolic names like this and then we're going to use the blue D just to create

34
00:02:55,870 --> 00:02:59,550
some output to less as defined a plurality as well.

35
00:02:59,560 --> 00:03:01,470
And there we have it.

36
00:03:01,570 --> 00:03:04,300
Long let's open our main function into me

37
00:03:07,450 --> 00:03:09,450
and let's just keep it open.

38
00:03:09,560 --> 00:03:16,450
No we need to create a couple of functions we need to create actually for individual functions you have

39
00:03:16,450 --> 00:03:24,430
to create one function to initialize the timer that we're going to use for detecting the edges in order

40
00:03:24,430 --> 00:03:31,300
to measure the time between the the edges we're going to create another function to create a microsecond

41
00:03:31,310 --> 00:03:36,930
delay for us and then we're going to fact to go in and create another function to initialize outputs

42
00:03:37,030 --> 00:03:43,180
rather than initialize and in the main this time let's just create a new function for Port initialization

43
00:03:43,630 --> 00:03:51,250
and we're going to create the final function to calculate to measure and calculate the distance of the

44
00:03:51,340 --> 00:03:57,950
object in front of the truss on except so let's start with the first function the time must zero in

45
00:03:57,950 --> 00:04:06,760
that we're going to use time as 0 for the arm for the the edge capture we're gonna use two timers like

46
00:04:06,760 --> 00:04:12,910
we said in the previous lesson the first time will be used to detect the urges in order to measure the

47
00:04:12,910 --> 00:04:14,570
time between them.

48
00:04:14,570 --> 00:04:19,870
So we're going to use timers here of this for this and I'm calling this function time as you were in

49
00:04:19,870 --> 00:04:20,760
it.

50
00:04:20,770 --> 00:04:29,650
So we start by by providing them with the access to clock and then that time a series is going to be

51
00:04:29,650 --> 00:04:35,470
used with the Echo pin you want to detect how long it takes the signal to return to the Echo pin we

52
00:04:35,470 --> 00:04:38,590
want to detect when the equipping tens Hi.

53
00:04:38,590 --> 00:04:44,650
So just to recap on what we supposed to do with the code we going to write the code to perform this

54
00:04:44,650 --> 00:04:52,660
for us first before we set it off the ultra sonic module Hi wait for this signal to bounce back to the

55
00:04:52,720 --> 00:04:59,350
echo we started timer when the Echo is detected and then when we stopped the time or when the echo goes

56
00:04:59,360 --> 00:05:03,890
slow so this timer is the time of zero that we write in.

57
00:05:03,890 --> 00:05:11,350
And so as we've defined above here the echo spin is connected to p B6 in order to detect the signal

58
00:05:11,350 --> 00:05:18,390
changes are p B6 we need to use time of zero cause P B6 is connected to time as you are right.

59
00:05:18,430 --> 00:05:25,050
So what we want to do like we saw in the theoretical part of the lesson is to start a timer when PBS

60
00:05:25,050 --> 00:05:33,340
6 goes high and then stop the timer when P B6 goes low cost P B6 is the echo spin if you're forgotten

61
00:05:33,390 --> 00:05:40,410
or are a bit confused just go to the previous listen and see how we initialize the the ultra sonic central

62
00:05:40,420 --> 00:05:43,990
drivers but this the code and leave your questions below.

63
00:05:43,990 --> 00:05:54,220
If you have any show we activate the clock for time on module 0 right and then we would provide clock

64
00:05:54,340 --> 00:06:03,130
access to port B since P B6 is on point B after this we set p B6 we set previous which is the echo pin

65
00:06:03,220 --> 00:06:12,010
us inputs begin by writing this to the direction register and then we digitally enable the echo pin

66
00:06:12,670 --> 00:06:22,600
like this next will be to set the P C T O and the upscale the alternate function select to register

67
00:06:23,170 --> 00:06:30,010
which we have to enable for the Echo pin because we're using the alternate function of P B6 after we

68
00:06:30,100 --> 00:06:39,600
enable the alternate function we have to set the p CTO and we set it this way is a two step approach.

69
00:06:39,790 --> 00:06:47,140
First right this particular hexadecimal code into the register and then write this.

70
00:06:47,140 --> 00:06:54,070
Remember there is a table that tells us what to write and we treated this the previous lesson and it's

71
00:06:54,160 --> 00:06:56,400
also available in the data sheet.

72
00:06:56,410 --> 00:06:58,550
This code overhead.

73
00:06:58,600 --> 00:07:02,930
Now let's go ahead to program the time a module 0 itself.

74
00:07:03,040 --> 00:07:09,040
So as we said before you couldn't figure out a time might you have to first disable it and would do

75
00:07:09,040 --> 00:07:11,920
this by accessing the control register.

76
00:07:11,920 --> 00:07:18,220
And this this is a new way of showing you how to disable using this on this unsigned over here and until

77
00:07:18,220 --> 00:07:29,800
the sets bit sets the first bit disabled is the same as type in equal to zero x zero.

78
00:07:29,970 --> 00:07:30,410
Right.

79
00:07:30,430 --> 00:07:32,980
But this is a friendly way of disabling it.

80
00:07:33,070 --> 00:07:40,840
This one disables just the first bit whereas this disables or bits in the register.

81
00:07:41,410 --> 00:07:49,780
So after that we go to the time a serial configuration register and then set it to 16 bits mode.

82
00:07:49,930 --> 00:07:55,850
We know this already we've been dealing with this in the other examples to set it to 16 bits mode.

83
00:07:55,870 --> 00:07:58,590
You just have to write for into this.

84
00:07:58,600 --> 00:08:01,600
You have to register and we know why that is.

85
00:08:01,600 --> 00:08:07,210
We can verify from the data sheet and this form is the same as X.

86
00:08:07,250 --> 00:08:14,780
So for like this the next thing will be to go to the timer a mode register and with the mode register

87
00:08:14,860 --> 00:08:19,420
we said we are setting characteristics of the timer.

88
00:08:19,540 --> 00:08:26,250
We enable it as an edge time mode capture mode and an up counter.

89
00:08:26,260 --> 00:08:29,910
That's why we have this hexadecimal code 2 x 1 7.

90
00:08:30,250 --> 00:08:38,440
If you expand it this is what it is in binary and these two bits enables us a capture mode and then

91
00:08:39,040 --> 00:08:40,220
this one bit.

92
00:08:40,540 --> 00:08:51,400
This one here it's number two here enables it as an edge time mode and then bit over here sets as an

93
00:08:51,460 --> 00:08:54,740
upper contact this one sets it as an up counter.

94
00:08:54,820 --> 00:08:58,160
In fact we can verify this from the data sheet.

95
00:08:58,240 --> 00:09:04,250
I'm gonna go to the data sheets and just show you how I came up with this code how I derive this.

96
00:09:04,850 --> 00:09:11,220
So we could just open a data sheet to help mine here it's just a portion of it.

97
00:09:12,810 --> 00:09:15,190
So you can just type and such.

98
00:09:15,190 --> 00:09:23,840
Keep team Cabinet papers timer TMR here like we did when compute your credit you'd realize over here.

99
00:09:24,060 --> 00:09:25,980
So this is it.

100
00:09:25,980 --> 00:09:33,630
We use the first two bits and let's see where the first two bit does two in one voice bit two in one

101
00:09:33,630 --> 00:09:40,490
useful bits you're in one it's defined us.

102
00:09:41,340 --> 00:09:43,600
This is it it's known as the T.

103
00:09:43,830 --> 00:09:46,530
And our values are defined this way.

104
00:09:46,590 --> 00:09:51,180
This the value in that description if you set it to 0 x 3.

105
00:09:51,210 --> 00:09:56,610
It's a capture mode and 0 x3 is 1 1 Cimino.

106
00:09:57,060 --> 00:10:01,380
So the first 2 one sets it to the capture mode.

107
00:10:01,380 --> 00:10:04,720
And then let's see the M with us the bits.

108
00:10:04,740 --> 00:10:15,720
Number two the course this is bit 0 1 but to be 2 Let's see what the data sheet says about bits 2 over

109
00:10:15,740 --> 00:10:23,460
here we should have been to number two is the capture mode so we set a too if we said the value one

110
00:10:23,490 --> 00:10:32,250
is each time mode and that is why it's to set one course we wanted to get the edge time not edge count

111
00:10:32,280 --> 00:10:40,590
so Beta 2 set to 1 and is the same today and our register has two sets one.

112
00:10:40,610 --> 00:10:43,450
Now let's see bits to number four what is.

113
00:10:43,490 --> 00:10:45,320
Number four two bits.

114
00:10:45,320 --> 00:10:47,440
Number four is the current direction.

115
00:10:47,440 --> 00:10:54,310
And it says when you set it to one the timer count up when counting up the time I started from fighting

116
00:10:54,360 --> 00:10:56,320
off to 0 hands.

117
00:10:56,420 --> 00:10:58,390
We have these by these.

118
00:10:58,400 --> 00:10:59,900
That's why we have this here.

119
00:11:00,110 --> 00:11:04,220
Each time mode capture mode up counter and this what it means.

120
00:11:04,220 --> 00:11:05,630
I hope you understand.

121
00:11:06,290 --> 00:11:08,080
Yes that's how it is derived.

122
00:11:08,090 --> 00:11:15,800
So the next thing we have to do is to access the time I could register again and set more of the parameters

123
00:11:15,830 --> 00:11:17,370
we require.

124
00:11:17,510 --> 00:11:24,800
And this is it the time control register gives us the ability to set where we want to detect rise in

125
00:11:24,800 --> 00:11:32,030
age only fall in error on the or both edges and then to set it for both ages you have to change the

126
00:11:32,030 --> 00:11:36,950
timer eight events bit to 1 1 and this is fine in this register.

127
00:11:36,980 --> 00:11:43,370
So just to do it for you and show you how it's derived a school to the data sheet and see what happens

128
00:11:43,370 --> 00:11:46,370
when we set this bit to 1 1.

129
00:11:46,370 --> 00:11:51,790
So I'm just going to go to the data sheet here.

130
00:11:52,440 --> 00:11:58,130
I'll just search comeback here like this and now enter.

131
00:11:58,900 --> 00:12:02,740
And this The Register we're looking for and

132
00:12:05,540 --> 00:12:05,860
what.

133
00:12:06,030 --> 00:12:07,660
Which bits are we looking for.

134
00:12:07,780 --> 00:12:09,480
We just checked out again.

135
00:12:10,220 --> 00:12:12,970
OK we're looking for the first four bits.

136
00:12:13,030 --> 00:12:13,510
Right

137
00:12:16,230 --> 00:12:20,600
so the first full bit the first bit is use to enable it rights.

138
00:12:20,660 --> 00:12:24,250
The second bit is used to set a stall in this one.

139
00:12:24,290 --> 00:12:27,940
It's two and three I use for the event.

140
00:12:27,990 --> 00:12:33,670
So let's see it's two and three it's 2 3 2 3.

141
00:12:33,670 --> 00:12:40,240
Here is it and distant description assess says when you set to see you X you you want to detect positive

142
00:12:40,360 --> 00:12:42,050
edge which is res image.

143
00:12:42,170 --> 00:12:47,630
We said to two X one you wanted to take the negative x mean and the fallen edge where you said to zero

144
00:12:47,710 --> 00:12:51,060
x to me you want to detect both edges.

145
00:12:51,100 --> 00:13:00,280
So when you compare this to x 3 where you converted and it's position you end up with a code 0 x 0 c

146
00:13:01,150 --> 00:13:09,350
we convert to x 3 and I put this code cause this 2 x 3 is not at the beginning right.

147
00:13:10,860 --> 00:13:14,790
Because this you X to me is the fourth.

148
00:13:14,800 --> 00:13:20,290
Let's see it's the third and second bits of this register.

149
00:13:20,470 --> 00:13:26,680
So if you're serious you in fact when you do the calculation you see it like it did.

150
00:13:26,680 --> 00:13:31,140
I explained it to her form is what it looks like right.

151
00:13:31,150 --> 00:13:33,020
This is why we have zero XY.

152
00:13:33,040 --> 00:13:38,180
And if you combat this binary code to hexadecimal you will get this.

153
00:13:38,200 --> 00:13:45,450
Finally what we have to do is to enable the timer by access in the quantum register then looks.

154
00:13:46,240 --> 00:13:53,640
So this is all the risk for the time I see your initialization function with initialize it with these

155
00:13:53,670 --> 00:13:54,650
parameters.

156
00:13:54,670 --> 00:14:00,130
The next thing would be to write a delay function to provide us with a microsecond delay.

157
00:14:00,130 --> 00:14:02,710
So that's what we are going to do next.

158
00:14:02,710 --> 00:14:05,430
So the lesson is getting a bit long.

159
00:14:05,440 --> 00:14:08,180
So let's continue the next lesson.

160
00:14:08,230 --> 00:14:08,740
See you.
