Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

THERMO LM35 DZ with PIC16F877

Status
Not open for further replies.

derrick826

New Member
Hi guys ,

i have a problem with interfacing the THERMO LM35 into PIC16F877. Basically i have already done a program to calculate the voltage converted in the PIC16F877 by using the internal ADC in to a temperature value. I have simulated my program in the PIC simulator IDE using the 7-segments LED ( common cathode) and it actually works. As the voltage value of the ADC changes due to the LM35 which converts the temperature to voltage value , it basically gives me the correct display on the 7-segment LEDS.

Based on the LM35 DZ data shee, + 10.0 mV/°C scale factor, i have calculated that let's say it's a temperature of 25 celcius , .....so the device itself will output about 0.25v which goes to the 10-bit ADC of the PIC that converts into a digital value of 51 in decimal. With that converted value , i have divided the 51 decimal by 2 to get back my temperature value which is approximately 25.5 celcius which will be displayed on the 3 7-segments leds.

i build the hardware as followed :
**broken link removed**


but it didn't give the expected display on the 3 7-segments LEDS. The displays didn't turn out to be numbers. Is my PIC program has something wrong or the hardware which i have build is wrong?:confused: hope you guys can give me a helping hand.. thanks
 
Your PIC program is probably wrong, you're probably not converting the numbers to the correct bit patterns for the LED's.

You might check my seven segment tutorial?, where I use a lookup table to do this.
 
Hi derrick826 can you tell me how do you get the value 51? Does it an automatically generated value from AD module (without calculated).
 
You haven't connected a voltage reference for the A to D. (Or is this not required for a pic A to D)
 
Well if VDD is 5V, and Voltage from the LM35 is going to be approximately 0.25V at all times, won't this cause issues with a 10bit resolution?

Edit: I take that back... it's fine
 
Last edited:
list p=pic16f877, f=inhx8m
include <P16F877.INC>
__config _CP_OFF & _PWRTE_ON & _XT_OSC & _WDT_OFF & _BODEN_OFF & _LVP_OFF

ERRORLEVEL -302 ;Suppress bank warning

CBLOCK h'20'
digit1
digit2
digit3
resultlbyte
resulthbyte
L_byte
H_byte
R0
R1
R2
count
temp
delaytime
s_digit
R2_TEMP
INDICATOR
disp_u ;1st digit address
disp_t ;10th digit address
disp_h ;100th digit address
g_time1
g_time2
ledindex
delaylarge
delaysmall

ENDC


RC0 equ .0
RC1 equ .1
RC2 equ .2





;----------------------------------------------------------------------------
; RESET VECTOR
;----------------------------------------------------------------------------
ORG 0x00
goto start
;
;----------------------------------------------------------------------------
org 0x04
BCF STATUS,RP1 ; select bank 0
BCF STATUS,RP0
BTFSC PIR1,TMR2IF
goto display
BTFSC INTCON,T0IF
goto getvalue
goto $




;----------------------------------------------------------------------------
getvalue
BCF STATUS,RP1 ; select bank 0
BCF STATUS,RP0
BCF INTCON,T0IF
BSF ADCON0,GO ; conversion starts now!
again2
BTFSC ADCON0,GO
goto again2
call getresult
retfie

getresult
BCF STATUS,RP1 ; select bank 1
BSF STATUS,RP0
movf ADRESL,w ; low byte of result.
bcf STATUS,RP0
movwf resultlbyte ; a/d low byte saved
movf ADRESH,w ; high byte of result
movwf resulthbyte ; a/d high byte saved
call atodforbcd
call xxx
call B2_BCD
call bcdsplit
call match
return

atodforbcd rrf resultlbyte,f ; routine for dividing the value
bcf STATUS,C
rrf resulthbyte,f
btfsc STATUS,C
bsf resultlbyte,7 ;
xxx movf resultlbyte,w ;
movwf L_byte ; bcd conversion subroutine.
movf resulthbyte,w ;
movwf H_byte ;
return



B2_BCD bcf STATUS,0 ; clear the carry bit
movlw .16
movwf count
clrf R0
clrf R1
clrf R2
loop16 rlf L_byte, f
rlf H_byte, f
rlf R2, f
rlf R1, f
rlf R0, f
;
decfsz count, f ; routine for bcd conversion
goto adjDEC
retlw 0
;
adjDEC movlw R2
movwf FSR
call adjBCD ; no bank switching, always
; ; indirect access for RAM
movlw R1
movwf FSR
call adjBCD
;
movlw R0
movwf FSR
call adjBCD
;
goto loop16
;
adjBCD movlw 3
addwf 0,W
movwf temp
btfsc temp,3 ; test if result > 7
movwf INDF
movlw 30
addwf 0,W
movwf temp
btfsc temp,7 ; test if result > 7
movwf INDF ; save as MSD
retlw 0
;----------------------------------------------------------------------------
; Arrange the result as digits 1,2,3.

bcdsplit movf R1,w ; bring first nibble
andlw 0Fh ; mask the upper nibble
movwf digit1 ; send to display routine variable.

movf R2,w ; bring the second digit!
MOVWF R2_TEMP
andlw 0F0h ; mask the lower nibble.
movwf digit2 ; send to display routine variable.
swapf digit2,f ; after swaping!

movf R2,w ; Again bring the lowbyte
andlw 0Fh ; mask the upper nibble
movwf digit3 ; send it to display routine variable
return
;----------------------------------------------------------------------------

match
MOVF R2_TEMP,W
SUBLW H'80'
BTFSS STATUS,Z
BTFSS STATUS,C
GOTO ON
GOTO STOP

ON
MOVLW B'10000000'
MOVWF PORTD
RETURN

STOP
MOVLW B'00000000'
MOVWF PORTD
RETURN

;**************** LED display control *****************



display_table
addwf PCL,f ; W + PCL -> PCL
retlw b'00111111' ; '0'
retlw b'00000110' ; '1'
retlw b'01011011' ; '2'
retlw b'01001111' ; '3'
retlw b'01100110' ; '4' table for segment patterns
retlw b'01101101' ; '5'
retlw b'01111101' ; '6'
retlw b'00000111' ; '7'
retlw b'01111111' ; '8'
retlw b'01100111' ; '9'
retlw b'10000000' ; '.'

;----------------------------------------------------------------------------
; SUBROIUTINES
;----------------------------------------------------------------------------

display

BCF STATUS,RP1 ;
bcf STATUS,RP0 ; bank 0
BCF PIR1,TMR2IF
movf digit1,w
movwf disp_h
movf digit2,w
movwf disp_t
movf digit3,w
movwf disp_u



movlw b'00000001' ; select the left most digit
movwf ledindex ;
movwf PORTC ; digit selecting transistor on!
movf disp_h,w ; get the bcd value to display
call digitout ; illuminate for a period

rlf ledindex,f ; rotate to left to select next digit
movf ledindex,w ; enable next digit
movwf PORTC ; digit selecting transistor on!
movf disp_t,w ; get the bcd value.
call digitout ; illuminate for a period

rlf ledindex,f
movf ledindex,w ; last digit selected.
movwf PORTC
movf disp_u,w ; get bcd value
call digitout ; illuminate for a period
retfie
;----------------------------------------------------------------------------

digitdelay movlw 5h ; outer loop for delay
movwf delaylarge
loopl decf delaylarge,f
btfsc STATUS,Z
return
movlw 0FFh
movwf delaysmall
loops decfsz delaysmall,f ; inner loop for delay
goto loops
goto loopl

;----------------------------------------------------------------------------

digitout nop
call display_table ; illuminate digit and wait
movwf PORTB ; for some time
call digitdelay
clrf PORTB ; off the digit and return.
clrf PORTC
return








;----------------------------------------------------------------------------
; Main program starts
;----------------------------------------------------------------------------
start

CLRF resultlbyte
CLRF resulthbyte
CLRF s_digit
call iniports ; initialise all relevent ports.
call iniatod ; initialise a/d converter module.
call initimer0
call initimer2 ;



movlw h'07'
movwf delaytime
aquiloop
decfsz delaytime,f ; wait for aqusition time.
goto aquiloop
BCF STATUS,RP1 ; select bank 0
BCF STATUS,RP0
BCF PIR1,ADIF
BSF ADCON0,GO ; conversion starts now!
again1
BTFSC ADCON0,GO
goto again1
call getresult
goto $ ; wait for interrupt




;_______________________________________________________________________________________________________________




;______________________________________________________________________________________________________________
iniports bsf STATUS,RP0 ; set bank 1
movlw b'00000001' ; input for analogue temp sensor
movwf TRISA ; RA0 as the input
movlw b'01111000'
movwf TRISC
movlw b'00000000'
movwf TRISB ; all portd output for segments.
movwf TRISD ; porte for digit selection
bcf STATUS,RP0 ; return to bank 0 and return
clrf PORTA
clrf PORTB
clrf PORTC
return
;_________________________________________________________________________________________________________________
iniatod bsf STATUS,RP0
movlw b'10001110' ; result right justified ,ra0 as
movwf ADCON1 ; analogue input with Vdd as Vref
bcf STATUS,RP0
movlw b'01000001' ; a/d clock Tosc*8 and Ra0 channel
movwf ADCON0 ; selected. a/d module still off.
return

;_____________________________________________________________________________________________________________________

initimer0
BCF STATUS,RP1 ; set bank 0
BSF STATUS,RP0
movlw b'11010011'
movwf OPTION_REG
movlw h'43'
movwf TMR0
BCF TMR0,T0IF
return

;______________________________________________________________________________________________________________________
initimer2 ;*** Display initialization (Timer2)

BCF STATUS,RP1 ; set bank 0
BCF STATUS,RP0
movlw b'00011110' ;OPS=1:4,T2=ON,EPS=1:16
movwf T2CON ;Set T2CON register
bsf STATUS,RP0 ;Change to Bank1
movlw d'157' ;157x64=10048usec 157
movwf PR2 ;Set PR2 register
bsf PIE1,TMR2IE ;TMR2IE=ON
bcf STATUS,RP0 ;Change to Bank0


;*** Interruption control
movlw b'11100000' ;GIE=ON,PEIE=ON,T0IE=ON
movwf INTCON ;Set INTCON register
return
;______________________________________________________________________________________________





;_________________________________________________________________________________________________________________________

end

here you go.... but i have already double checked my bit patterns for the LED's.
 
derrick826 said:
is my connection for the LM35 correct?

hi,
That looks OK, what do you measure with a DVM on the output pin of the LM35?? at say room temp, knowing that its 10mV/deg??

Copied your code, will give a spin later on, let you know!
 
here in my country is about 26 degree celcius so referring to the data sheet it produces 10mv/1 degree celcius.... the voltage which i get is approximately 0.26v when measuring with a DVM

answer for the above question:Hi derrick826 can you tell me how do you get the value 51?

0.25v if 25 degree celcius is divided by (5/1023) which gives 51.15

I suspect is the hardware configuration part? cause i have tried so many times to improve on the program, and retest on the simulation...... i'm not that good when comes to the hardware part..
 
Last edited:
i didn't use any base resistors for Q1-Q3 will it have any effect?
what do you mean standard pinout? i'm using the basic common cathode 7-segment display which has 10 pins (something like DIP pins)
 
derrick826 said:
i didn't use any base resistors for Q1-Q3 will it have any effect?
what do you mean standard pinout? i'm using the basic common cathode 7-segment display which has 10 pins (something like DIP pins)

hi,
Resistors are recommended, say 470R/1K.

If I am reading your diagram correctly, you are using PNP transistors, so the LED will be enabled when the PIC pin is Low 0v???

If you are driving the segments High shouldnt the transistors be npn, ON when the pin is High???
 
Last edited:
I agree with you, Eric. The transistors must be npn type, BC337 for example.
1k base resistors should be fine.
 
o000 meaning that for the RC0, RC1 and RC2 pin should be connected in series to a 470R/1K ohm before connecting to the PNP transistors and the output for each of this pins must be a 0 v, low to switch on the 7-segment led (common cathode). am i right.?
 
i have changed the diagram
**broken link removed**
is this the right one now? and the program have to be changed.. instead of driving high i must drive low
 
hi eng1,

I am pro-simulators, BUT they must be used with caution as they can appear to run a program correctly.

When I used to design surveying equipment, I had to rely heavily on simulation,
so I had a number of PC's configured as different types of equipment.

My argument in favour of simulation, if it works in the simulator it dosnt necessarily mean it will work in the field,
BUT if dosn't work in simulation it will NEVER work in the field... A sort of guilty until proven innocent approach!;)

Regards
 
hi,
NPN transistors as you are setting the transistor drive pins high.

The 1K's are OK
 
Last edited:
ericgibbs said:
hi eng1,

I am pro-simulators, BUT they must be used with caution as they can appear to run a program correctly.

When I used to design surveying equipment, I had to rely heavily on simulation,
so I had a number of PC's configured as different types of equipment.

My argument in favour of simulation, if it works in the simulator it dosnt necessarily mean it will work in the field,
BUT if dosn't work in simulation it will NEVER work in the field... A sort of guilty until proven innocent approach!;)

Regards

Hi Eric, is your observation directed at me? I totally agree with your point of view.
 
Status
Not open for further replies.

Latest threads

Back
Top