1
00:00:00,720 --> 00:00:01,060
Hello.

2
00:00:01,080 --> 00:00:01,680
Welcome back.

3
00:00:01,680 --> 00:00:06,210
In this lesson we're going to see how to write an ADC drive.

4
00:00:06,270 --> 00:00:13,350
We're going to see how to configure our ADC to read analog inputs so I'm going to create a new project

5
00:00:13,380 --> 00:00:19,340
by coming to buy a new car new Vision project and I'll give this a name

6
00:00:26,770 --> 00:00:33,550
a cautious ADC basic and then I'll quote a project ADC basic

7
00:00:37,010 --> 00:00:48,160
then I'll select the board team 4C 1 2 3 4 h 6 p.m. and then OK then under device l select start up

8
00:00:49,780 --> 00:00:51,260
and then on the Simpsons.

9
00:00:51,270 --> 00:00:55,560
Also the call over here.

10
00:00:55,640 --> 00:00:56,870
Okay.

11
00:00:57,020 --> 00:00:59,560
Going to rename this to the name of the board.

12
00:00:59,830 --> 00:01:01,500
Tim 4C.

13
00:01:01,760 --> 00:01:03,400
One two three.

14
00:01:03,510 --> 00:01:15,460
To expand this core this up and target options set us to 16 then debug dropdown.

15
00:01:15,470 --> 00:01:19,550
Still there is a CDI settings reset and run.

16
00:01:19,550 --> 00:01:20,060
Okay.

17
00:01:20,080 --> 00:01:20,450
And then.

18
00:01:20,450 --> 00:01:21,410
Okay.

19
00:01:21,590 --> 00:01:26,020
Going to right click over here to New item to see file.

20
00:01:26,030 --> 00:01:29,090
I call it main right.

21
00:01:30,290 --> 00:01:32,870
So our team 4C 1 2 3 2.

22
00:01:32,890 --> 00:01:41,960
I see a launch pad audit Tim 4C 1 2 3 microcontroller itself comes with 2 ADC modules each of the modules

23
00:01:42,050 --> 00:01:48,500
are twelve bit in resolution to program them then we first need to take a look at the registers we need

24
00:01:48,830 --> 00:01:56,450
in order to be able to write ADC drivers so we'll talk a bit about it registers before we start writing

25
00:01:56,450 --> 00:02:03,500
the code first and we need to do like any module we need to enable proc access to the module and the

26
00:02:03,530 --> 00:02:11,870
ADC is not different to enable clock access to the ADC module we need to access the RC GC ADC register

27
00:02:12,290 --> 00:02:21,260
and whether we want to use ADC 0 or adc 1 we've got to enable the 0 or 1 bits for the appropriate ADC

28
00:02:21,260 --> 00:02:28,280
module we want to use the sample pussy queen size a part of the ADC module and this moves the conversion

29
00:02:28,280 --> 00:02:36,860
result of the ADC to one of the ADC Firefox there are 4 sample sequences they are known as SS three

30
00:02:36,920 --> 00:02:45,380
Ss to SS 1 and SS 0 each of them is associated with a faithful defy force of different sizes so these

31
00:02:45,380 --> 00:02:52,470
sample sequences also have different sizes and sizes and sizes are equal to the defy force the associated

32
00:02:52,480 --> 00:02:53,450
two.

33
00:02:53,690 --> 00:03:00,410
I'll just come over here and put a bit of a comment over here so we've got SS 0 some pussy Kwanza and

34
00:03:00,410 --> 00:03:09,080
this one can have eight samples and then we've got SS one which can have four samples

35
00:03:12,980 --> 00:03:19,640
SS simply means sample sequencer and we've got SS to which in her four samples to because the five for

36
00:03:19,640 --> 00:03:31,860
it is associated with has a depth of four and we've got SS three which can hold just a single sample.

37
00:03:31,860 --> 00:03:38,010
So in this lesson we shall be using SS 3 because we're going to be pulling the analog signal just from

38
00:03:38,010 --> 00:03:39,230
a single channel.

39
00:03:39,240 --> 00:03:41,760
So this would suffice.

40
00:03:41,760 --> 00:03:47,910
And in order to select the sample sequence to use will be we'll have to access a register known as the

41
00:03:48,510 --> 00:03:57,270
ADC 80 SS This is known as the ADC active sample sequence I register and we're going to go to this register

42
00:03:57,600 --> 00:04:03,560
and select the sample sequencer that we want to use when our sample sequence are 3 2 1 0 0.

43
00:04:03,630 --> 00:04:10,560
So yeah don't worry we're going to write code to show all of this but by now you know how to find information

44
00:04:10,560 --> 00:04:17,370
from the data sheet as well so you can just go to the data sheet go to the part on ADC and you'll find

45
00:04:17,370 --> 00:04:23,310
everything no talking at all and in fact like we saw in the earlier sections the data sheet actually

46
00:04:23,310 --> 00:04:29,310
gives us step by step on how to configure or initialize that particular module.

47
00:04:29,310 --> 00:04:31,240
So all of this is going to be found there.

48
00:04:31,290 --> 00:04:39,180
If something is unclear you can either send me a message or improve your embedded system skills by going

49
00:04:39,180 --> 00:04:47,130
to the data sheet to check up on the ADC section so there are different ways of starting their conversion

50
00:04:47,640 --> 00:04:48,950
using the ADC.

51
00:04:49,050 --> 00:04:56,100
We have the option to start the conversion using a timer P drop M and A low comparator extend or signal

52
00:04:56,100 --> 00:05:00,690
from GPO or software trigger to study conversion.

53
00:05:00,840 --> 00:05:07,500
The selection trigger for SS 3 is done via a register known as the ADC IMU ex register.

54
00:05:07,500 --> 00:05:11,840
This is known as the ADC MOX register which will use this as well.

55
00:05:12,000 --> 00:05:18,960
And this we shall use to select whether we want a software trigger P.W. more time or trigger and trigger.

56
00:05:18,960 --> 00:05:23,550
Basically means what do you want to be the trigger for the conversion to start.

57
00:05:23,790 --> 00:05:35,640
After we we decide what trigger to use we have to access at a register known as ADC P SS I to start

58
00:05:35,670 --> 00:05:42,460
the conversion every time there is new everytime this new reading from the ADC inputs Channel 2.

59
00:05:42,480 --> 00:05:46,450
Also we have 2 ADC modules and I'll show you.

60
00:05:46,450 --> 00:05:51,530
I've got this pinata here for how they are mapped to the various pins.

61
00:05:51,540 --> 00:05:53,570
So this is our ADC here.

62
00:05:53,660 --> 00:05:57,150
We just put a comment here so these are your ADC lines.

63
00:05:57,150 --> 00:06:02,750
If you want an ADC you can access from any of these lines here right.

64
00:06:03,540 --> 00:06:07,160
But again you can find this information from the data sheet.

65
00:06:07,590 --> 00:06:09,700
So this is just yet to show you.

66
00:06:10,020 --> 00:06:11,010
Right.

67
00:06:12,090 --> 00:06:15,010
So these are the ADC lines P2P.

68
00:06:15,490 --> 00:06:26,670
P 0 through P 3 PD 1 through PD 3 PD 0 actually through PD 3 and then we've got P 5 before and then

69
00:06:26,790 --> 00:06:28,410
P before 5.

70
00:06:28,410 --> 00:06:33,150
These are our ADC lines and you can choose from any of these and when you go to the data datasheet you

71
00:06:33,150 --> 00:06:36,230
would see this there as well right.

72
00:06:36,270 --> 00:06:44,700
So we can detect the end of conversion in a bit located in the ADC raw interrupter register is known

73
00:06:44,700 --> 00:06:55,800
as the ADC r i s so we can find a particular bits in this register ADC right as to know what our conversion

74
00:06:55,800 --> 00:07:01,680
is complete or not by pulling this flag we know that the conversion is complete and we can read the

75
00:07:01,680 --> 00:07:04,550
value from the ADC sequencer.

76
00:07:04,590 --> 00:07:10,170
What are we using sequence of 3 or sequence are in a sequence that are we using when we pull a particular

77
00:07:10,170 --> 00:07:13,180
bit in this register to check whether the conversion is complete.

78
00:07:13,200 --> 00:07:15,390
Once it's complete we can read the value.

79
00:07:15,390 --> 00:07:21,690
Also we have the option to use interrupt rather than pulling and we can just set an ADC interrupts to

80
00:07:21,690 --> 00:07:27,300
inform us when the conversion is complete and when the conversion is complete the data is going to be

81
00:07:27,300 --> 00:07:29,070
read automatically.

82
00:07:29,070 --> 00:07:35,520
And after reading the data from the ADC SS faithful which is the sample sequence of faithful we must

83
00:07:35,520 --> 00:07:41,210
clear the into three flag in the ADC array as we must clear a flag here.

84
00:07:41,220 --> 00:07:48,570
After reading the data so that another conversion can be detected without clearing the flag the new

85
00:07:48,570 --> 00:07:50,650
conversion cannot be detected.

86
00:07:50,850 --> 00:07:58,440
The right interrupts flag is cleared by writing to these to a particular bits in the ADC interrupt status

87
00:07:58,440 --> 00:08:02,560
register which we shall see we are talking much let's just get straight to it.

88
00:08:02,610 --> 00:08:06,830
I'm just going to wipe all of this off like I said we can find them in the data sheet.

89
00:08:07,020 --> 00:08:10,070
And yeah if you have any questions just let me know.

90
00:08:10,300 --> 00:08:12,890
But I'm going to start with a code over here.

91
00:08:12,940 --> 00:08:18,650
I'll include a board header and I'll come over here and say it into me.

92
00:08:20,660 --> 00:08:26,380
And if you didn't understand the quick preview I gave you need not worry.

93
00:08:26,400 --> 00:08:29,110
I say type I'm going to be adding comment here.

94
00:08:29,390 --> 00:08:33,470
So we start off by enabling the clock access.

95
00:08:33,530 --> 00:08:42,440
We're going to do is in this program we're going to use ADC input channel 0 and this is connected to

96
00:08:42,530 --> 00:08:49,210
P3 and we'll be using some pull sequence at three because we need just one channel right sampler sequencer

97
00:08:49,260 --> 00:08:51,080
3s five for sizes 1.

98
00:08:51,130 --> 00:08:53,820
And since we have one channel that is sufficient.

99
00:08:54,050 --> 00:09:00,170
So we've got to enable clock access to this GPL European as well as enabling clock access to the ADC

100
00:09:00,170 --> 00:09:01,070
module.

101
00:09:01,070 --> 00:09:06,770
After that we've got to set the GPL open to alternate function and the alternate function we want is

102
00:09:06,830 --> 00:09:08,180
ADC.

103
00:09:08,250 --> 00:09:19,340
So I'm going to come over here and see a system control RC GC GPL you and I'm going to see over here.

104
00:09:19,330 --> 00:09:19,640
Sure.

105
00:09:19,700 --> 00:09:23,850
X 1 0 and this essentially says

106
00:09:27,390 --> 00:09:30,870
enable clock for Port E.

107
00:09:31,290 --> 00:09:32,650
That's what we have here.

108
00:09:32,730 --> 00:09:44,020
Once they start our say system control all CTC ADC and then I'm going to pass your X through one here

109
00:09:44,660 --> 00:09:45,470
and this is

110
00:09:52,440 --> 00:10:01,640
enabled clock for it is easier or just put ADC more jobs here.

111
00:10:01,650 --> 00:10:03,230
Here's what I you understand.

112
00:10:03,650 --> 00:10:09,480
If you are using ADC module 1 you can change this to the next bit which is built to.

113
00:10:09,500 --> 00:10:13,490
Once they stand we've got to initialize P3 for analog input

114
00:10:17,370 --> 00:10:26,870
and I'm going to start by saying general purpose input output e over here and I'm going to go to the

115
00:10:26,870 --> 00:10:31,190
alternate function selector register then I'm going to pass a tool here

116
00:10:34,030 --> 00:10:41,900
and this essentially says enable alternate function that's what we've done here.

117
00:10:42,020 --> 00:10:48,170
Once that's done we're going to disable digital functionality since we want the analog functionality

118
00:10:48,590 --> 00:10:55,170
over here so I'll see how you put E and digital enable register.

119
00:10:55,320 --> 00:10:59,030
I'm going to disable it by putting Amber sound over here.

120
00:10:59,120 --> 00:11:12,000
Understand you quote equal to to day and then just put a comment here.

121
00:11:12,440 --> 00:11:22,420
A digital function once that's done with code to enable the analog function by accessing the analog

122
00:11:22,420 --> 00:11:27,960
selector register to analog mode selector register.

123
00:11:28,020 --> 00:11:37,680
I say GPL you e over here and I'll see it m s e l This is the register and then I'll pass it over here.

124
00:11:37,870 --> 00:11:45,430
If you expand aid you realize that age corresponds to p 3 right age corresponds to be 3

125
00:11:51,620 --> 00:11:55,400
enable analog function.

126
00:11:55,400 --> 00:11:58,280
Once that's done you start off by

127
00:12:01,460 --> 00:12:05,780
would we initialize the ATC.

128
00:12:07,820 --> 00:12:13,210
So we stop by disabling the sample sequence at 3 before configuration.

129
00:12:13,220 --> 00:12:22,670
I'm gonna say ATC 0 over here and then a DC T S S this the sampler sequence I register and then I'm

130
00:12:22,670 --> 00:12:25,880
going to disable it by passing this over here.

131
00:12:28,550 --> 00:12:29,870
And this is

132
00:12:49,290 --> 00:12:49,590
right.

133
00:12:49,590 --> 00:12:51,150
That's what we have here.

134
00:12:51,150 --> 00:12:56,290
Once that is done we're going to select software trigger from the earmarks register.

135
00:12:56,300 --> 00:13:08,550
I'll say 80 C0 earmarks and I'm going to do and sign over here cause Tuesday and then I'm going to pass

136
00:13:08,550 --> 00:13:15,540
you x F and then one two three three zeros.

137
00:13:15,630 --> 00:13:21,570
And if you go to the earmarks register and you expand this you realize that we are disabling the bit

138
00:13:22,050 --> 00:13:25,170
and this keeps us the software trigger option.

139
00:13:26,980 --> 00:13:27,270
Right.

140
00:13:27,290 --> 00:13:28,850
So whatever you don't understand.

141
00:13:30,110 --> 00:13:32,990
If you don't understand why we're disabling bits.

142
00:13:32,990 --> 00:13:33,700
Number three here.

143
00:13:33,710 --> 00:13:39,700
By passing eight you can simply go to the digital enabled register and see what happens when bits.

144
00:13:39,710 --> 00:13:41,520
Number three is set to zero.

145
00:13:41,690 --> 00:13:44,950
And it applies to any register you see in this course.

146
00:13:45,080 --> 00:13:45,410
Right.

147
00:13:45,410 --> 00:13:51,650
That is why I started a course by showing you how to go to the data sheet locator register and then

148
00:13:52,130 --> 00:13:54,800
locate specific bits in the register.

149
00:13:54,800 --> 00:13:59,520
But if after all this you still have questions you can always send me a message.

150
00:13:59,560 --> 00:13:59,960
Right.

151
00:14:00,080 --> 00:14:06,420
So this over here we're seeing it.

152
00:14:06,510 --> 00:14:09,840
Say a software trigger conversion.

153
00:14:10,700 --> 00:14:14,730
Once that is done we are going to get input from the channel 0

154
00:14:17,550 --> 00:14:25,140
we're going to do 80 C0 over here and then I'm going to see SS x3.

155
00:14:25,440 --> 00:14:36,090
Then I'll pass you here.

156
00:14:36,140 --> 00:14:42,530
Once that's done we take one sample at a time and set a flag at the fair sample because we have just

157
00:14:42,530 --> 00:14:51,470
one sample to do them going to say 80 C0 and then SS control number three over here

158
00:14:54,370 --> 00:14:55,730
and I'll pass six over here

159
00:15:19,840 --> 00:15:26,500
and once that's done we enable ATC 0 plus sequence of three by coming back eighty six here over here

160
00:15:27,070 --> 00:15:33,240
and then it s.t. SS and then we pass a two over here like this

161
00:15:36,260 --> 00:15:43,080
and this keeps us enable SS 3 right.

162
00:15:43,080 --> 00:15:45,660
So this right here should enable the agency for us.

163
00:15:45,690 --> 00:15:50,540
Now we can go and start conversion and wait for conversion to complete.

164
00:15:50,610 --> 00:15:51,480
We do this in a.

165
00:15:51,840 --> 00:16:05,040
While one loop do well one over here open close and I'm going to say it is easier P SS I can then pass

166
00:16:05,100 --> 00:16:19,970
it over here and this essentially says start a conversion at SS 3.

167
00:16:20,650 --> 00:16:25,770
Once that's done we've got to wait for conversion complete and we do this by saying well

168
00:16:30,130 --> 00:16:39,300
80 you are right s and 8 Coursera

169
00:16:41,980 --> 00:16:43,490
we get stuck here we do nothing.

170
00:16:44,450 --> 00:16:47,960
So this is it full conversion complete

171
00:16:54,300 --> 00:16:58,230
and we're going to create a global variable here to hooded the resort.

172
00:16:58,270 --> 00:17:01,570
Can see end result.

173
00:17:01,660 --> 00:17:14,550
I'm going to come down here and see a resort because it is a zero SS five four three.

174
00:17:14,600 --> 00:17:17,040
That is where the resort is like this.

175
00:17:17,530 --> 00:17:24,860
And once they start started we've got to clear the completion of flag by doing an 86 year ISC equals

176
00:17:24,990 --> 00:17:25,930
eight.

177
00:17:26,000 --> 00:17:28,470
And this should cleared a completion flag for us

178
00:17:31,600 --> 00:17:32,140
right.

179
00:17:33,310 --> 00:17:38,500
So let's build and see if we have an arrow click over here.

180
00:17:38,670 --> 00:17:40,570
It's built successfully.

181
00:17:40,570 --> 00:17:42,960
Let's get it on our board and see how it works.

182
00:17:44,080 --> 00:17:46,530
So let's download onto our board.

183
00:17:46,630 --> 00:17:51,250
I've downloaded onto my board and this my set up of a potential meter here.

184
00:17:51,250 --> 00:17:53,530
It's got three legs and the middle leg.

185
00:17:53,530 --> 00:18:03,370
I'm going to connect to our P3 which is our ADC line P3 and then I'm going to connect the left leg to

186
00:18:03,370 --> 00:18:08,070
the three plus three vote and the right leg to the ground.

187
00:18:08,410 --> 00:18:15,640
So I've connected using these three wires the middle leg is connected to P3 over here and then the leftmost

188
00:18:15,640 --> 00:18:20,770
one is connected to ground and the right moves one is connected to three plus three over here.

189
00:18:20,770 --> 00:18:26,530
It's a simple potential meter and again you can replace this with any sense or at all I'm just using

190
00:18:26,740 --> 00:18:29,160
a potential meter to demonstrate this.

191
00:18:29,200 --> 00:18:34,480
So I'm just going to stick the potential meter into the bread board and once that is done I'm going

192
00:18:34,480 --> 00:18:37,990
to open our debugger click over here

193
00:18:47,660 --> 00:18:55,050
and I'm going to add our global variable to the watch window double click this right click and then

194
00:18:55,170 --> 00:18:56,430
up to watch window one

195
00:19:00,250 --> 00:19:00,890
and here it is.

196
00:19:00,890 --> 00:19:05,750
I'm going to right click this to disable hexadecimal display and I'm going to run the code by clicking

197
00:19:05,750 --> 00:19:09,140
over here and as you can see this the analog value.

198
00:19:09,140 --> 00:19:13,570
So I say move the potential meter you see the value change.

199
00:19:13,720 --> 00:19:14,440
Right.

200
00:19:14,470 --> 00:19:17,680
So I can move it to the opposite side.

201
00:19:17,680 --> 00:19:21,590
This is what I get right.

202
00:19:21,600 --> 00:19:22,260
So this is it.

203
00:19:22,260 --> 00:19:26,760
This is how to initialize the ADC on your TVC board.

204
00:19:26,850 --> 00:19:27,210
Right.

205
00:19:27,210 --> 00:19:31,260
If you have any questions at all just send me a message or leave them any questions area.

206
00:19:31,260 --> 00:19:38,510
And this the end of the lesson I shall see you later on disable the debug view by clicking over here.
