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.

16F628A and DS18B20 temp sensors

Status
Not open for further replies.
Thanks for the code Pommie!

I've been reading though it and it makes sense.

I only have one question left (for now anyway). What does this line do?

Code:
movlw	1<<OwBit	;get the bit mask

Thanks in advance.

Now that I have some more free time, it's time to sit down and right some code.
I have to adapt this to run on a 4mhz 16f628A
 
Ok, so I got some code up and running that sucessfully reads the DS18B20 running on a PIC16F628A, using the internal 4mhz Oscilator. I'm running the DS18B20 in default configuration with external power supply (Vcc is 4vdc). The dataline (PORTA,1) has a 4.7k pullup resistor connected to Vcc.

If I run the Skip ROM command and then a Read Scratch Pad command, it works. It reads the default temperature of 85 degC.

But if I add in the Convert-T command, it fails.
It blows right through the section (where PORTB=3) where I wait for the dataline to come high (because the conversion should take 750msec, and hold the dataline low until it is finished)

any idea what I am doing wrong?

incase your wondering, I'm using PORTB for debugging output. It lets me know how far the program went (if it hangs).

Code:
#define databit 1
#define dataline PORTA,databit

....

call reset_presence_pulse

clrf PORTB
incf PORTB ;=1

movlw 0xCC ;Skip ROM
movwf data_out
call send_data

incf PORTB ;=2

movlw 0x44 ;Convert-T
movwf data_out
call send_data

;Set dataline to input
bsf STATUS,RP0
bsf TRISA,databit
bcf STATUS,RP0

incf PORTB ;=3

;The dataline should go high when the conversion is complete
convert_delay
btfss dataline
goto convert_delay

....
 
Last edited:
When you issue a convert command you have to wait until it completes. You can either wait 750mS or send read slots and wait for a high bit. The OwBusy routine waits for the time slots and so if you send the convert command and then call OwBusy it should work correctly.

Mike.
 
But if I add in the Convert-T command, it fails.
It blows right through the section (where PORTB=3) where I wait for the dataline to come high (because the conversion should take 750msec, and hold the dataline low until it is finished)

If you are using parasite power, you have to force the line high during a temperature conversion. You then have to wait the 750 ms.

The data line should never stay low for longer than is needed to do a reset.
 
So, I checked the output with an O-scope. The output is never going low for 750msec... not sure why yet

Diver: I'm not using Parasite power. I have external 4vdc hooked up.
 
Last edited:
The line doesn't go low. You have to issue read slots and the DS1820 will pull the line low (during the read slot) when it has finished converting. See my OwBusy routine.

Mike.
 
Thanks for all the help.

I still have not gotten it to work :(

I have tried the following: (after issuing the Convert-T command)

#1. Holding the dataline high, waiting 750msec, releasing it, and then reading the dataline
#2. Waiting for the dataline to go high (indicating that the conversion is finished)
#3. Issuing read time slots in a loop, and waiting for that dataline to go high (indicating that the conversion is finished)

So far all have failed :-(

I always either read 0x00, 0x00 (too early) or 0xFF, 0xFF (too late)... or perhaps it's not working at all and I'm just reading random events?

I'm off to test my code some more. I'll check the accuracy of my delay 750msec routine, but it should be spot on (exactly 750,000 clock cycles).
 
Sweet, I FINALLY got it to work! :)

Here is my basic process:

1. Reset/presence pulse
2. Skip ROM --> send the 8bit command
3. Convert-T --> send the 8bit command
4. Delay 750msec (I never did get the read time slot, and wait for the dataline to go high thing to work)
5. Reset/presence pulse <-- this is the part that I was missing the whole time :mad:
6. Read Scratch pad --> send the 8bit command
7. Read time slots ---> read the incomming data, store it where ever you want it to go.

If all you care about is the temperature, you only need to read the first 16 bits of data.

The rest of the data contains the upper and lower temperature alarms, the config register, and the CRC byte, and a few bytes that useless to the user (the DS18B20 uses them internally).

Figure 7 in the datasheet shows them all.

---------------------------------------------------------------------------------------------------------

So... it was the 2nd reset that I was missing all of this time :mad:. I'm still kind of surprised that resetting the device RIGHT after you tell it to refresh its internal regs actually works... but I guess that's just the way they built it.
 
Status
Not open for further replies.

Latest threads

Back
Top