Hello and in this PIC programming tutorial. I am going to be doing an
analog temperature sensor and then we are physically going to display
the temperature on eight segment displays.
Now let's have a look at the schematic. The analog temperature sensor that I'm
going to be using is a lm35a . Now this is one of the more
simple temperature sensors. You just have your supply voltage in
ground and then our analog voltage out is going
to the analog input of the PIC on RA0 and the rest
is just a circuit for the eight segment display.
I have a video that explains the circuit. Now let's have a look at the datasheet.
Now the lm35a is an analog temperature sensor.
Which is the lm35 series are precision integrated circuits
temperature devices with an output voltage linear proportional to
centigrade temperature. Then its main feature it has a linear
temperature scale of 10 millivolts per degree celsius. So
it's fairly simple to do the temperature conversion for these
analog sensors. I am going to be using the
basics integrate temperature sensor which basically works from 2 degrees to
150 degrees Celsius. Which is literally just
the V+ to ground and then your output to
an analog sensor. Then you have the full range
temperature sensor which adds the one resistor here and then to a negative
ground. Because you need a negative voltage on
these sensors to be able to display a negative degrees celsius which
you can see over here and then here's also the calculation for
negative degrees but we are going to go with the basic integrate temperature
sensor. Since it rarely drops below zero degrees in my area. Then in section
three (7.3). We have the transfer function. Which is
actually fairly simple. We have our Vout times our millivolts
times temperature. Then we just need to manipulate this transfer function to get
the temperature out according to the voltage. Which is
basically going to be our voltage that we get out times 0.01. To get our
temperature. Okay now the code is a stripped down
version of the eight segment display code. So
we retain most of the functions. We have the uart
printout functions and then the only functions we keep from it
is seg numbers and then seg convert numbers which we are going to change a
bit later to work with floating points and then we have our standard oscillator
for 8 megahertz setup. Our pins output setup and then the uart
initialized with interrupts enabled with priority.
Just the string to send us data out and then uart receive and then in the
interrupts We have the uart receive in the high
priority interrupt. All right. Now let's start with the code. Now the
first thing we add is our adc resolution. Which I'm just
going to go paste in here since i've covered it in
the previous tutorial. Now the next thing we add is our degree
centigrade per volt which is 10 millivolts so then we
add a hash define c per volt which gives us 10 millivolts
so 0.01 volts per degree centigrade for the analog
temperature. Then we go down to our global variables. We
create a volatile unit
16 and we call that sensor temp and we say u16.
So we know which data variable we have here. Then
we add another variable which is also going to be volatile and we call
that tmr0 overflow and we set it also equals to
zero. Now the tmr0 is just going to be a overflow counter and that is going to
be a uint 16,
as well. Now since we have the basic set up here. Next thing you want to do is
initialize the adc. So we quickly just create a function void
adc underscore init and we give it void parameters.
Since i've covered this in a previous video I'm going to
just copy the previous video settings paste them in.
PCFG for the analog inputs. We are going to set to 0x0E. So we set
RA0 as a input so we also have to do the TRISA
register bits and we say RA0 is equals to
one for input and that needs to be a dot. Now we need to set up a interrupt
for the analog to digital converter. So we
say the peripheral interrupt enable one and we
say bits and we have ADIE.
So we enable the interrupt here. Then we have to set the priority of the
interrupt. So IPR bits... IPR
one bits and we say the priority is equals to
one. To put it as a high priority interrupt. And this
is optional what i'm going to do now is PIR one bit ADIF.
We manually clear the flag. Now this is optional but
it is good practice. Okay then we have our analog setup and
our interrupt setup. Then we say adc init. Copy that function. Stick it in before
the uart init. The next module we need to set up is the
timer. So we just create a function void tmr
0 init and we also pass it void parameters. Also something i covered
in a previous video. Just quickly paste that in. Now basically
this timer is set up to overflow once a millisecond
or once every 1 comma 0 2 4 milliseconds it will overflow.
Okay it's also a high priority interrupt and
it is currently switched off. So we take this tmr0 init
function put that under adc init and we enable
that. And then for the timer we need to switch
it on. So we take the TCON0 bits and
just before our main for loop starts. We say TCON0 equals one.
To switch our timer on. Okay now we have two interrupts.
That we need to setup. Which is the adc and the timer interrupts. So we say
if and we create another if under that and then for the adc in the first if we
say PIR one bits dot ADIF
equivalent to one. Clear that interrupt. After we
have entered it. Okay now we need to capture the value that we get from the
adc. So we go back to the top. We get the
sensor temp unit 16 value. We go back down to our
interrupt. We say sensor temp is equals to
ADRES H. Left shift it by eight and in case that in brackets and then we
or that with ADRES L.
And that will capture our current value of the temperature sensor
in a unit 16 form. So just a raw reading from the analog temperature sensor. Now
we need to set up timer and that will be INTCON
bits and we say TMRIF equals one and we also
clear the flag over here and then we quickly go to the top.
Then we find our timer overflow we just quickly stick that in here. And every
time it overflows just increment the timer overflow. Ok now we need to write a
function that converts our analog reading to
a temperature reading. Then we have a function that returns a float
and we are going to call that adc to temp. So our analog value to a
temperature value. That takes in a unit 16 value
and we are just going to call that raw. So
we just return our... Let's just correct that spelling. We just
return raw times our adc
res. So our raw times our adc resolution gives
us the voltage that we currently have and then we divide our adc
resolution by c per volt. That will give us the floating point
number of what the current temperature is to whatever decimal point. So now that
we have the adc temperature we are going to need
some method of displaying that temperature. So we go
back to our seg convert number. So we just
duplicate this function. This is now where c++ function
overloading would have been nice but we don't have that. So we just call
this function float. So seg convert float that will
take in a floating point number and we'll just
call that temp for temporary not temperature and
we say unit 16 underscore t. We then take num
is equals to temp times a hundred. So basically what we're doing is we're
just shifting the decimal points so that we can actually display it on
the eight segments itself. Since the eight segments
are in a type of bcd format. So we're just converting it to the second decimal
point. So we're multiplying by 100 and then we
chuck away the rest of the decimal points. So that
we have a real number and then we have our
remainder. Rhen we need to add our decimal point.
So we go to the second segment. Which is this one. Then we say
LATB is or equals to one.
To switch on the dot on the segment. Then after we deselect the segment. We say LAT
B is and equals to 0xFE. And that will clear the dot bin on the
eight segment display. And that needs to be 0x for
hexadecimal. Quickly do a test compile. Okay so the code compiles. Now we are
going to get to the meat of the stuff and actually start displaying it on the
eight segment display. So what we create in our main is just a float and we say
that's temp reading. Take that we copy it
and we go. After our UART has printed. We say temp reading is equals to adc
temp and we pass in. What is the global variable again.
Sensor temp unit 16 to it. So that will give us a temperature
reading but before we do that we need to trigger a adc read.
So before anything else we trigger our adc read so this is going to be a
singular read just to initialize our values. So that we
don't have zero zero on our display as it comes up. Then we
have the temp reading equals to adc to temp. Ok, now the reason I
initialize the timer we have if tmr0 overflow is greater or equals to 500.
So every 500 milliseconds update our temperature reading on the
display. And then when we are done with that just
reset the timer overflow variable to zero. So that we continuously can have
this read and check if we need to do a conversion. Then we are going to add a
small little debug for us. We say if your char is equivalent
to the character d so or debug we just print out a small
little string to the screen. Which i have pre-prepped
over here. With an sprintf. Just going to change
that to the temperature reading. What this
function is doing if i send a d to it from my computer via the uart.
Then it basically prints out whatever the temperature
is currently on the sensor. So that we can cross reference and debug.
Then we go get our display function for the floating point. So
seg convert float. We take that and we just call it here.
Then we take our temp reading. We insert that in.
So this will update the display on every loop. And then
we take our adc trigger function. We go underneath our segment display.
Then we remove the Nop from this. We change the while to an
if. We reindent it quickly. We say if GODONE is equals to zero.
Then do another reading on the adc. So we just trigger our adc read again
here. Okay and then we have our earth receive
that if there is any titan the uart received just reset the variable back to
false. okay so let's quickly program the PIC
Okay now we have the temperature sensor over
here. Now you can see it's about 16 to 17 degrees our
temperature is currently. Now when i press my finger against the sensor.
You'll see it start to increase slowly but surely.
Now the random jumps is because i'm not averaging the temperature
value. As i hold it. Need to apply more heat to it.
That gives me about 24 degrees. Then when i let the sensor go.
Let it just rest. Then the temperature will go down again.
Slowly but surely.
Now while we wait i'm just going to give a seizure warning
because it's going to start flashing. When I press
d we are going to have a debug message on the uart
and you can see the debug message on screen.
And now we just press any other key and that will disable the debug message
and return it to normal. And then we can see the temperature is
actually decreasing slowly to ambient again. And that's using
a PIC with a analog temperature sensor.
The code will be on git. A like comment and subscription will
be greatly appreciated. Thank you have a nice day.