Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading and writing to DS 1820...not working...need help...

Status
Not open for further replies.

seemanta

Member level 4
Joined
Jun 10, 2006
Messages
71
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,288
Activity points
1,968
Hi,
I am using the following routines for reading/writing from/to the DS1820 temperature sensor.

I am able to send the reset pulse and also able to get the presence pulse.
However, when sending the read scratchpad command, 0BEh, it is not working, because as per the datasheet, I am supposed to get the value as 50h. I am only getting FFh which is not correct.

I am posting the code, I would be grateful if anyone can look at it and let me know what is the mistake I am making.

SEND_BYTE and GET_BYTE are the main routines to write/read a single byte from the DS1820. Remaining are just helper routines. Also, for the moment, I am omitting the DELAY routines. Basically, I have used the delay routine for generating the bus timings.

And yes, almost forgot to mention, i/o line P2.5 is the one connected to the DQ line of the DS1820.

Code:
SEND_BYTE:         ;DATA TO SEND IS IN A
        MOV R0,#8
_START: JB ACC.0, SEND_1
        LCALL WRITE_0
        JMP FWD
SEND_1: LCALL WRITE_1
FWD:    RR A
        DJNZ R0,_START
        RET

GET_BYTE:           ;DATA READ WILL BE RETURNED IN A
        MOV B,#00H
        MOV R0,#3
        MOV R1,#3
_START2:
        LCALL READ_DATA
        ANL A,#20H
BACK:   RL A
        DJNZ R0,BACK
        ORL B,A
        INC R1
        MOV A,R1
        MOV R0,A  ;R1 AND R0 ARE SAME NOW, READY FOR NEXT CYCLE.
        XRL A,#11
        JNZ _START2
        MOV A,B
        RET


WRITE_0:
        CLR P2.5
        LCALL DELAY_65US
        SETB P2.5
        RET 

WRITE_1:
        CLR P2.5
        LCALL DELAY_12US
        SETB P2.5
        RET

READ_DATA:
        CLR P2.5
        NOP          ;FOR ABOUT 1US DELAY(1.085 US, TO BE PRECISE)
        NOP
        SETB P2.5    ;BUS RELEASED NOW
        NOP
        NOP
        MOV A,P2     ;READ THE DATA
        SETB P2.5    ;SET BUS TO HIGH STATE AGAIN
        RET


As always, thanks in advance for all the help I receive from this board!

regards,
Seemanta
 

It is kind of hard to see what your problem is because you did not include the code that configures the data direction register. It is also not obvious to me whether this is PIC code or AVR code or something else.

In general, I think your problem is that you are not using port P2.5 as an open drain output. To run 1-wire code from a microcontroller, the I/O port must be configured as open drain. This allows either the microcontroller OR the 1-wire to pull the pin low. The pin is externally pulled high via a 4.7K ohm resistor which provides the logic "1" state. I think you are running a full digital I/O and the microcontroller is forcing the line high which is why you always read "FF".

----- Steve
 

Re: Reading and writing to DS 1820...not working...need help

Hi Steve,
Sorry about that. I am using AT89S52 micro. Also I do have a 4.7k resistance as a pull up on the DQ bus line.

Also, I thought that only port p0 needed external pull up. Port p2 already has internal pull ups. So does that mean I do not have to connect an 'extra' pull up for the DQ line?


In general, do you see any issue with the code?

regards,
Seemanta
 

Re: Reading and writing to DS 1820...not working...need help

I believe you are reading the data too early: you should read it close to 15 us after the falling edge. Actually, about 12-13us would be fine. So insert a few more nop's there in the READ_DATA routine or use some delay code.

Second, the read/write slot is supposed to be 60us at least (actually 61us), but when you read the byte you call READ_DATA very fast. Inser some delay there, to make sure READ_DATA gets called once every 61us.
By the same token, when you write a 1 to the 1820 you need to maintain the slot at 61us, so insert some delay in the WRITE_1 code, such that you only begin writing the next bit at least 61us later (so you would need something like 50us delay inserted in WRITE_1, AFTER you let the bus float).

Third, I do not see the rest of the code, but I hope you are sending a ROM command first, even if it's just SKIP ROM.
 

Hi Seemanta,

I am not very familar with the AT89S52. However, most all microcontrollers that I have used require a write to an internal register to switch from output mode to input mode. I do not see that write in your code.
I would use a scope to monitor the 1-wire line. Then use an unused microcontroller pin to trigger the scope. Write this pin high upon entering the read routine and look at the scope. If it is a I/O direction issue, then the line will not ever go to a true logic low.
 

Re: Reading and writing to DS 1820...not working...need help

I will try to answer to those not familiar with the 8051: there is no data direction register in the traditional 8051 core.
The ports are open-drain with internal pullups (with some exceptions, which I am not going to discuss here).

The port pins can be read at any time. Since the outputs are open-drain, you only need to make sure you drive the port "high" to make sure it works as an input. If it is pulled low by any external device, it will stay low and you will read a low. If the external device drives it high, no problem, the pullup was pulling it high anyway and you will read a high.
 

Re: Reading and writing to DS 1820...not working...need help

Hello,

you're only posting your low-level 1-wire protocol, but the upper level may be wrong as well. Do you preceed the read scratchpad by skip rom command? Did you get any answer if you try read rom?

Regards,
Frank

P.S.: The bit timing seems to be within allowed limits, the byte read and write routines are apparently operational. I guess, you're also using a sufficiant pullup resistor at data line.
 

Hi all,
Thanks a lot for your response!
I just realised that my code works! Yeah! I am able to get the temperature reading now with the code posted in my first post, however after adding the skip ROM routine.

The reason I thought it was not working was that I was simply trying to read the powerup value of the temperature register.

As per the data sheet, the powerup value is +85*C.
Now I was not getting that, and as a result I made that post. Which brings me to my next query.

With the current code plus the skip ROM modification, even though I am able to get the temperature, I am still not able to get the powerup value of +85.

Any ideas, why?

Thanks in advance!

regards,
Seemanta
 

Re: Reading and writing to DS 1820...not working...need help

That sounds like the code may not be working properly. Please see my replies as to the possible causes.
 

Re: Reading and writing to DS 1820...not working...need help

Hello,

did you notice that a regular 1-wire bus reset isn't a power-on reset for the DS1820. To see the 85°C power-on reset default, you have to apply a considerable longer reset pulse (in case of parasitic power 1-wire operation) respectively to power down VDD.

Otherwise, I see no reason for the DS1820 to perform an unintended convert command. However, even if this would be the case, the application seems to be basically operating, which is most important, I think.

Regards,
Frank
 

Re: Reading and writing to DS 1820...not working...need help

Whenever you first power up the circuit it counts as a power-on reset. Therefore, if you do not issue a Convert command, the value you should read is 85C. So you should read 0xAA. If you don't, then something is wrong or you are not reading it right. My suspicion continues to be the timing, as I said before.

Of course the most important thing is that the application works, but how do you know it works correctly if you cannot read the expected value? This is a good test: read the value right after powerup, make sure it is 85C, then issue a convert command and then read the temperature again. It should now be close to the room temperature, so you should read something like 0x2A or thereabouts.
 

Re: Reading and writing to DS 1820...not working...need help

Exactly! That's my point! If I am not able to read the power up value as 85, then I am also not able to trust the conversion either :(

Let me give it another shot and I shall post my findings here.
The temperature seems to be near to room temperature though, around 28-29 degrees Celsius. I am in Hyderabad, India and that is the usual temperature around this time of the year.

regards,
Seemanta
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top