PDA

View Full Version : 12f638 simple temperature monitor



DavyJones
- 21st May 2015, 21:35
Hi all, I'm probably overlooking something simple I missed but I just can't seem to figure it out. I was messing around with a DS18B20 and a 12f683 and copied some simple code to read the DS and display the temperature from it on an LCD screen. So far so good all of that works. I tried to add what I thought was going to be a simpe IF statement to check the temperature and turn an LED on but this is turning out to be difficult for me. What is happening is the temp displays just fine on the LCD but my LED never comes on or at best it blinks on ever so slightly. I've tried a bunch of different combinations hooking the LED up so that low LED will turn it on instead of high LED and different combinations of using a pull up or down depending on what high or low I was trying but nothing seems to work. Below is the program I am using. I am used the U2 programmer from MELABS. If anyone could shed some light on this I would appreciate it. Just to make sure I am using the correct low/high syntax I have my code toggling the LED before I get into the main loop and the LED in fact works like a charm it just breaks when I get into mainloop.
Thanks
David

Include "modedefs.bas" ' Mode definitions for Serout

LCD Var GPIO.1 ' LCD TX pin
DQ VAR GPIO.4 'pin 3 One-wire Data-Pin "DQ"
C VAR BYTE 'Status of I/O port bit
R_Temp VAR WORD 'RAW Temperature readings
C_neg VAR BIT '=1 if cold bit for C set
NEGPOS VAR BYTE 'Sign char "-" or "+"
C_Temp VAR WORD 'Celsius Temperature
F_Temp VAR WORD 'Fahrenheit Temperature
maxT var word
LED CON 2 ' pin 5 for led indicator

ANSEL = 0 ' Set all digital
CMCON0 = 7 ' Analog comparators off
Pause 500 ' Wait .5 second for LCD to init
low led
pause 1000
high led
pause 1000
mainloop:
gosub start_Convert
goto mainloop
Start_Convert:
OWOUT DQ, 1, [$CC,$44] 'Send calculate temperature command
REPEAT
PAUSEUS 25 'Waiting loop until reading complete
OWIN DQ, 4, [C] 'Monitor for pin high transition ...
UNTIL C <> 0 ' ... reading complete
OWOUT DQ, 1, [$CC, $BE] 'Send read scratchpad command
OWIN DQ, 2, [R_Temp.LowByte,R_Temp.HighByte] 'get temperature
IF R_Temp.11 = 1 THEN 'check below zero bit
C_neg=1
NEGPOS = "-"
R_Temp = ~R_Temp + 1
ELSE
NEGPOS = "+"
C_neg=0
ENDIF
C_Temp = R_Temp >> 3 'Get whole # of degrees & first decimal bit
'SEROUT LCD , n2400,[$fe, 1,"C = ",NEGPOS,#C_Temp/2," deg. C",10,13]
F_Temp = C_Temp * 9
F_Temp = F_Temp / 5
F_Temp=F_Temp/2 'remove decimal bit
IF NEGPOS="+" THEN
F_Temp = F_Temp + 32
ENDIF
IF NEGPOS ="-" AND C_Temp <= 36 THEN 'was 18 - caused error!
F_Temp = 32 - F_Temp
NEGPOS = "+"
ENDIF
IF NEGPOS="-" AND C_Temp > 36 THEN 'was 18 - caused error!
F_Temp = F_Temp - 32
ENDIF
SEROUT LCD , n2400,[$fe, 1,"F = ",NEGPOS,#F_Temp," deg.F",10,13]
C_Temp = C_Temp/2
if F_temp =>70 then
low LED
else
high LED
endif
return
end

Tabsoft
- 21st May 2015, 22:56
Just a couple of ideas.

1. You have no pause in your main loop so you are polling the DS18B20 extremely fast. You may want to try a Pause 1000 (1second) in between you gosub start_Convert and your goto mainloop.

2. Your If/Then LED logic in start_Convert will only light the LED when the temperature is below 70 degF.


if F_temp =>70 then
low LED
else
high LED
endif

DavyJones
- 21st May 2015, 23:14
Tabsoft, Thanks...... I tried the pause but doesn't seem to have any effect. I have my LED reversed at the moment so low LED should actually turn it on when it gets above 70. Just for giggles I moved the LED up to GPIO.0 and it works perfectly. I think I am not initializing GPIO.2 correctly.... When I did have the LED blinking very dimly the blink seemed to corresponded to when the LCD was getting data sent to it on GPIO.1... it seemed to blink the same time the LCD would flash when updating. Perhaps I can't use GPIO.2 for an output to light the LED.



Just a couple of ideas.

1. You have no pause in your main loop so you are polling the DS18B20 extremely fast. You may want to try a Pause 1000 (1second) in between you gosub start_Convert and your goto mainloop.

2. Your If/Then LED logic in start_Convert will only light the LED when the temperature is below 70 degF.


if F_temp =>70 then
low LED
else
high LED
endif

Heckler
- 22nd May 2015, 04:32
Davy,

Our dearly departed Darrell Taylor (world class programmer, *(my opinion)) left us with this little tidbit...
it's an include file that will set most any PIC's pins to "all digital"
just put this file in the same directory as your code and then put this line somewhere near the top of your code


INCLUDE ALLDIGITAL15.BAS

you may need to eliminate other attempts to turn off analog's so they don't confilict.

Note: the 15 in the file name indicates that it is his version 1.5

I hope this helps and that I haven't led you astray as I am not the greatest programmer :biggrin:

Tabsoft
- 22nd May 2015, 06:21
Try these settings:

OPTION_REG = $FF
INTCON = $00
PIE1 = $00
PCON = $10
ANSEL = $00
CMCON0 = $07
TRISIO =$00
WPU = $00
IOC = $00
CCP1CON = $00
ADCON0 = $00

Change this "LED CON 2 ' pin 5 for led indicator" to this
"LED var GPIO.2"

AvionicsMaster1
- 25th May 2015, 17:26
Perhaps I can't use GPIO.2 for an output to light the LED. You can use GPIO.2 to light an LED if properly designed. I don't know much but I know that. Since you moved it to GPIO.0 and it worked then it should be designed correctly.

I think tabsoft has it right to change LED to LED var GPIO.2

Ifn that doesn't work can you show schematic?

Tabsoft
- 30th May 2015, 17:56
Just curious if you got this issue resolved David.