ds1821.c

This project created with ISD2560 record and playback chip, DS1821 temperature sensor, PIC16F84 with circuit for sightless Voice Termometer, fire and gas alarm system. (by Ayhan Dayanik).



//***************************************
// Author: Ayhan DAYANIK
// Date  : 21/07/2003
//***************************************

#include "DS1821.h"

//Tris Port for DS1821
void DS1821_Start( void )
{
   clear_bit(DS1821PortTris,DataPin);
   clear_bit(DS1821PortTris,VddPin);
   set_bit(DS1821Port,VddPin);
   set_bit(DS1821Port,DataPin);
}

// DataPin -> HIGH
void DS1821_PinHi( void )
{
   set_bit(DS1821PortTris,DataPin);
}

// DataPin -> LOW
void DS1821_PinLow( void )
{
   clear_bit(DS1821PortTris,DataPin);
   clear_bit(DS1821Port,DataPin);
}

//OneWireMode routine
void OneWireMode( void )
{
   char counter=16;

   clear_bit(DS1821PortTris,VddPin);
   set_bit(DS1821Port,VddPin);
   DS1821_PinHi();
   delay_ms(250);
   clear_bit(DS1821Port,VddPin);

   for (;counter>0;counter--)
   {
      DS1821_PinLow();
      DS1821_PinHi();
   }
   set_bit(DS1821Port,VddPin);
   delay_ms(10);
}

//Prepare DS1821
//Initialize
char DS1821_Init( void )
{
   char temp,error;

   DS1821_PinHi();         //DQ ->HIGH with Pull-up  
   DS1821_PinLow();     //DQ ->LOW 
   delay_us(250);
   delay_us(250);       //wait 500 us
   DS1821_PinHi();         //DQ ->HIGH with Pull-up  
   delay_us(80);        //within this time presence pulse
                     //must be created by DS1821 
   temp=DS1821Port & DataPinFlag;   //Presence Puls created?
   if (temp) error=1; else error=0;
   delay_us(250);
   delay_us(170);       //500 us wait (min 480 us)
   return error;        // if result=1 then.Initialize is not success
}

//data -> DS1821
void DS1821_WriteByte( char data )
{
   char counter=8;
   DS1821_PinHi();            //DQ ->HIGH with Pull-up - first state 

   for (;counter>0;counter--)
   {
      if (data & 0x01)
      {
         DS1821_PinLow();
         asm nop
         asm nop
         asm nop
         asm nop
         asm nop
         DS1821_PinHi();
         delay_us(50);
      }else
      {
         DS1821_PinLow();
         delay_us(60);
         DS1821_PinHi();
         asm nop
         asm nop
      }
      data=data>>1;
   }
}

//data read from DS1821
char DS1821_ReadByte( void )
{
   char ibyte, counter,temp;
   counter=8;
   ibyte=0;
   DS1821_PinHi();            //DQ ->HIGH with Pull-up - first state 

   for(;counter>0;counter--)
   {
      DS1821_PinLow();
      asm nop
      DS1821_PinHi();
      asm nop
      ibyte=ibyte>>1;
      if (DS1821Port & DataPinFlag) ibyte|=0x80; else ibyte&=0x7F;
      delay_us(30);
   }
   return ibyte;
}

//write configuration to DS1821
void DS1821_WriteConfig( char config )
{
   DS1821_Init();
   DS1821_WriteByte( 0x0C );
   DS1821_WriteByte( config );
   delay_ms(20);
}

//read configuration from DS1821
char DS1821_ReadConfig( void )
{
   DS1821_Init();
   DS1821_WriteByte( 0xAC );
   return DS1821_ReadByte();
}

//DS1821i -> termometer mode
void DS1821_TermometerMode( void )
{
   char temp;
   set_bit(DS1821Port,VddPin);
   temp=DS1821_ReadConfig();
   if (temp & 0x04)
   {
      OneWireMode();
      DS1821_WriteConfig(1);
   }
}

//Read term. value from DS1821
char DS1821_ReadTemp( void )
{
   DS1821_Init();
   DS1821_WriteByte( 0xAA );
   return DS1821_ReadByte();
}

//Convert temp. start
void StartConvert( void )
{
   DS1821_Init();
   DS1821_WriteByte( 0xEE );
   delay_ms(100);
}

//Convert temp. stop
void StopConvert( void )
{
   DS1821_Init();
   DS1821_WriteByte( 0x22 );
}

//Temp measure
char Temp_Measure( void )
{
   char Temp;
   StartConvert();
   Temp=DS1821_ReadTemp();
   StopConvert();
   return Temp;
}

//Write top range for termostat mode
void Write_TH( char TH )
{
   DS1821_Init();
   DS1821_WriteByte( 0x01 );
   DS1821_WriteByte( TH );
   delay_ms(20);
}

//Write bottom range for termostat mode
void Write_TL( char TL )
{
   DS1821_Init();
   DS1821_WriteByte( 0x02 );
   DS1821_WriteByte( TL );
   delay_ms(20);
}

//Read TH from DS1821
char Read_TH( void )
{
   DS1821_Init();
   DS1821_WriteByte( 0xA1 );
   return DS1821_ReadByte();
}

//Read TL from DS1821
char Read_TL( void )
{
   DS1821_Init();
   DS1821_WriteByte( 0xA2 );
   return DS1821_ReadByte();
}



//////////////////////////////////////////////////////



http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies