Hello.
Welcome back.
In this lesson we're going to see how to write an ADC drive.
We're going to see how to configure our ADC to read analog inputs so I'm going to create a new project
by coming to buy a new car new Vision project and I'll give this a name
a cautious ADC basic and then I'll quote a project ADC basic
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
and then on the Simpsons.
Also the call over here.
Okay.
Going to rename this to the name of the board.
Tim 4C.
One two three.
To expand this core this up and target options set us to 16 then debug dropdown.
Still there is a CDI settings reset and run.
Okay.
And then.
Okay.
Going to right click over here to New item to see file.
I call it main right.
So our team 4C 1 2 3 2.
I see a launch pad audit Tim 4C 1 2 3 microcontroller itself comes with 2 ADC modules each of the modules
are twelve bit in resolution to program them then we first need to take a look at the registers we need
in order to be able to write ADC drivers so we'll talk a bit about it registers before we start writing
the code first and we need to do like any module we need to enable proc access to the module and the
ADC is not different to enable clock access to the ADC module we need to access the RC GC ADC register
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
module we want to use the sample pussy queen size a part of the ADC module and this moves the conversion
result of the ADC to one of the ADC Firefox there are 4 sample sequences they are known as SS three
Ss to SS 1 and SS 0 each of them is associated with a faithful defy force of different sizes so these
sample sequences also have different sizes and sizes and sizes are equal to the defy force the associated
two.
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
this one can have eight samples and then we've got SS one which can have four samples
SS simply means sample sequencer and we've got SS to which in her four samples to because the five for
it is associated with has a depth of four and we've got SS three which can hold just a single sample.
So in this lesson we shall be using SS 3 because we're going to be pulling the analog signal just from
a single channel.
So this would suffice.
And in order to select the sample sequence to use will be we'll have to access a register known as the
ADC 80 SS This is known as the ADC active sample sequence I register and we're going to go to this register
and select the sample sequencer that we want to use when our sample sequence are 3 2 1 0 0.
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
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
everything no talking at all and in fact like we saw in the earlier sections the data sheet actually
gives us step by step on how to configure or initialize that particular module.
So all of this is going to be found there.
If something is unclear you can either send me a message or improve your embedded system skills by going
to the data sheet to check up on the ADC section so there are different ways of starting their conversion
using the ADC.
We have the option to start the conversion using a timer P drop M and A low comparator extend or signal
from GPO or software trigger to study conversion.
The selection trigger for SS 3 is done via a register known as the ADC IMU ex register.
This is known as the ADC MOX register which will use this as well.
And this we shall use to select whether we want a software trigger P.W. more time or trigger and trigger.
Basically means what do you want to be the trigger for the conversion to start.
After we we decide what trigger to use we have to access at a register known as ADC P SS I to start
the conversion every time there is new everytime this new reading from the ADC inputs Channel 2.
Also we have 2 ADC modules and I'll show you.
I've got this pinata here for how they are mapped to the various pins.
So this is our ADC here.
We just put a comment here so these are your ADC lines.
If you want an ADC you can access from any of these lines here right.
But again you can find this information from the data sheet.
So this is just yet to show you.
Right.
So these are the ADC lines P2P.
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
P before 5.
These are our ADC lines and you can choose from any of these and when you go to the data datasheet you
would see this there as well right.
So we can detect the end of conversion in a bit located in the ADC raw interrupter register is known
as the ADC r i s so we can find a particular bits in this register ADC right as to know what our conversion
is complete or not by pulling this flag we know that the conversion is complete and we can read the
value from the ADC sequencer.
What are we using sequence of 3 or sequence are in a sequence that are we using when we pull a particular
bit in this register to check whether the conversion is complete.
Once it's complete we can read the value.
Also we have the option to use interrupt rather than pulling and we can just set an ADC interrupts to
inform us when the conversion is complete and when the conversion is complete the data is going to be
read automatically.
And after reading the data from the ADC SS faithful which is the sample sequence of faithful we must
clear the into three flag in the ADC array as we must clear a flag here.
After reading the data so that another conversion can be detected without clearing the flag the new
conversion cannot be detected.
The right interrupts flag is cleared by writing to these to a particular bits in the ADC interrupt status
register which we shall see we are talking much let's just get straight to it.
I'm just going to wipe all of this off like I said we can find them in the data sheet.
And yeah if you have any questions just let me know.
But I'm going to start with a code over here.
I'll include a board header and I'll come over here and say it into me.
And if you didn't understand the quick preview I gave you need not worry.
I say type I'm going to be adding comment here.
So we start off by enabling the clock access.
We're going to do is in this program we're going to use ADC input channel 0 and this is connected to
P3 and we'll be using some pull sequence at three because we need just one channel right sampler sequencer
3s five for sizes 1.
And since we have one channel that is sufficient.
So we've got to enable clock access to this GPL European as well as enabling clock access to the ADC
module.
After that we've got to set the GPL open to alternate function and the alternate function we want is
ADC.
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.
Sure.
X 1 0 and this essentially says
enable clock for Port E.
That's what we have here.
Once they start our say system control all CTC ADC and then I'm going to pass your X through one here
and this is
enabled clock for it is easier or just put ADC more jobs here.
Here's what I you understand.
If you are using ADC module 1 you can change this to the next bit which is built to.
Once they stand we've got to initialize P3 for analog input
and I'm going to start by saying general purpose input output e over here and I'm going to go to the
alternate function selector register then I'm going to pass a tool here
and this essentially says enable alternate function that's what we've done here.
Once that's done we're going to disable digital functionality since we want the analog functionality
over here so I'll see how you put E and digital enable register.
I'm going to disable it by putting Amber sound over here.
Understand you quote equal to to day and then just put a comment here.
A digital function once that's done with code to enable the analog function by accessing the analog
selector register to analog mode selector register.
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.
If you expand aid you realize that age corresponds to p 3 right age corresponds to be 3
enable analog function.
Once that's done you start off by
would we initialize the ATC.
So we stop by disabling the sample sequence at 3 before configuration.
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
going to disable it by passing this over here.
And this is
right.
That's what we have here.
Once that is done we're going to select software trigger from the earmarks register.
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
you x F and then one two three three zeros.
And if you go to the earmarks register and you expand this you realize that we are disabling the bit
and this keeps us the software trigger option.
Right.
So whatever you don't understand.
If you don't understand why we're disabling bits.
Number three here.
By passing eight you can simply go to the digital enabled register and see what happens when bits.
Number three is set to zero.
And it applies to any register you see in this course.
Right.
That is why I started a course by showing you how to go to the data sheet locator register and then
locate specific bits in the register.
But if after all this you still have questions you can always send me a message.
Right.
So this over here we're seeing it.
Say a software trigger conversion.
Once that is done we are going to get input from the channel 0
we're going to do 80 C0 over here and then I'm going to see SS x3.
Then I'll pass you here.
Once that's done we take one sample at a time and set a flag at the fair sample because we have just
one sample to do them going to say 80 C0 and then SS control number three over here
and I'll pass six over here
and once that's done we enable ATC 0 plus sequence of three by coming back eighty six here over here
and then it s.t. SS and then we pass a two over here like this
and this keeps us enable SS 3 right.
So this right here should enable the agency for us.
Now we can go and start conversion and wait for conversion to complete.
We do this in a.
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
it over here and this essentially says start a conversion at SS 3.
Once that's done we've got to wait for conversion complete and we do this by saying well
80 you are right s and 8 Coursera
we get stuck here we do nothing.
So this is it full conversion complete
and we're going to create a global variable here to hooded the resort.
Can see end result.
I'm going to come down here and see a resort because it is a zero SS five four three.
That is where the resort is like this.
And once they start started we've got to clear the completion of flag by doing an 86 year ISC equals
eight.
And this should cleared a completion flag for us
right.
So let's build and see if we have an arrow click over here.
It's built successfully.
Let's get it on our board and see how it works.
So let's download onto our board.
I've downloaded onto my board and this my set up of a potential meter here.
It's got three legs and the middle leg.
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
the three plus three vote and the right leg to the ground.
So I've connected using these three wires the middle leg is connected to P3 over here and then the leftmost
one is connected to ground and the right moves one is connected to three plus three over here.
It's a simple potential meter and again you can replace this with any sense or at all I'm just using
a potential meter to demonstrate this.
So I'm just going to stick the potential meter into the bread board and once that is done I'm going
to open our debugger click over here
and I'm going to add our global variable to the watch window double click this right click and then
up to watch window one
and here it is.
I'm going to right click this to disable hexadecimal display and I'm going to run the code by clicking
over here and as you can see this the analog value.
So I say move the potential meter you see the value change.
Right.
So I can move it to the opposite side.
This is what I get right.
So this is it.
This is how to initialize the ADC on your TVC board.
Right.
If you have any questions at all just send me a message or leave them any questions area.
And this the end of the lesson I shall see you later on disable the debug view by clicking over here.