Jump to content
Sica

One Wire Communication Protocol Trouble

Recommended Posts

Hello,

 

I try to read a DS18S20 sensor with one wire protocol implemented in boostC. It seams that the communication work(I suppose) but I can not understand results. Any idea will be useful.

 

I use an 16F1934 MCU @ 32 MHz. In my project I included following libs : oo.pic16x.lob and lib.pic16x.lib, also I included oo.h file.

 

Code Sample :

 

//try one wire

char x = oo_busreset();

if (x==0)

{

tx_232("\nBus Reset result is 0 \n");

}

else

{

tx_232("\nBurst reset result is not 0\n");

}

oo_start_conversion();

tx_232("Conversion Started...\n");

while(oo_conversion_busy());

tx_232("Conversion Ended...\n");

if (oo_read_scratchpad())

{

tx_232("Scratchpad Readed failure...\n");

}

else

{

tx_232("Scratchpad read success .. \n");

}

short data[9];

for (int k = 0;k<9;k++)

{

data[k]= oo_get_data();

}

 

for (int q =0;q<9;q++)

{

tx_232_1(data[q]);

tx_232("\n");

 

}

 

 

 

Results :

 

Bus Reset result is 0

Conversion Started...

Conversion Ended...

Scratchpad read success ..

\0xba

\0xba

\0xba

\0xba

\0xba

\0xba

\0xba

\0xba

\0xba

 

 

 

also 0xba value is changing when I put my finger on the sensor and heat it.The resistor that pull up the line is 3.2K.

 

 

Thank you in advance.

Marius

Share this post


Link to post
Share on other sites

Hi Marius,

 

Have you tried following the exact sequence shown on page 19 of the DS18S20 manual.

 

It seems you have to send another reset sequence before reading the scratchpad data.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Hi Marius,

 

I think the problem lies in the OO library.

 

When reading a bit from the device the code waits 20us after releasing the bus which is outside the spec maximum of 15us. Also the time between reading bits is alot shorter than the minimum 60us per bit.

 

I think you would be better off writing your own code to handle this device.

 

I experimented with an older DS1820 and PIC16F687 and after some tweaking of the OO library source code got a good temperature reading.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

I have used this before and it works fine, cannot remember where it came from

 

#include "a6276.h"
unsigned char ten,one;
unsigned char romcode[8];
void main(void)
{
unsigned char x,digit1,digit2;
trisb=0b00110000;
trisc=0;
sspstat=0b01000000;   //set up SPI
sspcon1=0b00000010;
sspcon1.SSPEN=1;
latch=1;
outenable=0;
delay_ms(255);

while(1){
 readtemp();
 digit2=alpha1[one];
 digit1=alpha1[ten];
 spi_out(digit2,digit1);
 delay_ms(255);
}
}
unsigned char owreset(void)
{
DQ=0;	 //pull DQ low
delay_10us(50);	//for minimum 480uS
DQ=1;	 //then release bus and set DQ to input
delay_us(70);	//and wait 70uS
if(!portb.0){	//if presence pulse set
 delay_10us(43);
 return(0);   //1-wire device successfully found
}
else{
 delay_10us(43);
 return(1);   //no 1-wire device found
}
}
void owwritebit(bit thebit)
{
if(thebit){
 DQ=0;
 delay_us(6);
 DQ=1;
 delay_us(64);
}
else{
 DQ=0;
 delay_us(60);
 DQ=1;
 delay_us(10);
}
}
unsigned char owreadbit(void)
{
unsigned char result;
DQ=0;
delay_us(6);
DQ=1;
delay_us(9);
result=portb.0;
delay_us(55);
return result;
}
void owwritebyte(unsigned char byte)
{
unsigned char x;
for(x=0;x<8;x++){
 owwritebit(byte.0);
 byte>>=1;
}
}
unsigned char owreadbyte(void)
{
unsigned char x,result=0;
for(x=0;x<8;x++){
 result>>=1;
 if(owreadbit())
  result|=0x80;
}
return result;
}
void readtemp(void)
{
unsigned char store[10];
unsigned char x,msb,lsb;
owreset();
owwritebyte(0xcc);   //skip ROM
owwritebyte(0x44);   //start conversion
delay_ms(100);
owreset();
owwritebyte(0xcc);   //skip ROM
owwritebyte(0xbe);   //read scratchpad
for(x=0;x<9;x++){
 store[x]=owreadbyte();
}
msb=store[1];lsb=store[0];
msb=msb<<4;lsb=lsb>>4;
x=0;
x=x|msb|lsb;
fix(x);
}
void fix(unsigned char dec2)
{
unsigned char count=0;
while(dec2 >= 10){
 dec2=dec2-10;
 count=count+1;
}
ten=count;
one=dec2;
}
void spi_out(unsigned char ten,unsigned char one)
{
unsigned char junk;
sspbuf=ten;
while(!sspstat.BF);
junk=sspbuf;
sspbuf=one;
while(!sspstat.BF);
junk=sspbuf;
}

 

 

#include <system.h>
#include <oo.h>
#pragma CLOCK_FREQ 20000000
// #pragma DATA	_CONFIG1H, _HS_OSC_1H & _OSCS_ON_1H
// #pragma DATA	_CONFIG2H, _WDT_OFF_2H
// #pragma DATA	_CONFIG4L, _LVP_OFF_4L
void spi_out(unsigned char,unsigned char);
void fix(unsigned char);
unsigned char owreset(void);
void owwritebit(bit);
unsigned char owreadbit(void);
void owwritebyte(unsigned char);
unsigned char owreadbyte(void);
void readtemp(void);
void fix1(unsigned char);
unsigned char owreset1(void);
void owwritebit1(bit);
unsigned char owreadbit1(void);
void owwritebyte1(unsigned char);
unsigned char owreadbyte1(void);
void readtemp1(void);
#define latch  latc.0
#define outenable latc.2
#define DQ   latb.7
#define DQ1   latb.6
unsigned char alpha1[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
  0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0x79,0x38,0x73};

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
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...

×