1
00:00:05,620 --> 00:00:06,510
Welcome back.

2
00:00:06,520 --> 00:00:13,240
Let's continue where we left off in the previous lesson we created the initialization of function for

3
00:00:13,240 --> 00:00:19,290
our time like zero which is in charge of detecting the echo and then measuring the time.

4
00:00:19,300 --> 00:00:25,480
In this lesson we're going to stop by create an not delay microsecond and then go on to write the rest

5
00:00:25,480 --> 00:00:26,970
of the driver.

6
00:00:27,070 --> 00:00:33,450
So let's stop by writing the function it takes one argument.

7
00:00:34,010 --> 00:00:42,730
It takes a you end argument and this is the time delay we want to program and then we start by unlocking

8
00:00:42,730 --> 00:00:51,030
it with a clock source by activating the clock gate and mechanism for timer what your 0 uptime on what

9
00:00:51,040 --> 00:00:58,840
you're 1 Sorry we use in time at 1 year we use time of zero up there and then we stop by disabling the

10
00:00:58,840 --> 00:01:04,630
time up before making any changes by access in that time a control register.

11
00:01:04,870 --> 00:01:13,500
After that we go to the timeout configuration register and set it to the sixteen bits mode right.

12
00:01:13,690 --> 00:01:21,320
The next thing will be to go to that timer a mode register and choose it's us a periodic timer.

13
00:01:21,600 --> 00:01:27,980
We'll do this by writing two X two or two into this register.

14
00:01:28,120 --> 00:01:34,750
The next step will be to load the interval file into the Interpol load register and we've already said

15
00:01:34,840 --> 00:01:37,810
in order to get it microsecond delete.

16
00:01:38,020 --> 00:01:46,570
We have to load 16 into the interval a load register and minus one cause it begins content from zero.

17
00:01:47,000 --> 00:01:52,420
And also because 16 because we're running at 16 megahertz.

18
00:01:52,420 --> 00:02:00,020
If we were to run a 24 make hour load twenty four hundred megahertz we load a hundred.

19
00:02:00,070 --> 00:02:07,160
The next thing will be to access the ICR register and set it high.

20
00:02:07,510 --> 00:02:14,020
And then the next one would be to activate the time by going back to the time control register like

21
00:02:14,020 --> 00:02:15,690
this said to me.

22
00:02:17,610 --> 00:02:22,140
Finally we pull and wait for the time out to occur.

23
00:02:22,210 --> 00:02:32,500
We used to loop to pull to create our delay like we did before and we forgot to to up this local variable

24
00:02:32,500 --> 00:02:33,640
declaration.

25
00:02:33,640 --> 00:02:42,560
We can just declare the int I up here like this right and our delete functions set and then next thing

26
00:02:42,560 --> 00:02:49,840
as we initialize the port and write the final function which will be the function that actually measures

27
00:02:50,230 --> 00:02:54,060
the distance of the obstacle in front of the ultrasonic sensor.

28
00:02:55,360 --> 00:03:00,250
So this next function we're going to call it a port in it and this function is just going to perform

29
00:03:00,250 --> 00:03:04,330
basic initialization of the pins and ports.

30
00:03:04,330 --> 00:03:12,610
We are using for this particular project so we stopped by initialize in the cheapest I O port a course

31
00:03:12,670 --> 00:03:23,170
we've connected a trig up into Port a then we can also initialize to keep an eye O port F test to initialize

32
00:03:23,170 --> 00:03:30,910
the blue only to make the project more fun provide some outputs on the c we don't need to initialize

33
00:03:30,920 --> 00:03:38,070
points bu cos we've initialized it over here it's working with a timer set trigger pin US output it's

34
00:03:38,140 --> 00:03:45,100
like this because we've already defined trigger remember defined it so the code knows what trigger means

35
00:03:50,490 --> 00:03:55,240
and then we've set the direction of the blue which is also an output thing

36
00:04:00,660 --> 00:04:03,180
then we can digitally enabled the

37
00:04:06,870 --> 00:04:09,220
condition enabled the pin.

38
00:04:10,220 --> 00:04:15,950
We don't need this here this echo echo is connected to p p p.

39
00:04:16,460 --> 00:04:18,620
So that's a mistake over there.

40
00:04:19,040 --> 00:04:31,520
We need to do is enable this then enable that to wrap up this function we need to enable that then for

41
00:04:31,520 --> 00:04:33,780
the blue early D like this.

42
00:04:34,190 --> 00:04:34,920
Right.

43
00:04:35,490 --> 00:04:42,260
So the third function is complete the next function is going to be the function that does the calculation

44
00:04:42,260 --> 00:04:49,910
and outputs that distance of the object on the object close to the ultrasonic essential.

45
00:04:50,510 --> 00:04:58,330
And this function is going to return a new end 32 and it's going to take no argument.

46
00:04:58,730 --> 00:04:59,770
This what it looks like.

47
00:05:01,040 --> 00:05:09,520
I'm going to call this function measure D measured distance and stuff like this.

48
00:05:09,530 --> 00:05:10,210
Right.

49
00:05:11,180 --> 00:05:14,690
So the first thing to do is to disable the trigger pin.

50
00:05:14,690 --> 00:05:22,780
Remember when we spoke about the steps to detect the distance from the ultrasonic sense or we said you

51
00:05:22,810 --> 00:05:27,860
first disable the trigger pin just to make sure it is disabled.

52
00:05:27,860 --> 00:05:32,570
And then after that we give at least a 10 millisecond delay.

53
00:05:32,570 --> 00:05:39,200
So let's do the first to disable the trigger pin like this this one here disables is just the trigger

54
00:05:39,200 --> 00:05:48,200
pin and then we use our new delay function that delay microsecond and it says at least 10 milliseconds

55
00:05:48,240 --> 00:05:59,840
so we can give twelve milliseconds to find and then after that we enable the trigger pin right and then

56
00:06:00,710 --> 00:06:09,560
we delay at least 10 milliseconds again 10 microseconds sorry delay at least 10 microseconds again which

57
00:06:09,560 --> 00:06:10,570
is 12.

58
00:06:10,670 --> 00:06:13,490
Then finally we disable a trigger pin

59
00:06:16,210 --> 00:06:16,740
right.

60
00:06:18,130 --> 00:06:27,430
So this how we we send out the ultrasonic burst remember it sends aid 40 hertz or sonic bursts to this

61
00:06:27,430 --> 00:06:32,260
how we send it refers first to say would trigger pin.

62
00:06:32,290 --> 00:06:39,280
We wait for about 10 microseconds 10 microseconds and then we enable the trigger pin and then we wait

63
00:06:39,280 --> 00:06:42,910
for another 10 microseconds and disable it.

64
00:06:43,180 --> 00:06:52,120
Then the next thing we have to do is capture the return of the signal on the equipment.

65
00:06:52,120 --> 00:06:59,320
We have to clear a time a capture flag by right and a one to the ICR register.

66
00:06:59,320 --> 00:07:06,700
If you check the data sheet it says when you write a one two bits two of the ICR register you cleared

67
00:07:06,700 --> 00:07:12,430
a time of capture flag and this base is known as the CCI entry bit.

68
00:07:12,610 --> 00:07:19,290
It's an data sheet and we have to poll just like we did for the delay function.

69
00:07:19,390 --> 00:07:20,810
We have to pull over.

70
00:07:20,890 --> 00:07:30,760
We have to pull the raw interrupt the raw interrupt status register the right s and see if the second

71
00:07:30,760 --> 00:07:33,760
bit is high and I've put a comment here.

72
00:07:33,760 --> 00:07:42,490
I said Wait till captured by checking the C E R S which is the bit to check in.

73
00:07:42,490 --> 00:07:44,050
That too is high.

74
00:07:44,050 --> 00:07:50,040
That shows that an interrupt hustle can mean and capture has occurred.

75
00:07:50,170 --> 00:07:56,620
And when that capture occurs or you have to do is stored it timestamp in a variable so we can store

76
00:07:56,620 --> 00:08:00,270
it in this variable here known as high edge.

77
00:08:00,280 --> 00:08:02,410
And we've not declared its variable.

78
00:08:02,410 --> 00:08:05,590
That's why we we get getting this red marks.

79
00:08:05,610 --> 00:08:07,780
But let's declare the variable.

80
00:08:07,780 --> 00:08:15,920
Now we are going to have two of such variables one is high end and the other is low high edge sorry.

81
00:08:16,080 --> 00:08:17,340
And the other is little H.

82
00:08:18,880 --> 00:08:19,460
So yeah.

83
00:08:19,630 --> 00:08:29,110
So we pull to see whether the B2 is high which indicates that there's been a capture and then we store

84
00:08:29,110 --> 00:08:37,930
it in the T R and A T R is basically this statement basically means that time at which the last edge

85
00:08:38,110 --> 00:08:41,890
event took place should be stored in high edge.

86
00:08:42,100 --> 00:08:43,480
Right.

87
00:08:43,660 --> 00:08:50,860
Because that timestamp is stored in our in a t r register because we've with the course we've configured

88
00:08:50,860 --> 00:08:53,800
the timer to edge time mode.

89
00:08:53,800 --> 00:08:57,670
It stores the timestamp when we did the edge count mode.

90
00:08:57,700 --> 00:09:06,830
It stores the number of H counted in the T R because we can't forget earlier on to each time it stores

91
00:09:06,860 --> 00:09:08,100
the timestamp.

92
00:09:08,100 --> 00:09:12,890
And now we have timestamp stored over here in this variable.

93
00:09:13,100 --> 00:09:14,140
So that's what we.

94
00:09:14,180 --> 00:09:17,430
We want to do is get the next edge.

95
00:09:17,570 --> 00:09:22,670
Remember when a rise in your cast the next one is at fault an age when a fallen angel cast the next

96
00:09:22,670 --> 00:09:24,450
one is a rise in age.

97
00:09:24,470 --> 00:09:26,820
So we just need to do the same thing basically.

98
00:09:26,840 --> 00:09:29,400
And then we'll get food and age.

99
00:09:29,780 --> 00:09:34,630
So in fact I'll post a video and see if you can do that.

100
00:09:34,700 --> 00:09:41,390
Could you post a video and try it out and see whether you can write the rest of the code to detect the

101
00:09:41,540 --> 00:09:46,620
full edge.

102
00:09:46,650 --> 00:09:47,220
All right.

103
00:09:47,220 --> 00:09:49,140
Welcome back.

104
00:09:49,590 --> 00:09:51,210
OK let's do it together.

105
00:09:51,230 --> 00:09:54,160
I trust you tried your best.

106
00:09:55,380 --> 00:09:59,720
It's very straightforward and I'm sure most of you got it.

107
00:10:00,600 --> 00:10:05,100
So the next day and I'll just put a comment to a cop just take an edge which

108
00:10:07,890 --> 00:10:09,180
like this to fall in edge.

109
00:10:09,210 --> 00:10:13,120
And what we have to do is exactly the same thing first.

110
00:10:13,140 --> 00:10:22,740
We cleared a capture flag like we did above after we've cleared a couple to flag we've pulled the low

111
00:10:22,750 --> 00:10:27,170
interrupt status register to see whether the second bit is high.

112
00:10:27,200 --> 00:10:31,150
Good to see Coach hustle kid right.

113
00:10:31,210 --> 00:10:33,300
It's the same thing then.

114
00:10:34,880 --> 00:10:41,750
If it is high because we have it wild appear the program is going to freeze onto the cultural cars.

115
00:10:42,260 --> 00:10:47,050
If it's high we store it in the other variable which is the low edge.

116
00:10:47,050 --> 00:10:48,410
Now we have the.

117
00:10:48,680 --> 00:10:49,440
We have.

118
00:10:49,670 --> 00:10:53,030
We have detected the high edge and the low edge.

119
00:10:53,030 --> 00:10:59,450
The next thing we have to do is declare a variable to store the difference between the high edge and

120
00:10:59,450 --> 00:11:06,710
the low edge and then declared a variable that would return the results to what I'm going to do next

121
00:11:06,800 --> 00:11:11,840
is declare some constants and some yeah some new variables.

122
00:11:11,840 --> 00:11:13,690
So let's declare a new variable.

123
00:11:13,700 --> 00:11:16,440
I'm going to call this variable D distance.

124
00:11:16,460 --> 00:11:20,750
You can call it whatever you want to call it the distance.

125
00:11:20,750 --> 00:11:24,860
I don't know why that would be easier but could the distance.

126
00:11:24,860 --> 00:11:28,580
This one is going to start a distance in centimeters.

127
00:11:28,640 --> 00:11:36,840
We're going to create a constant and this constant is going to hold our period in the period of our

128
00:11:36,840 --> 00:11:39,900
run and frequency is sixty two point five.

129
00:11:40,710 --> 00:11:43,330
None of seconds and this is how you write.

130
00:11:43,420 --> 00:11:50,530
You're right sixty two point five E minus nine sixty two point five nanoseconds and then we need and

131
00:11:50,570 --> 00:11:52,120
multiply a constant.

132
00:11:52,650 --> 00:11:55,480
So this number here was derived from the speed of sound.

133
00:11:55,530 --> 00:12:04,360
We need this to multiply our timestamp and our run frequency in order to get a distance in centimeters.

134
00:12:04,470 --> 00:12:04,980
Right.

135
00:12:04,980 --> 00:12:07,080
So we have these new variables.

136
00:12:07,080 --> 00:12:10,760
Let's see what we use them for too.

137
00:12:10,890 --> 00:12:17,770
The next thing would be to store the difference between the low edge and high edge in the distance.

138
00:12:18,120 --> 00:12:18,690
Right.

139
00:12:18,690 --> 00:12:28,500
Since the low edge bulkhead latest you can just look up the H minus high edge like this.

140
00:12:28,500 --> 00:12:33,680
And then we have the difference of the two timestamps stand here and to get a distance.

141
00:12:34,050 --> 00:12:45,250
Finally we just multiply the distance by the multiplier and then by 0 60 megahertz one clock.

142
00:12:45,390 --> 00:12:52,810
Then we have the distance and because the function returns in that two would just return the distance.

143
00:12:52,820 --> 00:12:58,820
This is a straightforward yes.

144
00:12:58,830 --> 00:13:01,070
This function is complete.

145
00:13:01,170 --> 00:13:07,670
This function would return the distance of an object right in front of the ultrasonic sensor.

146
00:13:07,730 --> 00:13:17,830
And this is able and this program is able to do this by measuring the time between two events that rise

147
00:13:17,840 --> 00:13:19,730
in age in the fall and age.

148
00:13:19,830 --> 00:13:25,020
This one application of time is was used in designing drivers and signal processing.

149
00:13:25,890 --> 00:13:29,070
So I will pause here and continue.

150
00:13:29,070 --> 00:13:34,270
Later we continue the next lesson that we write the application to test this.

151
00:13:34,370 --> 00:13:39,320
We just write we basically complete the main function in the next lesson.

152
00:13:39,390 --> 00:13:40,820
So see you in the next lesson.
