#include <htc.h>
#include <STDIO.h>
//#include "Serial.h"
// GLOBAL Defines
#define TRUE 0x01
#define FALSE 0x00
#define DQ RC5
#define _XTAL_FREQ 8000000
/*==================================================================================
**
** RESET - this should ideally reset the DS18B20 and recieve a Presence Pulse
**
** =================================================================================
*/
unsigned char ow_reset (void)
{
unsigned char present;
TRISC &= ~0x01; // Set port to output
DQ = 0; // Pull DQ line Low
__delay_us(500); // Leave low for 500uS
TRISC |= 0x01; // Set port to input
DQ = 1; // Allow line to return high
__delay_us (60); // Wait 60uS for present signal
present = DQ; // Get present signal
__delay_us (60); // Wait 60uS for end of time slot
return (present); // Present signal returned
}
/*==================================================================================
**
** READ_BIT - the time_delay required for a read bit is 15uS
**
** =================================================================================
*/
unsigned char read_bit (void)
{
unsigned char i;
TRISC |= 0x01; // Set port to input
DQ = 0; // pull DQ line low to start time slot
DQ = 1; // then return immediatly high
for (i=0; i<1; i++); // require 15us time_delay, 1 cycle appears to be 12uS
return (DQ); // return value of DQ line
}
/*==================================================================================
**
** WRITE_BIT - writes a bit to the one-wire bus
**
** =================================================================================
*/
void write_bit (char b)
{
unsigned char i;
TRISC &= ~0x01; // Set port to output
DQ = 0; // pull DQ line low to start time slot
if (b==1)
DQ = 1; // return DQ high of write 1
__delay_us (60); // Wait 60uS for end of time slot
DQ = 1; // return DQ immediatly high
for (i=0; i<1; i++); // require 15us time_delay, 1 cycle appears to be 12uS
}
/*==================================================================================
**
** READ_BYTE - reads a byte from the one-wire bus
**
** =================================================================================
*/
unsigned char read_byte (void)
{
unsigned char i;
unsigned char value = 0;
TRISC |= 0x01; // Set port to input
for (i=0; i<8; i++) // reads in 8 bits of data
{
if (read_bit ()) value |= 0x01 << i; // reads data in and shifts it left
__delay_us (60); // Wait 60uS for end of time slot
}
return (value); // return value
}
/*==================================================================================
**
** WRITE_BYTE - writes a byte to the one-wire bus
**
** =================================================================================
*/
void write_byte (char val)
{
unsigned char i;
unsigned char temp;
TRISC &= ~0x01; // Set port to output
for (i=0; i<8; i++) // writes byte one bit at a time
{
temp = val >> i; // shifts val right i spaces
temp &= 0x01; // copy that bit to temp
write_bit (temp); // write bit
}
__delay_us (60); // Wait 60uS for end of time slot
}
/*==================================================================================
**
** READ_TEMPERATURE - writes a byte to the one-wire bus
**
** =================================================================================
*/
unsigned char Read_Temperature(void)
{
static char get[10];
char temp_lsb,temp_msb;
int k;
char temp_c;
unsigned char i;
ow_reset(); // Send reset and receive presence pulse
write_byte(0xCC); // Skip ROM
write_byte(0x4E); // Write Scratchpad
write_byte(0x00); // Write data byte TH
write_byte(0x00); // Write data byte TL
write_byte(0x7F); // Write data byte CONFIG
write_byte(0x4E); // Write Scratchpad
__delay_us(60); // 60uS delay
ow_reset(); // Send reset and receive presence pulse
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read ScratchPad
for (k=0;k<9;k++) // receive & read 9 data bytes
{
get[k]=read_byte();
}
ow_reset(); // Send reset and receive presence pulse
write_byte(0xCC); // Skip ROM
write_byte(0x48); // Copy Scratchpad
DQ = 1; // return DQ immediatly high
for (i=0; i<1; i++); // require 10mS time_delay, while copy is in progress
printf("\n ScratchPAD DATA = %X %X %X %X %X %X %X %X %X \n",get[8],get[7],get[6],get[5],get[4],get[3],get[2],get[1],get[0]);
temp_msb = get[1]; // Sign byte + lsbit
temp_lsb = get[0]; // Temp data plus lsb
if (temp_msb <= 0x80){temp_lsb = (temp_lsb/2);} // shift to get whole degree
temp_msb = temp_msb & 0x80; // mask all but the sign bit
if (temp_msb >= 0x80) {temp_lsb = (~temp_lsb)+1;} // twos complement
if (temp_msb >= 0x80) {temp_lsb = (temp_lsb/2);}// shift to get whole degree
if (temp_msb >= 0x80) {temp_lsb = ((-1)*temp_lsb);} // add sign bit
// printf( "\nTempC= %d degrees C\n", (int)temp_lsb ); // print temp. C
temp_c = temp_lsb;
return(temp_c);
}