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.
just to double check eric
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



BSF STATUS,C ; set carry bit so that when rotate = 0 to on
movlw b'11111110' ; 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

BSF STATUS,C ; set carry bit so that when rotate = 0 to on
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

BSF STATUS,C ; set carry bit so that when rotate = 0 to on
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

my new code with the ones output a low for PNP type.. is it correct now? for the above posted diagram which has PNP transsitors
 
eng1 said:
Hi Eric, is your observation directed at me? I totally agree with your point of view.

hi eng1,
No, it was just a comment to re-enforce to Derrick the limitations of using PIC sims..:)
 
derrick,
Which lines did you change?

I had to cut and paste the original, losing the formatting, had to reformat a line at a time.!
 
LOL sorry eric ... the ones in bold is the one i have changed and added ( at the display subroutine)

BSF STATUS,C ; set carry bit so that when rotate = 0 to on
movlw b'11111110' ; 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

BSF STATUS,C ; set carry bit so that when rotate = 0 to on
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

BSF STATUS,C ; set carry bit so that when rotate = 0 to on
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
;----------------------------------------------------------------------------
 
hi derrick,
Thats a lot better.
Digit select from msd to lsd is C2,C1,C0, nice regular selection on SIM.

Results:
0.25Vin = 025
0.5V = 051
1.0 =102

Is this what you are expecting?
 
yea thanks a lot eric and the rest who have helped me... i must try in on the real hardware first.... thanks a bunch guys... might have some problems then i will post them as i need guidance... thanks a bunch!
 
if u need to measure room temperature,

then u can measure 0-100C temp easly w/o any software calculations

if u get 10mv/C then use an op-amp with gain 5, so max voltage to input be 5000mv = 5V (for 100C temp)

n use internal reference to VDD = 5 volts

so u get exact reading in 10bit ADC

e.g

At 30C, LM35 gives 300mv then op-amp will give 1.5V. on ADC

10 bit shows u around 300, convert it to Digits n place decimal.
 
Hi guys, it works perfectly well and i have some questions to ask,

why must we put a 1kohm resistor at the RC0,Rc1 and RC2 pin of the PIC microcontroller?

how come the resistor value which I put, 470ohm at the RB0- RB7 differs from the 1kohm resistor at RC0-RC2.... ?

what is the purpose of having this resistors for?

thanks in advance
 
derrick826 said:
Hi guys, it works perfectly well and i have some questions to ask,

why must we put a 1kohm resistor at the RC0,Rc1 and RC2 pin of the PIC microcontroller?

how come the resistor value which I put, 470ohm at the RB0- RB7 differs from the 1kohm resistor at RC0-RC2.... ?

what is the purpose of having this resistors for?

They are all current limiters, the 1K's limit the base current to the transistors, and the 470 ohms limit the current to the LED's.
 
derrick826 said:
why must we put a 1kohm resistor at the RC0,Rc1 and RC2 pin of the PIC microcontroller?
They limit the current into the base of the transistors when they're switched on.

derrick826 said:
how come the resistor value which I put, 470ohm at the RB0- RB7 differs from the 1kohm resistor at RC0-RC2.... ?
The 470 ohm resistors limit the current into the LEDs.


The two values (1k and 470) are calculated with the same formula (Omh's law) but with different values of current and voltage drop accross the resistors.
 
so your are actually saying that if i do not put any resistors, the current flowing to both the transistors and the LEDs are big and therefore it might damage those components for a long period?
 
blueroomelectronics said:
X4ce your math is off, the A/D has 1024 steps not 1000. The ideal solution is a vref of 4.096 V

thx for reminding me that :), i know there r 1024 steps in 10bit A2D.

BTW he needs to measure room temperature, don;t need any high accuracy.
 
negative temperature reading

Hi, I wanted to add a negative temperature reading into this project. what will I add in the circuit? tnx
 
Hi, I wanted to add a negative temperature reading into this project. what will I add in the circuit? tnx

Hi,
The LM35 in the basic configuration only goes as low a +2Cdeg.

If you look at the datasheet you can add 2 diodes and a 18K resistor to get as low as -55Cdeg.
The only problem is that the output from LM35 is from the top of the 2 diodes and the LM35 output, so the signal is not referenced to 0V.

A better choice of sensor is the LM135 that output values related to Kelvin.
 
This is my circuit. The button is for switching between C and F
Celcius.jpg Fahrenheit.jpg

Ive tried doing what's in the datasheet (adding the diodes and res) to be able to measure negative degrees. I think I can work with the negative, just altering my codes. But now, the problem lies with the positive ones.
**broken link removed**
 

Attachments

  • neg.jpg
    neg.jpg
    44.5 KB · Views: 262
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top