PDA

View Full Version : 12F508 and Dallas 1820 not working



Picprogman
- 6th February 2012, 18:38
Hello

I am old but new here, just found this forum.

I have written several Picbasic Pro programs using 16F873 or later 16F876A, LCD-display and Dallas 1820 temperature sensors. Now I wanted to make a simple thermostat using the attached code with 12F508. I can't get it working. The program starts ok, I see the led flashing for 1 sec, but then I think the program cannot get any result from the sensor. The rom code is ok, tested the same sensor with 16f876A. I think the problem may be, that I cannot use 12F508 registers right. Here is the code and I WANT to use 12F508 if possible. Please help :)
GPIO.0 is the led pin
GPIO.1 is the data pin


'-----------------------------------------

led var GPIO.0

Tempa Var Word ' Temperature storage
Stat var bit

DQ Var GPIO.1 ' One-wire data pin

TRISIO =%00000010 '???? maybe wrong?
GPIO =%00000000 '???? maybe wrong?




'flash led to see if running

high led
PAUSE 1000
low led

'read 1820

Temp_1:

OWOUT DQ, 1, [$55,$10,$B2,$AD,$D8,$00,$08,$00,$26,$44]
W1:
OWIN DQ, 4, [Stat]' Check for still busy converting
IF Stat = 0 THEN W1' Still busy?, then loop
OWOUT DQ, 1,[$55,$10,$B2,$AD,$D8,$00,$08,$00,$26,$BE]
OWIN DQ, 2, [Tempa.LOWBYTE,Tempa.HIGHBYTE]' Read two bytes, then end communications


If Tempa >= 41 then low led ' if >= 20,5 led off
If Tempa <= 40 then high led ' if <= 20 led on

PAUSE 1000

Goto Temp_1

'-----------------------------------

Demon
- 7th February 2012, 00:53
What CONFIGs are you using?

I don't see any oscillator setting and no mention of what osc you are using.

Robert


EDIT: What about this?


Note:
Erasing the device will also erase the preprogrammed internal calibration value for the internal oscillator. The calibration value must be read prior to erasing the part so it can be reprogrammed correctly later.


Suggestion: Do a small test program using only the LED pin and a loop turning it on and off. Get the PIC running properly first, then add the sensor logic.

And I just have to ask, why use what appears as an archaic PIC? You have a truckload at 1 penny per dozen sitting in the yard?

Picprogman
- 7th February 2012, 06:49
Sorry, I forgot to mention, that I use internal oscillator, and I have set that when programming the chip. I use Epic to program the chip, and internal oscillator is selected there. I tested everything with simple led blink program and it worked ok. And as I mentioned, also the attached program starts fine. Led at GPIO.0 comes up for one second but the program never comes out from sensor routine (Temp_1). I have tested that by adding simple led on/off routine to before Goto Temp_1. So, my guess is, that I cannot configure the data pin (GPIO.1) right. I have used PortC.6 or PortC.0 when using 16F876A and those pins do not require any special configuration.

c_moore
- 7th February 2012, 10:20
If you are using the internal osc you might want to setup the osccon register. I don't see it in your code.

Picprogman
- 7th February 2012, 11:19
Thanks.

I do not know what OSCON has to do with this, because test program (led blink) works, so the program is running without any OSCON-setting. The orinal calibration value is there. The only problem is DS1820 sensor handling. The same read loop (Temp_1) works very well with 16F873 and 16F876A running 4 MHz. The only difference is that I use here GPIO.1 as DQ-pin instead of PortC.0 with those 16F series chips. Maybe I have to try to ask rentron.com, what is wrong with my code with 12F508.

c_moore
- 8th February 2012, 09:56
I do not know what OSCON has to do with this, because test program (led blink) works, so the program is running without any OSCON-setting. The orinal calibration value is there. The only problem is DS1820 sensor handling. The same read loop (Temp_1) works very well with 16F873 and 16F876A running 4 MHz. The only difference is that I use here GPIO.1 as DQ-pin instead of PortC.0 with those 16F series chips. Maybe I have to try to ask rentron.com, what is wrong with my code with 12F508.


I may have been wrong. It looks as though this pic does not have a osccon register. I tried your code on a 12F683 and it did not work either.

Acetronics2
- 8th February 2012, 14:29
Hi,

The first thing is to be sure of your sensor ID ...

The second ... to be sure the bytes are transmitted in the right sequence ( not swapped ) :torn:

from this point of view ... why not try to write and read to it skipping this ID. That could point closer to the reason your code doesn't work ...


Alain

Picprogman
- 9th February 2012, 12:52
Thank you again.

The sensor ID is correct. I have tested the same sensor and the same piece of code with 16F876A using PortC.0, and I can read the sensor without any trouble. Hardware is ok, checked it carefully. Again my opinion is, that something is wrong with my code with GPIO.1. I am not an engineer, and I am not sure how these pins work in 12F508. When using 16F876A or 16F873 no special configuration is needeed. Just define:

DQ var PortC.0

and everything works.

fratello
- 9th February 2012, 13:25
Instead :
TRISIO =%00000010 '???? maybe wrong?
try :
TRISGPIO + %00000010
since I think don't exist TRISIO (see page 32 of datasheet : just TRISGPIO, TRISC, GPIO ....where GPIO is refferenced as PortB)

Picprogman
- 10th February 2012, 10:56
"Instead :
TRISIO =%00000010 '???? maybe wrong?
try :
TRISGPIO + %00000010"

--> Syntax error

Acetronics2
- 10th February 2012, 15:21
Are you really sure of your OWIN parameters ???

I'm not !!!

Alain

fratello
- 10th February 2012, 15:27
Sorry... TRISGPIO = %00000010

Picprogman
- 10th February 2012, 16:04
Are you really sure of your OWIN parameters ???

I'm not !!!

Alain

Thank you. I am fairly new with Picbasic, but all that code is directly copied from rentron.com Picbasic sample page. Only changed the DS1820 ID. I again double checked the ID and the code. I have a check board with a suitable program and an LCD. I just plug in any DS18x20 and can I see immediately the ID and CRC-code. By the way, my english may not be good. I am from Finland.


See the original code at Rentron:

http://www.rentron.com/PicBasic/one-wire2.htm

and find Temp_1 label.

The code works exteremely well with my other programs with 16F87xx Pics. I also changed my sample code a bit and used GPIO.2 as data pin. Datasheet says "General Purpose IO-pin", just like PortC-pins on 16F87xx. But this tiny chip is against me. I have also tested another 12F508 without success. Maybe I have to give up ;)

Picprogman
- 10th February 2012, 17:13
Oh sh*t!!!

I had forgotten to connect a pull-up resistor to data pin, and that was the reason for non workin code. I am sorry, that I have caused trouble. The following works. Data pin is now back to GP1 (GPIO.1)





'************************************************* ***************
'* Name : 12F508thermostat.BAS *
'* Author : AN OLD FINNISHMAN *
'* Notice : *
'* : *
'* Date : 02/12/2012 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************

' CHIP: PIC 12F508
' A simple program to read DS1820 (18S20) and depending on the temperature
' to drive a led (and/or a solit state relay to drive an
' electric heater).
' ID for the sensor and a CRC value is here:
' $10,$B2,$AD,$D8,$00,$08,$00,$26
' replace it with yours
' USE INTERNAL OSCILLATOR (INTRC)

' connect a led via 470 ohm resistor to GP0
' connect a 4k7 pull-up resistor to data pin GP1


Define osc 4

led var GPIO.0


Tempa Var Word ' Temperature storage
Stat var bit

DQ Var GPIO.1 ' One-wire data pin

TRISIO =000010
GPIO =000000

'led on for a second to see if code runnig

High led
PAUSE 1000
low led

pauseus 100

Temp_1:

OWOUT DQ, 1, [$55,$10,$B2,$AD,$D8,$00,$08,$00,$26,$44]
W1:
OWIN DQ, 4, [Stat]' Check for still busy converting
IF Stat = 0 THEN W1' Still busy?, then loop
OWOUT DQ, 1,[$55,$10,$B2,$AD,$D8,$00,$08,$00,$26,$BE]
OWIN DQ, 2, [Tempa.LOWbyte,Tempa.HIGHBYTE]' Read two bytes, then end communications

' replace upper and lower limits with your own
' variable Tempa is 2 times the temperature in degrees C

If Tempa >= 41 then low led ' if temp >= 20,5 (upper limit)
If Tempa <= 40 then High led ' if temp <= 20 (lower limit)

PAUSE 1000

Goto Temp_1