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

Trouble getting a DS18B20 to work in parasite power mode...
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 14, 2010 1:22 pm     Reply with quote

Here's the complete test program for the ds18B20 in parasite-power
mode. This is the original poster's program, with the modifications
added, that I recommended in a post above. I'm posting the integrated
program because of a request.
Code:

#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define DQ   PIN_C2   

//-------------------------------------
void ow_reset(void)
{
output_low(DQ);
delay_us(500);          // Min. 480uS
output_float(DQ);
delay_us(500);          // Wait for end of timeslot
}

//-------------------------------------
// Read bit on one wire bus

int8 read_bit(void)
{
output_low(DQ);
delay_us(1);         
output_float(DQ);
delay_us(12);        // Read within 15uS from start of time slot
return(input(DQ));   
}                       

//-------------------------------------
int8 read_byte(void)
{
int8 i;
int8 val = 0;

for(i=0 ; i<8 ; i++)
   {
    if(read_bit()) val |= (0x01 << i);
       delay_us(120);  // To finish time slot
   }

return val;
}

//-------------------------------------
void write_byte(int8 val, int8 power_on)
{
int i;

for(i=0; i<8; i++)
   {
    output_low(DQ);
    delay_us( 2 ); 
    output_bit(DQ, shift_right(&val,1,0)); 

    delay_us(60);
 
    if((i == 7) && (power_on == 1))
      {
       output_high(DQ);
      }
    else
      {
       output_float(DQ);
       delay_us( 2 ); 
      }
   }

}

//==========================================
void main(void)
{
int8 i;
signed int16 temperature;
int8 scratch[9];       
   
output_float(DQ);     
   
while(1)
  {
   ow_reset();
   write_byte(0xCC, 0); // Skip Rom command
   write_byte(0x44, 1); // Temperature Convert command

   delay_ms(750);    // Max. time for conversion is 750mS
   
   ow_reset();
   write_byte(0xCC, 0); // Skip Rom command
   write_byte(0xBE, 0); // Read scratch pad command
   
   // Get the data bytes
   for(i=0; i < 8; i++)
      {
       scratch[i] = read_byte(); 
      }
   
   ow_reset();
     
   temperature = (signed int16) make16(scratch[1],scratch[0]);
         
   if(temperature >= 0)
      temperature = (temperature + 8)/16;
   else
      temperature = (temperature - 8)/16;
           
   printf("Temp: %4Ld C \n\r",temperature);
  }

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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