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

DS18S20 & DS18B20 Codes
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
xokolatecake



Joined: 09 Mar 2007
Posts: 14

View user's profile Send private message

PostPosted: Wed Jun 04, 2008 10:40 am     Reply with quote

Sorry guys!..i was so mad with the time i wasted on this i didn't noticed that i was using a DS18B20P, the (only) parasitic power version of DS18B20!
So my problem was not in the software but on the hardware, although i made a few adjustments to the delays duration and added the strong pull up control maxim recomends in the datasheet!

Will post the code for future reference. Again this is meant for DS18B20-PAR, don't make the same mistake i did!;P

Code:

#include <18F4525.h>
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, stream=PC)


//ds18b20 pin
#define DS18B20_IO PIN_B0
#define DS18B20_PU PIN_B1

int1 ds18b20_initialization(void){
   int1 ready;
   
   output_low(DS18B20_IO);
   delay_us(490);          //min 480us
   output_float(DS18B20_IO);
   delay_us(75);           //15 - 60us for device to respond
   ready=!input(DS18B20_IO);
   delay_us(415);
   return ready;
}

void ds18b20_write_bit(int8 b){
   output_low(DS18B20_IO);
   if(b == 1){
      delay_us(5);         //min 1us
      output_float(DS18B20_IO);
   }
   delay_us(65);
   output_float(DS18B20_IO);
   delay_us(2);            //min 1us between write slots
}

void ds18b20_write_byte(int8 B){
   int8 i, aux;
   
   for(i=0; i<8; i++){
      aux = B >> i;     //aux equals B shifted i times to the right
      aux &= 0x01;      //least significant bit survives
      ds18b20_write_bit(aux);
   }
   //delay_us(120);
}

int1 ds18b20_read_bit(void){
   output_low(DS18B20_IO);
   delay_us(3);         //min 1us
   output_float(DS18B20_IO);
   delay_us(5);
   return(input(DS18B20_IO)); //next a delay of 60 to 120us must be done!
}

int8 ds18b20_read_byte(void){
   int8 i, result=0x00;
   
   for(i=0; i<8; i++){
      if(ds18b20_read_bit())
         result |= (0x01 << i);
      delay_us(65);
   }
   return result;
}

void main() {
 
   int8 i;
   int8 scratchpad_data[9];
   signed int16 temp=0x0000;
   float temperature;
   int8 busy =0;
   
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(False);
   port_b_pullups(FALSE); 
   
   output_float(DS18B20_IO);
   output_low(DS18B20_PU);
   delay_ms(1000);
   printf("DS18B20 1-wire temperature sensor\r\n");
 
   while(1){
     
     
      if(ds18b20_initialization()){
      ds18b20_write_byte(0xCC);  //skip ROM command
      ds18b20_write_byte(0x44);  //convert temperature command
      output_float(DS18B20_IO);
      output_high(DS18B20_PU);     
      delay_ms(800);             //max conversion time for 12bit resolution=750ms
      /*while (busy == 0){
         printf("waiting...\r\n");
         busy = ds18b20_read_byte();
      }
      busy=0;*/
      output_low(DS18B20_PU);
      ds18b20_initialization();
      ds18b20_write_byte(0xCC);  //skip ROM command
      ds18b20_write_byte(0xBE);  //read scratch pad command
     
      for(i=0; i<2; i++){
         scratchpad_data[i]=ds18b20_read_byte();
         printf("scratchpad_data[%d]=%X\r\n", i, scratchpad_data[i]);
      }
      temp=make16(scratchpad_data[1], scratchpad_data[0]);
      //temperature = temperature/16;
     
      //calculate the whole number part
      temperature = (temp >> 4) & 0x00FF;
      //calculate the fractional part
      if(temp & 0x0001) temperature = temperature + 0.06250;
      if(temp & 0x0002) temperature = temperature + 0.12500;
      if(temp & 0x0004) temperature = temperature + 0.25000;
      if(temp & 0x0008) temperature = temperature + 0.50000;
     
      printf("temperature reading: %7.3fdegrees C (%X%X)\r\n", temperature, scratchpad_data[1], scratchpad_data[0]);
      }
      else{
         printf("DS18B20 failed to initialize\r\n");
      }
      delay_ms(5000);
   }
}



See ya!:D
azakuri



Joined: 02 Aug 2008
Posts: 4

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Sep 02, 2008 6:40 am     Reply with quote

i'm still wonder if i use 2 ds18s20 and pic16f877, do i need to make a major changes in this coding......
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Wed Oct 01, 2008 6:05 pm     Reply with quote

If I wanted to convert this to Fahrenheit I'd need to convert the "whole number part"

Code:
//calculate the whole number part
      temperature = (temp >> 4) & 0x00FF;
//convert to Fahrenheit
temperature=((temperature*9)/5)+32;


I believe that is right above

The part I've stuck on is getting the fractional part converted

I'm tempted to go this route but I'm unsure if it correct or proper coding.

Code:
//calculate the fractional part
      if((((temp*9)/5)+32) & 0x0001) temperature = temperature + 0.06250;
      if((((temp*9)/5)+32) & 0x0002) temperature = temperature + 0.12500;
      if((((temp*9)/5)+32) & 0x0004) temperature = temperature + 0.25000;
      if((((temp*9)/5)+32) & 0x0008) temperature = temperature + 0.50000;
HTAluvBeBeo



Joined: 23 Feb 2008
Posts: 35

View user's profile Send private message

PostPosted: Tue Nov 11, 2008 8:49 am     Reply with quote

Can I use a normal BJT and pullup resistor instead of a MOSFET in
parasite power mode ? Thank you all for reply my noob question.
HTAluvBeBeo



Joined: 23 Feb 2008
Posts: 35

View user's profile Send private message

PostPosted: Thu Nov 13, 2008 6:26 am     Reply with quote

xokolatecake wrote:
Sorry guys!..i was so mad with the time i wasted on this i didn't noticed that i was using a DS18B20P, the (only) parasitic power version of DS18B20!
So my problem was not in the software but on the hardware, although i made a few adjustments to the delays duration and added the strong pull up control maxim recomends in the datasheet!

Will post the code for future reference. Again this is meant for DS18B20-PAR, don't make the same mistake i did!;P




See ya!:D


Would you like to help me about the hardware for ds18b20p. A simple schematic will help me much. I am bad at electronics. Which MOSFET should I use?

Thank you all!
soulraven



Joined: 08 Feb 2009
Posts: 72
Location: campulung muscel

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Sat Feb 14, 2009 12:13 pm     Reply with quote

I tried the code below, go, only 2 questions that I
1. Below 0 degrees Celsius shows error displays 250 degrees Celsius
2. How can I adjust for sensors 3-4

xokolatecake wrote:
Hi!
Regarding scanan post, what does this little piece do?:

Code:
//for 10 bit resolution mod
onewire_write(0xCC);
onewire_write(0x4E);

onewire_write(125);
onewire_write(-55); //this should be done for proper working of DS18B20
onewire_write(127);

onewire_reset();
onewire_write(0xCC);
onewire_write(0x48);
delay_ms(15);


He issues a write scratchpad command, then sends the 3bytes and then copies the scratchpad to eeprom...what for?

And why it should be done for proper DS18B20 work?


I'm having trouble using a DS18B20, all it gives me is the power up state temperature reading: 0x0550->85ºdegrees. Any ideas of what the problem may be?
Code:

#include <18F4525.h>
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, stream=PC)


//ds18b20 pin
#define DS18B20_IO PIN_A0


int1 ds18b20_initialization(void){
   int1 ready;
   
   output_low(DS18B20_IO);
   delay_us(488);          //min 480us
   output_float(DS18B20_IO);
   delay_us(72);           //15 - 60us for device to respond
   ready=!input(DS18B20_IO);
   delay_us(424);
   return ready;
}

void ds18b20_write_bit(int8 b){
   output_low(DS18B20_IO);
   if(b == 1){
      delay_us(2);         //min 1
      output_float(DS18B20_IO);
   }
   delay_us(125);
   output_float(DS18B20_IO);
}

void ds18b20_write_byte(int8 B){
   int8 i, aux;
   
   for(i=0; i<8; i++){
      aux = B >> i;     //aux equals B shifted i times to the right
      aux &= 0x01;      //least significant bit survives
      ds18b20_write_bit(aux);
   }
   delay_us(120);
}

int1 ds18b20_read_bit(void){
   output_low(DS18B20_IO);
   delay_us(2);         //min 1us
   output_float(DS18B20_IO);
   delay_us(8);
   return(input(DS18B20_IO)); //next a delay of 60 to 120us must be done!
}

int8 ds18b20_read_byte(void){
   int8 i, result=0x00;
   
   for(i=0; i<8; i++){
      if(ds18b20_read_bit())
         result |= (0x01 << i);
      delay_us(125);
   }
   return result;
}

void main() {
 
   int8 i;
   int8 scratchpad_data[9];
   signed int16 temp=0x0000;
   float temperature;
   int8 busy =0;
   
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(False);
   port_b_pullups(FALSE); 
   
   output_float(DS18B20_IO);
   delay_ms(1000);
   printf("DS18B20 1-wire temperature sensor\r\n");
 
   while(1){
     
      for(i=0; i<9; i++)
         scratchpad_data[i]=0x00;
     
     
      ds18b20_write_byte(0xCC);
      ds18b20_write_byte(0x4E);

      ds18b20_write_byte(125);
      ds18b20_write_byte(-55); //this should be done for proper working of DS18B20
      ds18b20_write_byte(127);

      ds18b20_initialization();
      ds18b20_write_byte(0xCC);
      ds18b20_write_byte(0x48);
      delay_ms(15);

     
      if(ds18b20_initialization()){
      ds18b20_write_byte(0xCC);  //skip ROM command
      ds18b20_write_byte(0x44);  //convert temperature command
      //output_float(DS18B20_IO);
      delay_ms(800);             //max conversion time for 12bit resolution=750ms
      /*while (busy == 0){
         printf("waiting...\r\n");
         busy = ds18b20_read_byte();
      }
      busy=0;*/
      ds18b20_initialization();
      ds18b20_write_byte(0xCC);  //skip ROM command
      ds18b20_write_byte(0xBE);  //read scratch pad command
     
      for(i=0; i<2; i++){
         scratchpad_data[i]=ds18b20_read_byte();
         printf("scratchpad_data[%d]=%X\r\n", i, scratchpad_data[i]);
      }
      temp=make16(scratchpad_data[1], scratchpad_data[0]);
      //temperature = temperature/16;
     
      //calculate the whole number part
      temperature = (temp >> 4) & 0x00FF;
      //calculate the fractional part
      if(temp & 0x0001) temperature = temperature + 0.06250;
      if(temp & 0x0002) temperature = temperature + 0.12500;
      if(temp & 0x0004) temperature = temperature + 0.25000;
      if(temp & 0x0008) temperature = temperature + 0.50000;
     
      printf("temperature reading: %7.3fdegrees C (%X%X)\r\n", temperature, scratchpad_data[1], scratchpad_data[0]);
      }
      else{
         printf("DS18B20 failed to initialize\r\n");
      }
      delay_ms(5000);
   }
}





Thanks*
Alioshag



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

Pls Help!!
PostPosted: Mon Feb 23, 2009 3:32 am     Reply with quote

I used the code posted by scanan and all what I get on the serial port
is "Temperature =10.6"
I'm using a PIC16F628A and a DS18S20P.
is this DS18S20P compatible with DS18S20 or DS18B20?
pls answer to me.
Alioshag



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

DS18S20 and DS18S20P
PostPosted: Mon Feb 23, 2009 7:34 pm     Reply with quote

are these two sensors incompatible?
I get only one constant temperature even if I heat the sensor
Sad
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Feb 25, 2009 2:24 am     Reply with quote

DS18S20P - derives power from dataline (parasite power), and is not need power supply, pin 3 is NC. Did you used this sensor in parasite mode or not?

For more info see:
http://datasheets.maxim-ic.com/en/ds/DS18S20-PAR.pdf
http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf
Alioshag



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

PostPosted: Sun Mar 15, 2009 5:26 pm     Reply with quote

I used this sensor in normal mode, I used all the 3 wires and I used a 10K pull up resistor; that's all I've made.
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Mon Mar 16, 2009 2:01 am     Reply with quote

if you use DS18S20P, you shouldn't use all 3 pins.
Alioshag



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

PostPosted: Mon Mar 16, 2009 9:24 am     Reply with quote

I saw many people on internet having problem with this DS18S20P sensor, and they all didn't succeed to solve their problem.
The DS18S20P sensor is not compatible with the DS1820 or DS18B20. That's all what I found about this difficult to use sensor.
Is there anyone who managed to use this sensor (DS18S20P)?
If it's so, pls post some schematics or code, or both. Very Happy .
Thanks a lot.


Last edited by Alioshag on Mon Mar 16, 2009 10:01 am; edited 1 time in total
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Mon Mar 16, 2009 9:27 am     Reply with quote

Why didn't you use DS18B20 ? It has almost the same price.
Alioshag



Joined: 23 Feb 2009
Posts: 5

View user's profile Send private message

PostPosted: Mon Mar 16, 2009 11:20 am     Reply with quote

I got it as a free sample.
Borke



Joined: 31 Mar 2009
Posts: 1
Location: Novi Sad

View user's profile Send private message

PostPosted: Tue Mar 31, 2009 7:44 am     Reply with quote

I'm having problems with output_high(), output_low(), output_bit(), output_float(), shift_right().... and delay functions. Compiler is not recognizing this functions. I get "undefined symbols" error. I use PIC12f629 and I 've included its .h file. Compiler is HI-TECH STD and I use MPLAB. Where can I find their definitions, or how can I cope with that? Can somebody help me?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4  Next
Page 2 of 4

 
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