Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is some code that I wrote for a PIC18 to read a DS18B20.</p> <pre><code>/**************************************************************************** * temperature.h ****************************************************************************/ #ifndef TEMP_H #define TEMP_H extern double read_temp(void) ; extern void start_temp(void); #endif /**************************************************************************** * temperature.c ****************************************************************************/ void reset_ow(void); void write_ow(uint8_t b); uint8_t read_ow (void); #define OW_TEMP_SIG LATHbits.LATH0 #define OW_TEMP_TRIS TRISHbits.TRISH0 #define OW_TEMP_SIG_IN PORTHbits.RH0 #define DIR_OUT 0 #define DIR_IN 1 void start_temp(void) { uint8_t i; OW_TEMP_SIG=1; OW_TEMP_TRIS=DIR_OUT; for ( i=0;i&lt;100;i++) { Delay_us(100); } reset_ow(); write_ow(0xcc); // skip rom write_ow(0x44); // start t conv } double read_temp(void) { double temp=0; S16 itemp; reset_ow(); write_ow(0xcc); // skip rom write_ow(0xbe); // read scratch pad itemp=read_ow(); itemp|=(S16)read_ow()&lt;&lt;8; temp = itemp*(0.0625); OW_TEMP_TRIS=DIR_IN; OW_TEMP_SIG=1; return temp; } void reset_ow(void) { OW_TEMP_TRIS=DIR_OUT; OW_TEMP_SIG=0; Delay_us(250); Delay_us(250); OW_TEMP_TRIS=DIR_IN; OW_TEMP_SIG=1; Delay_us(250); Delay_us(250); } void write_ow(uint8_t b) { uint8_t i; OW_TEMP_SIG=1; OW_TEMP_TRIS=DIR_OUT; for ( i=0;i&lt;8;i++) { OW_TEMP_SIG=0; if ( b &amp; 0x01 ) { Delay_us(10); OW_TEMP_SIG=1; } Delay_us(70); OW_TEMP_SIG=1; Delay_us(10); b &gt;&gt;= 1; } OW_TEMP_TRIS=DIR_IN; OW_TEMP_SIG=1; } uint8_t read_ow(void) { uint8_t b=0; uint8_t m; uint8_t i; m=1; for ( i=0;i&lt;8;i++) { OW_TEMP_SIG=1; OW_TEMP_TRIS=DIR_OUT; OW_TEMP_SIG=0; Delay_us(8); OW_TEMP_TRIS=DIR_IN; OW_TEMP_SIG=1; Delay_us(15); if ( 1 == OW_TEMP_SIG_IN ) { b |= m; } m &lt;&lt;=1; Delay_us(60); } OW_TEMP_TRIS=DIR_IN; OW_TEMP_SIG=1; return b; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload