16F690 and DS18b20

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Hi
Have this setup,
16F690 connected to LCD = WORKS FINE
Connected a DS18b20 temperatur sensor.
When Building the code the result just be.

Error [103] C:\******\ds1820.h; 55.35 #error: DS1820 data pin not defined!

Here is the code.

C:
#include <htc.h>
#include <stdlib.h>
#include "lcd.h"
#include "ds1820.h"
__CONFIG (FCMEN_ON & IESO_OFF & BOREN_OFF & CPD_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTRCIO);

#define _XTAL_FREQ  4000000
#define FOSC 4000000L
#define DS1820_DATAPIN PIN_C2
//global defs



void setup(void)
{

  TRISC=0; // c output 0b11111000;//RC0-RC2 out,RC3-RC6 in,RC7 in
  TRISB=0; //All port B output
GIE = 0; // Global interrupt disable just in case
IRCF0=1;
IRCF1=1;
IRCF2=1;
ANSEL=0;
  ANSELH=0;
SCS=1; //internal clock


//setup LCD
  lcd_init();
lcd_goto(0); // select first line

}


void main (void) //hovedprogram
{
setup();

GIE=0;
lcd_clear();
lcd_goto(0x00);
lcd_puts("Godav doo");
lcd_goto(0x40); // linje 2
  lcd_puts("Den virker");
  while (1)
  {

}

}
And here is the ds1820.c file

C:
/*** FILEHEADER ****************************************************************

*

* FILENAME: ds1820.h

* DATE: 25.02.2005

* AUTHOR: Christian Stadler

*

* DESCRIPTION: Driver for DS1820 1-Wire Temperature sensor (Dallas)

*

******************************************************************************/

/*** HISTORY OF CHANGE *********************************************************

*

* $Log: /pic/_drv/ds1820.h $

*

* 9 13.11.10 20:02 Stadler

* - changed interrupt lock and delay functions to #defines to remove

* function call overhead

*

* 8 6.11.10 10:24 Stadler

* - adjusted 1-wire timing

*

* 7 5.11.10 22:55 Stadler

* - changed driver API

*

* 6 5.11.10 21:59 Stadler

* - added DS18B20 support

* - fixed problem with ROM search algorithm

*

* 5 2.11.10 20:25 Stadler

* - changed function DS1820_FindFirstDev to DS1820_FindFirstDevice

* - changed function DS1820_FindNextDev to DS1820_FindNextDevice

* - updated code style

*

* 4 31.10.10 17:12 Stadler

* - introduced DS1820_DelayUs

*

* 3 25.10.10 13:09 Stadler

* - added interrupt lock

*

* 2 12.03.05 11:24 Stadler

* - added EEPROM write function

* - added "Search ROM Algorithm" to control multiple devices

*

* 1 26.02.05 18:18 Stadler

* Driver for DS1820 1-Wire Temperature sensor (Dallas)

*

******************************************************************************/

#ifndef _DS1820_H


#define _DS1820_H



/* check configuration of driver */

#ifndef DS1820_DATAPIN


#error DS1820 data pin not defined!

#endif



#define TEMP_RES 0x100 /* temperature resolution => 1/256C = 0.0039C */



/* -------------------------------------------------------------------------- */

/* DS1820 Timing Parameters */

/* -------------------------------------------------------------------------- */

#define DS1820_RST_PULSE 480 /* master reset pulse time in [us] */

#define DS1820_MSTR_BITSTART 2 /* delay time for bit start by master */

#define DS1820_PRESENCE_WAIT 40 /* delay after master reset pulse in [us] */

#define DS1820_PRESENCE_FIN 480 /* dealy after reading of presence pulse [us] */

#define DS1820_BITREAD_DLY 5 /* bit read delay */

#define DS1820_BITWRITE_DLY 100 /* bit write delay */



/* -------------------------------------------------------------------------- */

/* DS1820 Registers */

/* -------------------------------------------------------------------------- */

#define DS1820_REG_TEMPLSB 0


#define DS1820_REG_TEMPMSB 1


#define DS1820_REG_CNTREMAIN 6


#define DS1820_REG_CNTPERSEC 7


#define DS1820_SCRPADMEM_LEN 9 /* length of scratchpad memory */

#define DS1820_ADDR_LEN 8



/* -------------------------------------------------------------------------- */

/* DS1820 Commands */

/* -------------------------------------------------------------------------- */

#define DS1820_CMD_SEARCHROM 0xF0


#define DS1820_CMD_READROM 0x33


#define DS1820_CMD_MATCHROM 0x55


#define DS1820_CMD_SKIPROM 0xCC


#define DS1820_CMD_ALARMSEARCH 0xEC


#define DS1820_CMD_CONVERTTEMP 0x44


#define DS1820_CMD_WRITESCRPAD 0x4E


#define DS1820_CMD_READSCRPAD 0xBE


#define DS1820_CMD_COPYSCRPAD 0x48


#define DS1820_CMD_RECALLEE 0xB8



#define DS1820_FAMILY_CODE_DS18B20 0x28


#define DS1820_FAMILY_CODE_DS18S20 0x10



/* -------------------------------------------------------------------------- */

/* static variables */

/* -------------------------------------------------------------------------- */

static bool bDoneFlag;


static uint8 nLastDiscrepancy_u8;


static uint8 nRomAddr_au8[DS1820_ADDR_LEN];





/* -------------------------------------------------------------------------- */

/* Low-Level Functions */

/* -------------------------------------------------------------------------- */



/*******************************************************************************

* FUNCTION: DS1820_DelayUs

* PURPOSE: Delay for the given number of micro seconds.

*

* INPUT: dly_us number of micro seconds to delay

* OUTPUT: -

* RETURN: -

******************************************************************************/

#define DS1820_DelayUs(dly_us) delay_us(dly_us)



/*******************************************************************************

* FUNCTION: DS1820_DelayMs

* PURPOSE: Delay for the given number of milliseconds.

*

* INPUT: dly_ms number of milliseconds to delay

* OUTPUT: -

* RETURN: -

******************************************************************************/

#define DS1820_DelayMs(dly_ms) delay_ms(dly_ms)



/*******************************************************************************

* FUNCTION: DS1820_DisableInterrupts

* PURPOSE: Disable interrupts

*

* INPUT: -

* OUTPUT: -

* RETURN: -

******************************************************************************/

#ifdef DS1820_INTERRUPT_LOCK


#define DS1820_DisableInterrupts() disable_interrupts(GLOBAL)

#else

#define DS1820_DisableInterrupts()

#endif





/*******************************************************************************

* FUNCTION: DS1820_EnableInterrupts

* PURPOSE: Enable interrupts

*

* INPUT: -

* OUTPUT: -

* RETURN: -

******************************************************************************/

#ifdef DS1820_INTERRUPT_LOCK


#define DS1820_EnableInterrupts() enable_interrupts(GLOBAL)

#else

#define DS1820_EnableInterrupts()

#endif



/*******************************************************************************

* FUNCTION: DS1820_Reset

* PURPOSE: Initializes the DS1820 device.

*

* INPUT: -

* OUTPUT: -

* RETURN: FALSE if at least one device is on the 1-wire bus, TRUE otherwise

******************************************************************************/

bool DS1820_Reset(void)

{

bool bPresPulse;

DS1820_DisableInterrupts();



/* reset pulse */

output_low(DS1820_DATAPIN);


DS1820_DelayUs(DS1820_RST_PULSE);

output_high(DS1820_DATAPIN);

/* wait until pullup pull 1-wire bus to high */

DS1820_DelayUs(DS1820_PRESENCE_WAIT);

/* get presence pulse */

bPresPulse = input(DS1820_DATAPIN);


DS1820_DelayUs(424);



DS1820_EnableInterrupts();

return bPresPulse;

}



/*******************************************************************************

* FUNCTION: DS1820_ReadBit

* PURPOSE: Reads a single bit from the DS1820 device.

*

* INPUT: -

* OUTPUT: -

* RETURN: bool value of the bit which as been read form the DS1820

******************************************************************************/

bool DS1820_ReadBit(void)

{

bool bBit;

DS1820_DisableInterrupts();



output_low(DS1820_DATAPIN);

DS1820_DelayUs(DS1820_MSTR_BITSTART);

input(DS1820_DATAPIN);

DS1820_DelayUs(DS1820_BITREAD_DLY);

bBit = input(DS1820_DATAPIN);



DS1820_EnableInterrupts();

return (bBit);

}



/*******************************************************************************

* FUNCTION: DS1820_WriteBit

* PURPOSE: Writes a single bit to the DS1820 device.

*

* INPUT: bBit value of bit to be written

* OUTPUT: -

* RETURN: -

******************************************************************************/

void DS1820_WriteBit(bool bBit)

{

DS1820_DisableInterrupts();



output_low(DS1820_DATAPIN);

DS1820_DelayUs(DS1820_MSTR_BITSTART);

if (bBit != FALSE)

{

output_high(DS1820_DATAPIN);

}

DS1820_DelayUs(DS1820_BITWRITE_DLY);

output_high(DS1820_DATAPIN);



DS1820_EnableInterrupts();

}



/*******************************************************************************

* FUNCTION: DS1820_ReadByte

* PURPOSE: Reads a single byte from the DS1820 device.

*

* INPUT: -

* OUTPUT: -

* RETURN: uint8 byte which has been read from the DS1820

******************************************************************************/

uint8 DS1820_ReadByte(void)

{

uint8 i;

uint8 value = 0;

for (i=0 ; i < 8; i++)

{

if ( DS1820_ReadBit() )

{

value |= (1 << i);

}

DS1820_DelayUs(120);

}

return(value);

}



/*******************************************************************************

* FUNCTION: DS1820_WriteByte

* PURPOSE: Writes a single byte to the DS1820 device.

*

* INPUT: val_u8 byte to be written

* OUTPUT: -

* RETURN: -

******************************************************************************/

void DS1820_WriteByte(uint8 val_u8)

{

uint8 i;

uint8 temp;

for (i=0; i < 8; i++) /* writes byte, one bit at a time */

{

temp = val_u8 >> i; /* shifts val right 'i' spaces */

temp &= 0x01; /* copy that bit to temp */

DS1820_WriteBit(temp); /* write bit in temp into */

}


DS1820_DelayUs(105);

}





/* -------------------------------------------------------------------------- */

/* API Interface */

/* -------------------------------------------------------------------------- */



/*******************************************************************************

* FUNCTION: DS1820_AddrDevice

* PURPOSE: Addresses a single or all devices on the 1-wire bus.

*

* INPUT: nAddrMethod use DS1820_CMD_MATCHROM to select a single

* device or DS1820_CMD_SKIPROM to select all

* OUTPUT: -

* RETURN: -

******************************************************************************/

void DS1820_AddrDevice(uint8 nAddrMethod)

{

uint8 i;



if (nAddrMethod == DS1820_CMD_MATCHROM)

{

DS1820_WriteByte(DS1820_CMD_MATCHROM); /* address single devices on bus */

for (i = 0; i < DS1820_ADDR_LEN; i ++)

{

DS1820_WriteByte(nRomAddr_au8[I]);

}

}

else

{

DS1820_WriteByte(DS1820_CMD_SKIPROM); /* address all devices on bus */

}


}



/*******************************************************************************

* FUNCTION: DS1820_FindNextDevice

* PURPOSE: Finds next device connected to the 1-wire bus.

*

* INPUT: -

* OUTPUT: nRomAddr_au8[] ROM code of the next device

* RETURN: bool TRUE if there are more devices on the 1-wire

* bus, FALSE otherwise

******************************************************************************/

bool DS1820_FindNextDevice(void)

{

uint8 state_u8;

uint8 byteidx_u8;

uint8 mask_u8 = 1;

uint8 bitpos_u8 = 1;

uint8 nDiscrepancyMarker_u8 = 0;

bool bit_b;

bool bStatus;

bool next_b = FALSE;

/* init ROM address */

for (byteidx_u8=0; byteidx_u8 < 8; byteidx_u8 ++)

{

nRomAddr_au8[byteidx_u8] = 0x00;

}

bStatus = DS1820_Reset(); /* reset the 1-wire */

if (bStatus || bDoneFlag) /* no device found */

{

nLastDiscrepancy_u8 = 0; /* reset the search */

return FALSE;

}

/* send search rom command */

DS1820_WriteByte(DS1820_CMD_SEARCHROM);


byteidx_u8 = 0;

do

{


state_u8 = 0;

/* read bit */

if ( DS1820_ReadBit() != 0 )

{

state_u8 = 2;

}

DS1820_DelayUs(120);

/* read bit complement */

if ( DS1820_ReadBit() != 0 )

{

state_u8 |= 1;

}

DS1820_DelayUs(120);

/* description for values of state_u8: */

/* 00 There are devices connected to the bus which have conflicting */

/* bits in the current ROM code bit position. */

/* 01 All devices connected to the bus have a 0 in this bit position. */

/* 10 All devices connected to the bus have a 1 in this bit position. */

/* 11 There are no devices connected to the 1-wire bus. */

/* if there are no devices on the bus */

if (state_u8 == 3)

{

break;

}

else

{

/* devices have the same logical value at this position */

if (state_u8 > 0)

{

/* get bit value */

bit_b = (bool)(state_u8 >> 1);

}

/* devices have confilcting bits in the current ROM code */

else

{

/* if there was a conflict on the last iteration */

if (bitpos_u8 < nLastDiscrepancy_u8)

{

/* take same bit as in last iteration */

bit_b = ( (nRomAddr_au8[byteidx_u8] & mask_u8) > 0 );


}

else

{


bit_b = (bitpos_u8 == nLastDiscrepancy_u8);

}

if (bit_b == 0)

{

nDiscrepancyMarker_u8 = bitpos_u8;

}

}

/* store bit in ROM address */

if (bit_b != 0)

{

nRomAddr_au8[byteidx_u8] |= mask_u8;

}

else

{


nRomAddr_au8[byteidx_u8] &= ~mask_u8;

}

DS1820_WriteBit(bit_b);

/* increment bit position */

bitpos_u8 ++;

/* calculate next mask value */

mask_u8 = mask_u8 << 1;

/* check if this byte has finished */

if (mask_u8 == 0)

{

byteidx_u8 ++; /* advance to next byte of ROM mask */

mask_u8 = 1; /* update mask */

}


}

} while (byteidx_u8 < DS1820_ADDR_LEN);



/* if search was unsuccessful then */

if (bitpos_u8 < 65)

{

/* reset the last discrepancy to 0 */

nLastDiscrepancy_u8 = 0;


}

else

{

/* search was successful */

nLastDiscrepancy_u8 = nDiscrepancyMarker_u8;


bDoneFlag = (nLastDiscrepancy_u8 == 0);

/* indicates search is not complete yet, more parts remain */

next_b = TRUE;


}

return next_b;

}



/*******************************************************************************

* FUNCTION: DS1820_FindFirstDevice

* PURPOSE: Starts the device search on the 1-wire bus.

*

* INPUT: -

* OUTPUT: nRomAddr_au8[] ROM code of the first device

* RETURN: bool TRUE if there are more devices on the 1-wire

* bus, FALSE otherwise

******************************************************************************/

bool DS1820_FindFirstDevice(void)

{

nLastDiscrepancy_u8 = 0;

bDoneFlag = FALSE;

return ( DS1820_FindNextDevice() );

}



/*******************************************************************************

* FUNCTION: DS1820_WriteEEPROM

* PURPOSE: Writes to the DS1820 EEPROM memory (2 bytes available).

*

* INPUT: nTHigh high byte of EEPROM

* nTLow low byte of EEPROM

* OUTPUT: -

* RETURN: -

******************************************************************************/

void DS1820_WriteEEPROM(uint8 nTHigh, uint8 nTLow)

{

/* --- write to scratchpad ----------------------------------------------- */

DS1820_Reset();


DS1820_AddrDevice(DS1820_CMD_MATCHROM);

DS1820_WriteByte(DS1820_CMD_WRITESCRPAD); /* start conversion */

DS1820_WriteByte(nTHigh);


DS1820_WriteByte(nTLow);

DS1820_DelayUs(10);

DS1820_Reset();

DS1820_AddrDevice(DS1820_CMD_MATCHROM);

DS1820_WriteByte(DS1820_CMD_COPYSCRPAD); /* start conversion */

delay_ms(10);


}



/*******************************************************************************

* FUNCTION: DS1820_GetTempRaw

* PURPOSE: Get temperature raw value from single DS1820 device.

*

* Scratchpad Memory Layout

* Byte Register

* 0 Temperature_LSB

* 1 Temperature_MSB

* 2 Temp Alarm High / User Byte 1

* 3 Temp Alarm Low / User Byte 2

* 4 Reserved

* 5 Reserved

* 6 Count_Remain

* 7 Count_per_C

* 8 CRC

*

* Temperature calculation for DS18S20 (Family Code 0x10):

* =======================================================

* (Count_per_C - Count_Remain)

* Temperature = temp_raw - 0.25 + ----------------------------

* Count_per_C

*

* Where temp_raw is the value from the temp_MSB and temp_LSB with

* the least significant bit removed (the 0.5C bit).

*

*

* Temperature calculation for DS18B20 (Family Code 0x28):

* =======================================================

* bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0

* LSB 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4

* bit15 bit14 bit13 bit12 bit3 bit2 bit1 bit0

* MSB S S S S S 2^6 2^5 2^4

*

* The temperature data is stored as a 16-bit sign-extended twos

* complement number in the temperature register. The sign bits (S)

* indicate if the temperature is positive or negative: for

* positive numbers S = 0 and for negative numbers S = 1.

*

* RETURN: sint16 raw temperature value with a resolution

* of 1/256C

******************************************************************************/

sint16 DS1820_GetTempRaw(void)

{

uint8 i;

uint16 temp_u16;

uint16 highres_u16;

uint8 scrpad[DS1820_SCRPADMEM_LEN];

/* --- start temperature conversion -------------------------------------- */

DS1820_Reset();

DS1820_AddrDevice(DS1820_CMD_MATCHROM); /* address the device */

output_high(DS1820_DATAPIN);

DS1820_WriteByte(DS1820_CMD_CONVERTTEMP); /* start conversion */

//DS1820_DelayMs(DS1820_TEMPCONVERT_DLY); /* wait for temperature conversion */

DS1820_DelayMs(750);




/* --- read sratchpad ---------------------------------------------------- */

DS1820_Reset();

DS1820_AddrDevice(DS1820_CMD_MATCHROM); /* address the device */

DS1820_WriteByte(DS1820_CMD_READSCRPAD); /* read scratch pad */

/* read scratch pad data */

for (i=0; i < DS1820_SCRPADMEM_LEN; i++)

{

scrpad[I] = DS1820_ReadByte();

}



/* --- calculate temperature --------------------------------------------- */

/* Formular for temperature calculation: */

/* Temp = Temp_read - 0.25 + ((Count_per_C - Count_Remain)/Count_per_C) */

/* get raw value of temperature (0.5C resolution) */

temp_u16 = 0;


temp_u16 = (uint16)((uint16)scrpad[DS1820_REG_TEMPMSB] << 8);

temp_u16 |= (uint16)(scrpad[DS1820_REG_TEMPLSB]);

if (nRomAddr_au8[0] == DS1820_FAMILY_CODE_DS18S20)

{

/* get temperature value in 1C resolution */

temp_u16 >>= 1;




/* temperature resolution is TEMP_RES (0x100), so 1C equals 0x100 */

/* => convert to temperature to 1/256C resolution */

temp_u16 = ((uint16)temp_u16 << 8);




/* now substract 0.25C */

temp_u16 -= ((uint16)TEMP_RES >> 2);




/* now calculate high resolution */

highres_u16 = scrpad[DS1820_REG_CNTPERSEC] - scrpad[DS1820_REG_CNTREMAIN];


highres_u16 = ((uint16)highres_u16 << 8);

if (scrpad[DS1820_REG_CNTPERSEC])

{

highres_u16 = highres_u16 / (uint16)scrpad[DS1820_REG_CNTPERSEC];

}



/* now calculate result */

highres_u16 = highres_u16 + temp_u16;


}

else

{

/* 12 bit temperature value has 0.0625C resolution */

/* shift left by 4 to get 1/256C resolution */

highres_u16 = temp_u16;


highres_u16 <<= 4;

}

return (highres_u16);

}



/*******************************************************************************

* FUNCTION: DS1820_GetTempFloat

* PURPOSE: Converts internal temperature value to string (physical value).

*

* INPUT: none

* OUTPUT: none

* RETURN: float temperature value with as float value

******************************************************************************/

float DS1820_GetTempFloat(void)

{

return ((float)DS1820_GetTempRaw() / (float)TEMP_RES);

}



/*******************************************************************************

* FUNCTION: DS1820_GetTempString

* PURPOSE: Converts internal temperature value to string (physical value).

*

* INPUT: tRaw_s16 internal temperature value

* OUTPUT: strTemp_pc user string buffer to write temperature value

* RETURN: sint16 temperature value with an internal resolution

* of TEMP_RES

******************************************************************************/

void DS1820_GetTempString(sint16 tRaw_s16, char *strTemp_pc)

{

sint16 tPhyLow_s16;

sint8 tPhy_s8;

/* convert from raw value (1/256C resolution) to physical value */

tPhy_s8 = (sint8)(tRaw_s16/TEMP_RES);

/* convert digits from raw value (1/256C resolution) to physical value */

/*tPhyLow_u16 = tInt_s16 % TEMP_RES;*/

tPhyLow_s16 = tRaw_s16 & 0xFF; /* this operation is the same as */

/* but saves flash memory tInt_s16 % TEMP_RES */

tPhyLow_s16 = tPhyLow_s16 * 100;


tPhyLow_s16 = (uint16)tPhyLow_s16 / TEMP_RES;

/* write physical temperature value to string */

sprintf(strTemp_pc, "%d.%02d", tPhy_s8, (sint8)tPhyLow_s16);

}

#endif /* _DS1820_H */
Any hint to why ?


Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
You have to #define DS1820_DATAPIN before including ds1820.h
The error is generated at line 106 of ds1820.h - the tipoff is the #error message - that is a user generated error which will be in the source file.

Good luck.
 

JohnInTX

Joined Jun 26, 2012
4,787
OK.. well, fix that first then resolve the function output_low(DS1820_DATAPIN)
I don't know if output_low is in the compiler's library- Search the project for it and see what it wants as parameters.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
just dont get it, why is there a million different 1820.h files out on the net.
when reading it should be so simple to get it to Work,
Have a analog tmp35 sensor before, and that was pretty easy to get working,
 

be80be

Joined Jul 5, 2008
2,395
Some thing like this just use your pin names
Code:
#define Port_18B20          RB4
#define Tx_18B20            TRISB4 = 0
#define Rx_18B20            TRISB4 = 1
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
when moving the #define DS1820_DATAPIN to before including ds1820.h
i get a lots of errors.
Warning [374] C:\**\ds1820.h; 111.13 missing basic type; int assumed
Error [372] C:\**\ds1820.h; 111.13 "," expected
Warning [374] C:\**\ds1820.h; 112.14 missing basic type; int assumed
Error [372] C:\**\ds1820.h; 112.14 "," expected

and a LOT more errors,
should i not include the 1820.h file ?
 

ErnieM

Joined Apr 24, 2011
8,415
A #define statement is a simple text replacement used by the C pre-processor. The best way to figure out how to write those a software package needs is to consult the documentation of that same package.

So you need to hunt down this Christian Stadler and hold him down until he explains what his ds1820 stuff needs.

You may want o see if he has a website before using physical force.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Hi
Have been putting something together, from stole from net.
Now i have som working thing, but somehow the temperatur is wrong, and is dropping when Warming up the sensor,
and rising when cooling Down, any hint on why

Code:
#include <htc.h>            //include hitech C header file
#include "lcd.h"

#define    _XTAL_FREQ            4000000

// This configuration bits are for PIC16F690 using internal crystal

//__CONFIG(FCMEN_ON & IESO_OFF & BOREN_OFF & CPD_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTRCIO);
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)


#define SCS_BIT                0x01
#define INT_OSC_8MHZ         (0b01110000 | SCS_BIT)
#define INT_OSC_4MHZ         (0b01100000 | SCS_BIT)
#define INT_OSC_2MHZ         (0b01010000 | SCS_BIT)
#define INT_OSC_1MHZ         (0b01000000 | SCS_BIT)
#define INT_OSC_500KHZ         (0b00110000 | SCS_BIT)
#define INT_OSC_250KHZ         (0b00100000 | SCS_BIT)
#define INT_OSC_125KHZ         (0b00010000 | SCS_BIT)
#define INT_OSC_31KHZ         (0b00000000 | SCS_BIT)

#define Skip_ROM             0xCC
#define Convert_T             0x44
#define Read_scratchpad     0xBE

#define Port_18B20             RA2
#define Tx_18B20             TRISA2 = 0
#define Rx_18B20             TRISA2 = 1

#define WAIT1                1000
#define WAIT2                500

#define DS18B20_CONV_TIME    750
#define DS18B20_RESET_PULSE 480
#define DS18B20_WAIT_TIME     60
#define DS18B20_PULLUP_TIME 2

#define ONEWIRE_PRESENT     0
#define ONEWIRE_ABSENT         1

unsigned char TLow;
unsigned char vDisp[9];
unsigned char DP;


void init (void)
{
    ANSEL = 0;        //Initialize A/D ports off
    ANSELH = 0;        //Initialize A/D ports off
    CM1CON0 = 0;    //Initialize comp 1 off
    CM2CON0 = 0;    //Initialize comp 2 off
    PORTA = 0x00;    // Clears PORTA
    PORTB = 0x00;    // Clears PORTB
    PORTC = 0x00;    // Clears PORTC
    TRISA = 0x00;    // Sets PORTA to all output
    TRISB = 0x00;    // Sets PORTB to all output
    TRISC = 0x00;    // sets PORTC to all output.
    //TRISA1=1;

    // We want 8MHz
    OSCCON = INT_OSC_8MHZ;

    // Wait for clock to stabilise.
    __delay_ms(10);
}

/*void nibble(unsigned byte)
{
    PORTC = (unsigned)(byte & 0x0f);
    return;
}*/

/*void delay_ms(unsigned int ui_value)
{
    while (ui_value-- != 0) {
        __delay_ms(1);                    // macro from HI-TECH compiler which will generate 1ms delay base on value of _XTAL_FREQ in system.h
    }   
}*/

unsigned char reset()
{
    Tx_18B20;                            // Tris = 0 (output)
    Port_18B20 = 0;                        // set pin# to low (0)
    __delay_us(DS18B20_RESET_PULSE);    // 1 wire require time delay
    Rx_18B20;                            // Tris = 1 (input)
    __delay_us(DS18B20_WAIT_TIME);        // 1 wire require time delay

    if (Port_18B20 == 0)                 // if there is a presence pluse
    {
        __delay_us(DS18B20_RESET_PULSE);// Reset and return
        return ONEWIRE_PRESENT;            // return 0 ( 1-wire is presence)
    }

    __delay_us(DS18B20_RESET_PULSE);    // Reset and return
    return ONEWIRE_ABSENT;                // return 1 ( 1-wire is NOT presence)
}

void write(char WRT)
{
    char i,Cmd;
    Cmd = WRT;
    Rx_18B20; // set pin# to input (1)
    for(i = 0; i < 8; i++)
    {
        if((Cmd & (1<<i))!= 0)
        {
            // write 1
            Tx_18B20; // set pin# to output (0)
            Port_18B20 = 0; // set pin# to low (0)
            __delay_us(DS18B20_PULLUP_TIME); // 1 wire require time delay
            Rx_18B20; // set pin# to input (release the bus)
            __delay_us(DS18B20_WAIT_TIME); // 1 wire require time delay
        } else  {
            //write 0
            Tx_18B20; // set pin# to output (0)
            Port_18B20 = 0; // set pin# to low (0)
            __delay_us(DS18B20_WAIT_TIME); // 1 wire require time delay
            Rx_18B20; // set pin# to input (release the bus)
        }
    }
}

unsigned char read()
{
    char i,result = 0;
    Rx_18B20; // TRIS is input(1)
    for(i = 0; i < 8; i++)
    {
        Tx_18B20; // TRIS is output(0)
        Port_18B20 = 0; // genarate low pluse for 2us
        __delay_us(DS18B20_PULLUP_TIME);
        Rx_18B20; // TRIS is input(1) release the bus
        if(Port_18B20 != 0) {
            result |= 1<<i;
        }
        __delay_us(DS18B20_WAIT_TIME); // wait for recovery time
    }
    return result;
}

/*******************************************************************************
* MAIN FUNCTION                                                                *
*******************************************************************************/
int main(void)
{
    //unsigned temp, val;
    unsigned short tempL, tempH; //, fraction;
    //unsigned int i = 0;    // declare a variable to store  
      unsigned int temptotal  ;
    init();
     lcd_init();
    __delay_ms(500);
    lcd_goto(0x00);
    lcd_puts("Try again");
    __delay_ms(2000);
    lcd_clear();
    vDisp[4] = '.';
    vDisp[6] = " ";
     vDisp[7] = "ß";
     vDisp[8] = 'C';
     lcd_goto(0x00);
     lcd_puts("Temp:");

    while(1)     // create an infinite loop
    {       
        if (!reset() == ONEWIRE_PRESENT)
        {
          
            write(Skip_ROM);
            write(Convert_T);       
            __delay_ms(DS18B20_CONV_TIME);
           
            reset();
            write(Skip_ROM);       
            write(Read_scratchpad);   
           
            tempL = read();            //read low temp value
            tempH = read();            //read high temp value
           // TLow = read();
            //temptotal = (tempH << 8) + tempL;

            DP = tempL & 0x01; // 'Check if Temperature is integer or fractional
         if (tempH){ //If reading is negative
            vDisp[0] = '-';
            tempL = ((~tempL) + 1) >> 1;
         }
         else{
             vDisp[0] = '+';
             tempL = tempL >> 1; // 'Shift one position right (divide by 2) to get integer reading and get rid of decimal point
         }
         vDisp[1] = (tempL / 100) + 48; // 'Get hundreds and convert to ASCII
         vDisp[2] = ((tempL / 10) % 10) + 48; // 'Get tens and convert to ASCII
         vDisp[3] = (tempL % 10) + 48; // 'Get units and convert to ASCII
         if (DP){ // 'If reading is fractional, ie has 0.5 at end
            vDisp[5] = '5';
         }
         else{ // 'If reading is a whole number
            vDisp[5] = '0';
         }
         lcd_goto(0x08);
         lcd_putch(vDisp[0]);
         lcd_putch(vDisp[1]);
         lcd_putch(vDisp[2]);
         lcd_putch(vDisp[3]);
         lcd_putch(vDisp[4]);
         lcd_putch(vDisp[5]);
         lcd_putch(vDisp[6]);
         lcd_putch(vDisp[7]);
         lcd_putch(vDisp[8]);
         lcd_goto(0x40);
        
        
           
           

        
        }
    }

   
}
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Can someone help me,
HAve been struggeling for days now.

iam not very strong in bit manipulation..
The string that will be read is
like this
0000 0001 1001 0001 should bee +25,0625 and i can see how according to datasheet.
0000 = always 0 when positive.
0001 = is bit 8 -7 - 6 -5 together with 1001 should be 25 in dec.
the last part is the part after the "," fraction of degree, this be 0,0625 - 0,1250 - 0,1875 - 0,2500 ect to 0,9375 and 0.0000 aka 1/16 degree.
but how can i ex put these binary numbers to a int ? to use i thermometer.
should bee able to compare the reading to older reading, this is why i need int,
i am lost.....
 
Top