Home - Search - Members
Full Version: (SOLVED) Problem with LM35, ADC0804 and 89s52 interface
Pages: 1, 2
rvs
May 9 2012, 1:33 PM
I have a school project and I can't make it work correctly. I used diagram and code from this page http://www.eprlabs.com/2011/11/89s52-based-temperature-sensor-using-adc/. I added reset button, relay and ISP for programming. Now, the program shows values from 011 to 014 at room temperature (about 24 degrees in Celsius). Can anyone help me with writing the code, so it would work correctly? In the code I'm using right now, the relay isn't used at all.
Looking forward to solutions!

Raivo


Picture of diagram is
http://static3.album.ee/../files/1068/45/orig_26711343_BgYl.jpg

Names of elements in diagram are not correct, because Eagle didn't have correct items and I used items, that had same size/number of legs/function for printig on board.

Ajay Bhargav
May 9 2012, 8:22 PM
I am not able to see the circuit clearly. can you attach the full resolution image in post instead of directly posting it?

Ajay Bhargav
May 9 2012, 8:23 PM
[Topic moved to 8051 Discussion forum]

rvs
May 9 2012, 9:03 PM
I hope this is better
http://static3.album.ee/../files/1068/50/orig_26712615_7S3c.jpg
Ajay Bhargav
May 10 2012, 7:54 PM
you're using a different sensor that what is used on that website. I mean that website uses LM35 whereas you're using LM50 so calibration table will change i guess. Do check datasheets of both the sensors and do the modifications accordingly.
rvs
May 10 2012, 8:23 PM
I'm using LM35. Like I said in previous post, I had to use elements that Eagle had in it (I used LM50 in diagram for printig on board because it was the same size and holes fitted on board).
Ajay Bhargav
May 10 2012, 9:00 PM
yeah one more thing which is missing in your circuit is.. ADC0804 VREF is connected to VCC where as in your case its left unconnected. please do that change coz Vref will have direct effect on the ADC readings, as its the reference voltage for calculating the ADC value.
rvs
May 11 2012, 7:38 AM
Thank you! Got it working! It isn't still on on room temperature, it shows abooud +- 1 degree, but that is not a big deal at the moment.

Now second part of my problem. I need to get relay to switch (for example) at 27 degrees Celsius. Could someone please write me the code for it (so I could copy-paste it to my original code).
pH
May 11 2012, 11:49 AM
@rvs , its really simple , just take the value from ADC into a register and compare it to required value , depending on the output (if register value is zero , less than or equal to) you can either set or reset the pin to which you have your relay connected

and dont expect someone to write a code for you , try it urself and if the problem exists we are all here to help you
rvs
May 11 2012, 1:25 PM
I know the principle of how it shoul be done, but I don't can't write it in assembler code, I don't understand what (or where) is #38H, #0EH, #06H....

It takes from register 1 and puts it into #38H? Then it calls register 1 again? Why does it move register 1 to so many places?


It's simpler to say that I don't know assembler code..
rvs
May 13 2012, 7:21 AM
Got some (lots of it) help and made new code, that should activate relay at 27 degrees Celsius. Perhaps someone could check it out and let me know if this code will work or if it will not work.


PS! In code I attach LCD's R/W (pin 5) to P1.4, If this code works, then I will connect these two pins, if not, then I will leave LCD's R/W connected to GND.

org 0h
RAM_ADDR EQU 30H
COUNT EQU 3

hundreds equ 30h
tens equ 31h
ones equ 32h


ACALL DELAY_200M
ACALL ONETIME_LCDINIT

STT:
ACALL ADC
ACALL Bin2Dec
ACALL DEC_ASCI_CONVRT
ACALL DELAY_200M
ACALL DELAY_200M
ACALL DELAY_200M
sjmp STT

;=================================================
;this subroutine is used to take data from ADC and
;keep to Accumulator
;=================================================
ADC:
mov A,P0 // ACD output to P.0
nop
nop
ret
Bin2Dec:
mov b,#100d
div ab
mov hundreds,a
mov a,b
mov b,#10d
div ab
mov tens,a
mov ones,b
ret

DEC_ASCI_CONVRT:
MOV R0,#RAM_ADDR
MOV R2,#3
BACKk: MOV A,@R0
ORL A,#30H
ACALL DATA_DISPLAY
INC R0
DJNZ R2,BACKk
RET

ONETIME_LCDINIT:
MOV A,#38H
ACALL COMMAND
MOV A,#0EH
ACALL COMMAND
MOV A,#01H
ACALL COMMAND
MOV A,#06H
ACALL COMMAND
MOV A,#86H
ACALL COMMAND
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COMMAND:
ACALL READY
MOV P2,A // LCD D0 to D7 connected to P2
CLR P3.4 // LCD RS (pin4) to P3.4
CLR P1.4 // LCD R/W (pin5) to P1.4 (I took unused port for this)
SETB P3.5 // LCD E (pin6) to P3.5
CLR P3.5
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DATA_DISPLAY: // I changed port numbres here
ACALL READY
MOV P2,A
SETB P3.4
CLR P1.4
SETB P3.5
ACALL DELAY
CLR P3.5
RET
READY: SETB P2.7
CLR P3.4
SETB P1.4
BACK: CLR P3.5
ACALL DELAY
SETB P3.5
JB P2.7,BACK
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ACALL ADC // added relay control correctly??
CJNE A,#27, TRIPP
SETB P1.3 // pin connected to relay
TRIPP:
ACALL Bin2Dec
RET // is this necessary??
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DELAY_200M:
MOV R7,#30H
L1_DELAY:
MOV TMOD,#10
MOV TH1,#3CH
MOV TL1,#0B0H
SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
DJNZ R7,L1_DELAY
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DELAY: MOV R3,#50
HERE3: MOV R4,#255
HERE2: DJNZ R4,HERE2
DJNZ R3,HERE3
RET
END
ExperimenterUK
May 13 2012, 3:59 PM


Got some (lots of it) help and made new code, that should activate relay at 27 degrees Celsius.

rvs


Does that mean activate relay at exactly 27 degrees Celsius..
or 27 degrees and above or 27 degrees and below ?
rvs
May 13 2012, 5:41 PM
27 and above relay should be active...for example if in theory it is connected to a fan, fan would lower temperature (in environment where it is reading temperature) until it is under 27 degrees again and then it would switch off.
ExperimenterUK
May 13 2012, 7:17 PM


ACALL ADC // added relay control correctly??
CJNE A,#27, TRIPP
SETB P1.3 // pin connected to relay
TRIPP:
ACALL Bin2Dec
RET // is this necessary??

rvs






This should do it.
Add a call to TestTemperature to the main loop.
TestTemperature:
	ACALL ADC  ;read temperature
	CJNE A,#27,CheckFurther   ;jmp if not exactly 27 deg
			;is 27 so turn on
RelayOn:
	SETB P1.3   ;set pin connected to relay high
        ret
CheckFurther:
	jnc RelayOn  ;jmp if greater than 27
                      ;less than 27 so turn off
	CLR P1.3  ;set pin connected to relay low
        ret


rvs
May 13 2012, 7:31 PM
org 0h
RAM_ADDR EQU 30H
COUNT EQU 3

hundreds equ 30h
tens equ 31h
ones equ 32h


ACALL DELAY_200M
ACALL ONETIME_LCDINIT

STT:
ACALL ADC
ACALL Bin2Dec
ACALL DEC_ASCI_CONVRT
ACALL DELAY_200M
ACALL DELAY_200M
ACALL DELAY_200M
sjmp STT

;=================================================
;this subroutine is used to take data from ADC and
;keep to Accumulator
;=================================================
ADC:
mov A,P0 // ACD output to P.0
nop
nop
ret
Bin2Dec:
mov b,#100d
div ab
mov hundreds,a
mov a,b
mov b,#10d
div ab
mov tens,a
mov ones,b
ret

DEC_ASCI_CONVRT:
MOV R0,#RAM_ADDR
MOV R2,#3
BACKk: MOV A,@R0
ORL A,#30H
ACALL DATA_DISPLAY
INC R0
DJNZ R2,BACKk
RET

ONETIME_LCDINIT:
MOV A,#38H
ACALL COMMAND
MOV A,#0EH
ACALL COMMAND
MOV A,#01H
ACALL COMMAND
MOV A,#06H
ACALL COMMAND
MOV A,#86H
ACALL COMMAND
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COMMAND:
ACALL READY
MOV P2,A // LCD D0 to D7 connected to P2
CLR P3.4 // LCD RS (pin4) to P3.4
CLR P1.4 // LCD R/W (pin5) to P1.4 (I took unused port for this)
SETB P3.5 // LCD E (pin6) to P3.5
CLR P3.5
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DATA_DISPLAY: // I changed port numbres here
ACALL READY
MOV P2,A
SETB P3.4
CLR P1.4
SETB P3.5
ACALL DELAY
CLR P3.5
RET
READY: SETB P2.7
CLR P3.4
SETB P1.4
BACK: CLR P3.5
ACALL DELAY
SETB P3.5
JB P2.7,BACK
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ACALL ADC // added relay control correctly??
CJNE A,#27, TRIPP
SETB P1.3 // pin connected to relay
TRIPP:
ACALL Bin2Dec
RET // is this necessary??
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TestTemperature:
ACALL ADC ;read temperature
CJNE A,#27,CheckFurther ;jmp if not exactly 27 deg
;is 27 so turn on
RelayOn:
SETB P1.3 ;set pin connected to relay high
ret
CheckFurther:
jnc RelayOn ;jmp if greater than 27
;less than 27 so turn off
CLR P1.3 ;set pin connected to relay low
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DELAY_200M:
MOV R7,#30H
L1_DELAY:
MOV TMOD,#10
MOV TH1,#3CH
MOV TL1,#0B0H
SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
DJNZ R7,L1_DELAY
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DELAY: MOV R3,#50
HERE3: MOV R4,#255
HERE2: DJNZ R4,HERE2
DJNZ R3,HERE3
RET
END


Did I put it in correct place? So this should be the code, with what my device should work correctly?
ExperimenterUK
May 13 2012, 8:00 PM
Remove this old code...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ACALL ADC // added relay control correctly??
CJNE A,#27, TRIPP
SETB P1.3 // pin connected to relay
TRIPP:
ACALL Bin2Dec
RET // is this necessary??
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

You have a routine called ADC.
;=================================================
;this subroutine is used to take data from ADC and
;keep to Accumulator
;=================================================
ADC:
mov A,P0 // ACD output to P.0

Change it to Read_ADC: as the name will cause confusion

so it becomes...

;=================================================
;this subroutine is used to take data from ADC and
;keep to Accumulator
;=================================================
Read_ADC:
mov A,P0 // ACD output to P.0

Change the new code I gave you to
TestTemperature:
         ACALL Read_ADC ;read temperature



change the main loop to

STT:
ACALL Read_ADC
ACALL Bin2Dec
ACALL DEC_ASCI_CONVRT
ACALL DELAY_200M
ACALL DELAY_200M
ACALL DELAY_200M
ACALL TestTemperature ;test temperature
sjmp STT  


Will it work ?
I don't know ....
I haven't been through every line, (and am not going to ).
Try it, either in hardware or in a simulator such as Proteus.

rvs
May 13 2012, 8:24 PM
What is the command mov b,#100d suposed to do? And mov b,#10d??

Bin2Dec:
mov b,#100d
div ab
mov hundreds,a
mov a,b
mov b,#10d
div ab
mov tens,a
mov ones,b
ret

Tried the code in edsim51, shows error when these two lines are in code. If I delete these two lines, edsim runs without problems.
ExperimenterUK
May 13 2012, 10:09 PM


What is the command mov b,#100d suposed to do? And mov b,#10d??

rvs


mov b,#100d is supposed to load 100 into the b register, probably
the 'd' is an error

try using
mov b,#100 and mov b,#10
or
mov b,#0x64h and mov b,#0x0ah
instead.
rvs
May 14 2012, 6:32 AM
I used code

org 0h
RAM_ADDR EQU 30H
COUNT EQU 3

hundreds equ 30h
tens equ 31h
ones equ 32h


ACALL DELAY_200M
ACALL ONETIME_LCDINIT

STT:
ACALL Read_ADC
ACALL Bin2Dec
ACALL DEC_ASCI_CONVRT
ACALL DELAY_200M
ACALL DELAY_200M
ACALL DELAY_200M
ACALL TestTemperature
sjmp STT

;=================================================
;this subroutine is used to take data from ADC and
;keep to Accumulator
;=================================================
Read_ADC:
mov A,P0
nop
nop
ret
Bin2Dec:
mov b,#100d
div ab
mov hundreds,a
mov a,b
mov b,#10d
div ab
mov tens,a
mov ones,b
ret

DEC_ASCI_CONVRT:
MOV R0,#RAM_ADDR
MOV R2,#3
BACKk: MOV A,@R0
ORL A,#30H
ACALL DATA_DISPLAY
INC R0
DJNZ R2,BACKk
RET

ONETIME_LCDINIT:
MOV A,#38H
ACALL COMMAND
MOV A,#0EH
ACALL COMMAND
MOV A,#01H
ACALL COMMAND
MOV A,#06H
ACALL COMMAND
MOV A,#86H
ACALL COMMAND
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COMMAND:
ACALL READY
MOV P2,A
CLR P3.4
CLR P1.4
SETB P3.5
CLR P3.5
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DATA_DISPLAY:
ACALL READY
MOV P2,A
SETB P3.4
CLR P1.4
SETB P3.5
ACALL DELAY
CLR P3.5
RET
READY: SETB P2.7
CLR P3.4
SETB P1.4
BACK: CLR P3.5
ACALL DELAY
SETB P3.5
JB P2.7,BACK
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TestTemperature:
ACALL Read_ADC
CJNE A,#27,CheckFurther
RelayOn:
SETB P1.3
ret
CheckFurther:
jnc RelayOn
CLR P1.3
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DELAY_200M:
MOV R7,#30H
L1_DELAY:
MOV TMOD,#10
MOV TH1,#3CH
MOV TL1,#0B0H
SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
DJNZ R7,L1_DELAY
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DELAY: MOV R3,#50
HERE3: MOV R4,#255
HERE2: DJNZ R4,HERE2
DJNZ R3,HERE3
RET
END


and it shows 255
and numbers don't start from left side on lcd, but from middle...and it shoes 255->255255->255255255....

If I tried mov b,#100 then it was the same, if I tried mov b,#0x64h numbers doubeled (it showd sth 5XX->5XX5XX....

What did I do wrong?
ExperimenterUK
May 14 2012, 3:31 PM


Thank you! Got it working! It isn't still on on room temperature, it shows abooud +- 1 degree, but that is not a big deal at the moment.

Now second part of my problem. I need to get relay to switch (for example) at 27 degrees Celsius. Could someone please write me the code for it (so I could copy-paste it to my original code).

rvs


You said here that the display was working.
Did it stop working when you added my relay code ?
rvs
May 14 2012, 4:22 PM
I can't have display and programming port connected together, because I accidentally put them too close to each other and if I connect programming port then I have to take LCD off.

If you ment that if display worked when I put your code in.
The problem is that it showd temperature correctly with original code, but from another forum I got a bit different code that I tried to use (and to that code you gave me the relay connecting/temp testing code).

Maybe you can check the original code and maybe there is only few lines missing from it, so it would switch relay at 27 degrees?

Original code is in here http://eprlabs.blogspot.com/2011/11/89s52-based-temperature-sensor-using.html
ExperimenterUK
May 14 2012, 8:58 PM


Maybe you can check the original code and maybe there is only few lines missing from it, so it would switch relay at 27 degrees?

rvs


As you can see the relay switching code is very simple.
Go back to the code that worked (displayed the temperature), and add my relay code to it.
Post your circuit diagram and code as zip attachments.
rvs
May 14 2012, 9:11 PM
No arguing that your posted code is simple, but I can't figure out exactly how that code works. It's like Chinese for me at the moment
rvs
May 14 2012, 9:17 PM
In zip file are picture of diagram (.png) and the code in notepad (.txt)
ExperimenterUK
May 14 2012, 9:51 PM
Here is the original code with minor changes and the relay code added.

rvs
May 15 2012, 6:58 AM
Thank you so much! Got it working, now I can pass this course.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Rickey's World © 2003 - 2007