CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem when using DS18B20 in parasitic power mode

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

Problem when using DS18B20 in parasitic power mode
PostPosted: Wed May 25, 2016 7:25 pm     Reply with quote

Hi every one.
I'm doing a project using DS18B20 in parasitic power mode.
I've referenced a example circuit of Maxim Inc when designing a bus for 1 wire sensor.
I'm redesigning a circuit like as Appendix A and C in a below link:
https://www.maximintegrated.com/en/app-notes/index.mvp/id/148
And this sensor I bought from here, it's only 2 wire so that I think only interface via parasitic power mode:
http://www.aliexpress.com/store/product/Freeshipping-2-wire-DS18B20-1m-Waterproof-DS18b20-temperature-sensor-probe/516972_1172485886.html
Here is my sensor circuit with 3.3V
https://drive.google.com/file/d/0Bz215_lkn4g-dV9YbExfR1dFaEk/view?usp=sharing
Because in my project using BLE module so that I chose 3.3V power supply for all circuit.
And I'm using PIC18F2520, CCS C ver 5.048.
I tried read sensor and send result to RS232 terminal but it isn't OK.
Here is the driver and program I'm using from CCS C forum:

*DS18B20 driver
Code:

#define ds18b20_data   PIN_C2
#define ds18b20_rate   PIN_C1

int1 init_ds18b20(void){
   int1 ready;
   
   output_low(ds18b20_data);
   delay_us(490);
   output_float(ds18b20_data);
   delay_us(75);
   ready=!input(ds18b20_data);
   delay_us(420);
   return ds18b20_data;
}


void write_bit_ds18b20(int8 b){
   
   output_low(ds18b20_data);
   if(b==1){
      delay_us(5);               // min 1us: time for master write 1 slot -figure16
      output_float(ds18b20_data);
   }
   delay_us(65);                  // min 60us
   output_float(ds18b20_data);
   delay_us(2);                  // min 1us between timeslo
   
   
   
}

void write_byte_ds18b20(int8 B){
   
   int8 loop, aux;
   for(loop=0;loop<8;loop++){
      aux = B>>loop;
      aux = aux&0x01;
      write_bit_ds18b20(aux);

   }
}

int1 read_bit_ds18b20(void){
   output_low(ds18b20_data);
   delay_us(3);      // min 1us
   output_float(ds18b20_data);
   delay_us(5);
   return(input(ds18b20_data));   
}

int8 read_byte_ds18b20(void){
   int8 j=0, result=0x00;
   for(j=0;j<8;j++){
      if(read_bit_ds18b20()){
         result |= (0x01<<j);
         delay_us(65);
      }
   }
   return result;
}

void get_temperature(void){
   unsigned int loop;
   unsigned int temp_data[9];
   signed int16 temp=0x0000;
   float temp_disp;
   
   if(init_ds18b20())
   {
      write_byte_ds18b20(0xCC);   // skip ROM
      write_byte_ds18b20(0x44);   // convert AD
      output_float(ds18b20_data);
      output_high(ds18b20_rate);
      delay_ms(800);
     
      output_low(ds18b20_rate);
      init_ds18b20();
      write_byte_ds18b20(0xCC);
      write_byte_ds18b20(0xBE);
     
      for(loop=0; loop<2 ; loop++){
         temp_data[loop]= read_byte_ds18b20();
         printf("\r\n byte read: %x", temp_data[loop]);
      }
      temp=make16(temp_data[1], temp_data[0]);
     
      temp_disp= (temp>>4) & 0x00FF;
     
      if(temp&0x0001) temp_disp=temp_disp+0.0625;
      if(temp&0x0002) temp_disp=temp_disp+0.125;
      if(temp&0x0004) temp_disp=temp_disp+0.25;
      if(temp&0x0008) temp_disp=temp_disp+0.5;
     
      printf("\r\n Temperature data : %7.3f", temp_disp);
     
   }
   
   else printf("\r\n Init DS18B20 failure !!!");
}


* Main
Code:

                                                                                                                                         
#include <18F2520.h>
#fuses   HS
#fuses   PUT
#fuses   NOFCMEN
#fuses   NOIESO
#fuses   NOPBADEN
#fuses   MCLR
#fuses   NOSTVREN
#fuses   NOXINST
#fuses   NODEBUG
#fuses   PROTECT
#fuses   NOLVP
#fuses   NOWDT
#fuses   NOBROWNOUT
#fuses   NOXINST
#use     delay(clock=16000000)
//!#use     SPI(MASTER, DI=PIN_C5, DO=PIN_C4, CLK=PIN_C2, BAUD=1000000, MODE=0, BITS=8, MSB_FIRST, STREAM=STREAM_SPI)
#use     RS232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, errors, parity=n, bits=8, stop=1) // HARD UART

#include "74595_role.c"
#include "74595_lcd_ex.c"
#include "DS18B20_ntc.c"

void main(void)
{
      Delay_ms(1000);
      set_tris_b(0);
     
      output_toggle(PIN_B2);
      delay_ms(200);

      output_float(ds18b20_data);
      output_high(ds18b20_rate);
    
      delay_ms(1000);       
      printf("\n\r Begin Test Demo Kit IOT");
      while(1)
      {
   
         get_temperature();
         output_toggle(PIN_B2);
         delay_ms(2000);
      }
}

_________________
Begin Begin Begin !!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 25, 2016 10:10 pm     Reply with quote

Quote:
so that I chose 3.3V power supply for all circuit.
And I'm using PIC18F2520,

The lowest approved voltage for 18F2520 is 4.2 volts. See Figure 26-1
on page 324 of the PIC data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf
The LF version will work at 3.3 volts. Which do you have, F or LF ?

This code was tested in Parasite mode at 4 MHz and it worked.
http://www.ccsinfo.com/forum/viewtopic.php?t=40106&start=28
I didn't use your complicated circuit. As I recall it was just a DQ line
and a ground. I think I used a 4.7K pullup to +5v on the DQ line.
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Wed May 25, 2016 11:48 pm     Reply with quote

for parasitic mode I always use 2k2 resistor, and it works fine.
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu May 26, 2016 12:41 am     Reply with quote

PCM programmer wrote:
Quote:
so that I chose 3.3V power supply for all circuit.
And I'm using PIC18F2520,

The lowest approved voltage for 18F2520 is 4.2 volts. See Figure 26-1
on page 324 of the PIC data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf
The LF version will work at 3.3 volts. Which do you have, F or LF ?

This code was tested in Parasite mode at 4 MHz and it worked.
http://www.ccsinfo.com/forum/viewtopic.php?t=40106&start=28
I didn't use your complicated circuit. As I recall it was just a DQ line
and a ground. I think I used a 4.7K pullup to +5v on the DQ line.


Thanks for your helping.
Sorry when I careless when write a fail name of PIC, it's PIC18F25K20.
About driver circuit using FET from Maxim Inc, it's a power supply line for DS18B20 sensor in parasitic mode, and I dont known you're using another circuit to driver this line?
Can you share me your solution to expand distance of DS18B20?
Thanks you.
_________________
Begin Begin Begin !!!
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu May 26, 2016 12:46 am     Reply with quote

40inD wrote:
for parasitic mode I always use 2k2 resistor, and it works fine.

Thanks for your helping.
Are you doing with 5V in the circuit?
I think a small value of pullup resistor make a rise time is faster, also it have a little power dissipation.
Are you using this sensor in parasitic mode ? and can you share me a drive circuit ?
Thanks you.
_________________
Begin Begin Begin !!!
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Thu May 26, 2016 1:01 am     Reply with quote

I do not use any special circuit for powering DS18B20. I usually drive it from same DC source as the rest of circuit (the same 7805, for example), using 2.2 k pullup.
I have sensor working in such connection for 9 years in my basement through flat 2-core telephone wire 6 m long.
If you have troubles while reading sensor, then check wiring for poor connection. Then check timings in program. Is the selected crystal in program at same frequency as in real scheme?
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu May 26, 2016 5:07 am     Reply with quote

I'm with PCM P about the PIC's voltage requirements ! The datasheet does show a chart that 4.2 is the LOWEST Vdd can be!! IF you MUST run 3 volts, use the 'L' version of the PIC or choose a an 'F' PIC that WILL run at 3 volts ( some do...most don't). It is very important to read the electrical spec section as it tells you what SPEED vs Voltage a PIC will work at.
All my projects just use a 2k2r to VDD. I had to read the Maxim link to see what 'rate' was for, as I've never used it.
Since you are sending data to a PC ,use a 5 volt power source. Confirm wiht a '1Hz LED' program the PIC is running at the correct frequency. This is very important !! The PIC is 'bitbanging' the DSA communications so timing is critical, confirm with oscilloscope if you have one. It'll also show you the waveform, you need good clean square edges.
I'd get rid of all the 'extra' parts and just use a 2k2r on DQ. 'Slew rate' control would only be necessary for 'electrically' long lines,say 30m of 2c 'bell wire'.
You've got 3 'drivers' in 'main'...unless you ARE using them, delete and work 100% on the DS portion of code.
Also it's bad PIC form to have
#fuse PROTECT
You ONLY need that for the final programming before the PIC gets sold to a customer.

edit: I compared your driver timings to mine and they are NOT the same ! Now I know mine works,has for a few years, so I can only conclude that yours are not correct. It is critcal that the 'bit' timing of the DS device follow what's in the datasheet. even if mine are 'slightly' off in one case you have 65us and I have 120us. That is a huge difference. I suggest you reread the DS datasheet, 'play computer' and check every delay(), this needs to be accurate.


Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group