Jump to content
Sign in to follow this  
Daz

Ds18B20 Woes

Recommended Posts

Hi everyone,
After a couple of years I am re-visiting my temperature conversion headache.

The PIC is an 18F2525 @ 4MHz.
dq is the I/O pin on RB0.

The code below works to the point where the value displayed on the lcd is a large non sensical number that fluctuates.
The problem I have is with the conversion formula (I have looked at and tried many versions found on the 'net').
It may be that I cannot use the 'float' command as my version of sourceboost ide doesn't support it (v6.40).

Could one of you clever people take a look at my code and suggest a way around this problem.
Many thanks in advance.



volatile bit dq @ PORTB.0;
volatile bit pin @ TRISB.0;


unsigned char presence;
unsigned char i;
int k;
unsigned short tempL, tempH, fraction,temp;
int data[12];
unsigned char reset(void);
unsigned char read_byte(void);
unsigned int signbit;


#define skip_rom 0xCC
#define convert_T 0x44
#define read_scratchpad 0xBE


void write_byte(char val);
void Read_Temperature(void);

//***********************************
unsigned char reset(void)
{
pin = 0;
dq = 0;
delay_10us(48);
pin = 1;
delay_10us(6);
presence = dq;
delay_10us(48);

return (presence);//not needed
//0 = part, 1 = no part
}
//************************************
unsigned char read_byte(void)
{
char i;
char result = 0;
pin = 1;

for(i=0;i<8;i++)
{
pin = 0;
dq = 0;
asm nop;
asm nop;
pin = 1;
if(dq!=0)
{
result |= 1<<i;
delay_10us(6);
}
}
return result;
}
//***************************
void write_byte(char val)
{
char i;

pin = 1;
for(i=0;i<8;i++)
{
if((val & (1<<i))!= 0)
{
//write 1
pin = 0;
dq = 0;
asm nop;
pin = 1;
delay_10us(6);
}
else
{
//write 0
pin = 0;
dq = 0;
delay_10us(6);
pin = 1;
}
}
}
//****************************
void Read_Temperature(void)
{
reset();
if(presence)
{
lcd_gotoxy(0,0);
lprintf("NO SENSOR ");
}
write_byte(skip_rom);
write_byte(convert_T);
delay_ms(750);

reset();
write_byte(skip_rom);
write_byte(read_scratchpad);

// Receive 9 bytes
for ( i = 0; i < 9; i++)
{
data = read_byte();
}

temp = data[0] + data[1] * 256;//here's where the problem lies!

signbit = temp & 0b100000000;//test most sig. bit
if(signbit)//negative reading
{
temp = (temp ^ 0xffff + 1);//2's comp.
}
}

void main( void )
{

//***Interrupts
intcon = 0b00000000;
intcon2 = 0b10000000;
intcon3 = 0b00000000;

delay_ms(500);
lcd_setup();

lcd_gotoxy(0,0);
lprintf("TEMPERATURE READING ");
lcd_gotoxy(0,1);
lprintf(" DS18B20 ");
delay_s(5);
//Endless loop
while( 1 )
{
delay_s(1);

Read_Temperature();

lcd_gotoxy(0,0);
lprintf("T=%d ",temp);

}//while
}//main

Share this post


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.

Guest
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.

Loading...
Sign in to follow this  

×
×
  • Create New...