snooer
 
 
  Joined: 22 Apr 2009 Posts: 14
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| DS18B20 and pic16f917 | 
			 
			
				 Posted: Wed May 27, 2009 4:17 am     | 
				     | 
			 
			
				
  | 
			 
			
				I have a problem with reading data from it. And just wondering is't code problem ore circuit. I used normal power mode for ds18b20 and allways getting 0 value from it. i used 4k7 pullup rsistor as writen and shown in datasheet. 
 
so is't posible to use 16f917 pic with ds18b20?
 
 
my code is...
 
 	  | Code: | 	 		  #include <16f917.h>
 
#device *=16
 
 
 
#FUSES NOWDT                    //No Watch Dog Timer
 
#FUSES HS                       //High speed Osc (> 4mhz)
 
#FUSES NOPUT                    //No Power Up Timer
 
#FUSES NOPROTECT                //Code not protected from reading
 
#FUSES MCLR                     //Master Clear pin enabled
 
#FUSES NOCPD                    //No EE protection
 
#FUSES BROWNOUT                 //Reset when brownout detected
 
#FUSES IESO                     //Internal External Switch Over mode enabled
 
#FUSES FCMEN                    //Fail-safe clock monitor enabled
 
#FUSES NODEBUG                  //No Debug mode for ICD
 
 
#use delay(clock=20000000)
 
#include "1wire.c" 
 
#include "C:\Users\Mantas\Univeras\Bakalauras\PCC\flex_lcd.c" 
 
 
int8 tH,tL,Conf; 
 
 
float ds1820_read() 
 
{ 
 
int8 busy=0, temp1, temp2; 
 
signed int16 temp3; 
 
float result; 
 
 
onewire_reset(); 
 
onewire_write(0xCC); 
 
onewire_write(0x44); 
 
delay_ms(200); 
 
while (busy == 0) 
 
busy = onewire_read(); 
 
 
onewire_reset(); 
 
onewire_write(0xCC); 
 
onewire_write(0xBE); 
 
temp1 = onewire_read(); 
 
temp2 = onewire_read(); 
 
tH=onewire_read(); 
 
tL=onewire_read(); 
 
Conf=onewire_read(); 
 
temp3 = make16(temp2, temp1); 
 
 
//result = (float) temp3 / 2.0; // 0.5 deg C resolution 
 
result = (float) temp3 / 16.0; //0.1 deg C resolution 
 
delay_ms(200); 
 
return(result); 
 
} 
 
void main() 
 
{ 
 
 
float temperature; 
 
 
delay_ms(200);
 
lcd_init();
 
 
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 
 
setup_timer_1(T1_DISABLED); 
 
setup_timer_2(T2_DISABLED,0,1); 
 
 
//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); 
 
 
 
 
while (1) 
 
{ 
 
temperature = ds1820_read(); 
 
 
printf(lcd_putc "Temp= %3.1f",temperature); 
 
} 
 
 
}  
 
  | 	  
 
adn 1wire code..
 
-----------------------------
 
 	  | Code: | 	 		  /***********************1Wire Class***********************/ 
 
/*Description: This class handles all communication */ 
 
/* between the processor and the 1wire */ 
 
/* sensors. 
 
/*********************************************************/ 
 
 
/*-------1-wire definitions-------*/ 
 
#define ONE_WIRE_PIN PIN_C6 
 
 
/*******************1-wire communication functions********************/ 
 
 
 
void onewire_reset() // OK if just using a single permanently connected device 
 
{ 
 
output_low(ONE_WIRE_PIN); 
 
delay_us( 500 ); // pull 1-wire low for reset pulse 
 
output_float(ONE_WIRE_PIN); // float 1-wire high 
 
delay_us( 500 ); // wait-out remaining initialisation window. 
 
output_float(ONE_WIRE_PIN); 
 
} 
 
 
/*********************** onewire_write() ********************************/ 
 
/*This function writes a byte to the sensor.*/ 
 
/* */ 
 
/*Parameters: byte - the byte to be written to the 1-wire */ 
 
/*Returns: */ 
 
/*********************************************************************/ 
 
 
void onewire_write(int data) 
 
{ 
 
int count; 
 
 
for (count=0; count<8; ++count) 
 
{ 
 
output_low(ONE_WIRE_PIN); 
 
delay_us( 2 ); // pull 1-wire low to initiate write time-slot. 
 
output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire 
 
delay_us( 60 ); // wait until end of write slot. 
 
output_float(ONE_WIRE_PIN); // set 1-wire high again, 
 
delay_us( 2 ); // for more than 1us minimum. 
 
} 
 
} 
 
 
/*********************** read1wire() *********************************/ 
 
/*This function reads the 8 -bit data via the 1-wire sensor. */ 
 
/* */ 
 
/*Parameters: */ 
 
/*Returns: 8-bit (1-byte) data from sensor */ 
 
/*********************************************************************/ 
 
 
int onewire_read() 
 
{ 
 
int count, data; 
 
 
for (count=0; count<8; ++count) 
 
{ 
 
output_low(ONE_WIRE_PIN); 
 
delay_us( 2 ); // pull 1-wire low to initiate read time-slot. 
 
output_float(ONE_WIRE_PIN); // now let 1-wire float high, 
 
delay_us( 8 ); // let device state stabilise, 
 
shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result. 
 
delay_us( 120 ); // wait until end of read slot. 
 
} 
 
 
return( data ); 
 
}
 
 
 | 	 
  | 
			 
		  |