#include <htc.h>
#include <stdio.h>
#include "Serial.h"
#include "Defines.h"
/*==================================================================================
**
** The 16F636 should be running at 8MHz using the internal oscillator
** Not sure at this stage how long it takes to run or what each count is
**
** =================================================================================
*/
/*==================================================================================
**
** Each communication cycle must begin with a reset from the 16F684
** To do this the port needs to be pulled low for 480uS then released
** The DS18B20 will respond by pulling the port low to indicate it's present
**
** 0 indicates device present, 1 indicates no device present
**
** DQ is on RC0
**
** Time Slot (Min 60uS - Max 120uS)
** Recovery Time - 1uS
** Reset Time (Min 480uS - Max 960uS)
** Present Detect 60uS
**
** One cycle appears to be 12uS
**
** =================================================================================
*/
/*==================================================================================
**
** RESET - this should ideally reset the DS18B20 and recieve a Presence Pulse
**
** =================================================================================
*/
unsigned char reset (void)
{
unsigned char present;
DQ = 0; // Pull DQ line Low
TRISC &= ~0x01; // Set port to output
__delay_us(500); // Leave low for 500uS
TRISC |= 0x01; // Set port to input
__delay_us(60); // Wait 60uS for present signal
present = DQ; // Get present signal
__delay_us(500); // Wait 500uS 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 data;
PORTC &= ~0x01; // preset output latch to '0'
TRISC &= ~0x01; // Set port to output - present a '0' on the bus
__delay_us(1); // Wait required time
__delay_us(1); // Wait required time
TRISC &= 0x01; // Set port to input - present a '1' on the bus
__delay_us(15); // Wait for response
// data = PORTC.0; // capture Rx bit
__delay_us(45); // complete time slot
return(data); // return value
}
/*==================================================================================
**
** WRITE_BIT - writes a bit to the one-wire bus
**
** =================================================================================
*/
void write_bit (char b)
{
unsigned char i;
PORTC &= ~0x01; // preset output latch to '0'
TRISC &= ~0x01; // Set port to output - present a '0' on the bus
if (b==1) // if were writing a 1 then - also acts as 1uS delay
TRISC &= 0x01; // Set port to input - present a '1' on the bus
__delay_us(60); // Wait 60uS for end of time slot
}
/*==================================================================================
**
** READ_BYTE - reads a byte from the one-wire bus
**
** =================================================================================
*/
unsigned char read_byte (void)
{
unsigned char data;
unsigned char count = 0;
unsigned char bt = 0;
while (count < 8) // reads in 8 bits of data
{
data >> = 1; //
bt = read_bit(); //
if (bt) data |= 128; //
count ++;
}
return (data); // return value
}
/*==================================================================================
**
** WRITE_BYTE - writes a byte to the one-wire bus
**
** =================================================================================
*/
void write_byte (char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte one bit at a time
{
temp = val & 1; // copy that bit to temp
write_bit(temp); // write bit
val >>= 1; // prep for next bit
}
}
/*==================================================================================
**
** READ_ROM - finds the 64-bit ROM code when only a single device is conected
**
** =================================================================================
*/
void Read_ROM(void)
{
int n;
static char dat[9];
printf("\nReading ROM Code\n");
reset();
write_byte(0x33);
for (n=0;n<8;n++){dat[n]=read_byte();}
printf("\n ROM Code = %X%X%X%X%X%X%X%X\n",dat[7],dat[6],dat[5],dat[4],dat[3],dat[2],dat[1],dat[0]);
}
/*==================================================================================
**
** READ_ROM - finds the 64-bit ROM code when only a single device is conected
**
** =================================================================================
*/
void Read_TEMP(void)
{
char temp_lo;
char temp_hi;
reset ();
write_byte (0xCC); // Send "skip rom"
write_byte (0x44); // Send "convert"
while (!read_bit()); // wait for convert complete
reset ();
write_byte (0xCC); // Send "skip rom"
write_byte (0xBE); // Send "read scratchpad"
temp_lo = read_byte();
temp_hi = read_byte();
reset ();
}