Jump to content

Recommended Posts

hi all,


I'm trying to read the temperature of a DS18S20 with the dallas 1-wire protocol. I want to make the protocol myself using the datasheet so i can fully understand it. However i stumbled upon a problem that i can't resolve.


In my code i need to switch the tris of portd.6 from output to input (to detect the DS18s20) this is where my pic18f452 gets stuck. I have no idea what i'm doing wrong so if anybody can help i would be grateful.I copied the code related to this section below.

The problem is at line: dq_dir=1; //set the data pin to input //stuck here


#define dq portd.6
#define dq_dir trisd.6

void init( void ){
dq_dir=0; //set the data pin to output
dq=0; //set the data pin low to reset
delay_us(500); //wait longer then 480us for reset to complete
dq_dir=1; //set the data pin to input //stuck here

	lprintf("DS18s20 not present");
delay_us(400); //wait for end of presence pulse



Link to post
Share on other sites

What exactly do you mean "it gets stuck"? Stuck in compilation or stuck in execution?


In any case the problem is probably with the line prior to the "dq_dir=1;".

All the library delay routines in SourceBoostC take a character as the delay parameter so the maximum valid delay value you can pass to the delay_us() (or delay_anything() for that matter) is 255 or 0xFF. If you want to have a 500uSec dealy you must use "delay_10us(50);"

The same applies to the last line of code in your post, change the delay_us(400); to delay_10us(40);

Link to post
Share on other sites

Take a look at the SourceBoostC documentation (page 73). The function is declared as void delay_us(unsigned char t)

Probably in the past the exact value of the delay has not been critical so it all appeared to work. In this case the timing is critical but any value you pass to the delay() function will be truncated to 8 bits so when you pass 500 to the delay_us() function the value it actually sees is 244 which happens to be the lower 8 bits of decimal 500.

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...