Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

ds1631.c

Go to the documentation of this file.
00001 /*! \file ds1631.c \brief Dallas DS1631 Temperature Sensor Driver Library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'ds1631.c'
00005 // Title        : Dallas DS1631 Temperature Sensor Driver Library
00006 // Author       : Pascal Stang - Copyright (C) 2004
00007 // Created      : 2004.02.10
00008 // Revised      : 2004.02.19
00009 // Version      : 0.1
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // NOTE: This code is currently below version 1.0, and therefore is considered
00014 // to be lacking in some functionality or documentation, or may not be fully
00015 // tested.  Nonetheless, you can expect most functions to work.
00016 //
00017 // This code is distributed under the GNU Public License
00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00019 //
00020 //*****************************************************************************
00021 
00022 #include <avr/io.h>
00023 #include <avr/signal.h>
00024 #include <avr/interrupt.h>
00025 
00026 #include "global.h"
00027 #include "timer.h"
00028 #include "i2c.h"
00029 #include "ds1631.h"
00030 
00031 // global variables
00032 
00033 // Functions
00034 u08 ds1631Init(u08 i2cAddr)
00035 {
00036     u08 chip_ok;
00037     // issue a reset
00038     if(ds1631Reset(i2cAddr) == I2C_OK)
00039         chip_ok = TRUE;
00040     else
00041         chip_ok = FALSE;
00042     // set a default configuration
00043     // (1-shot mode, T_OUT active high, and 12-bit conversion)
00044     ds1631SetConfig(i2cAddr,
00045         DS1631_CONFIG_1SHOT | DS1631_CONFIG_POL |
00046         DS1631_CONFIG_R0 | DS1631_CONFIG_R1);
00047     return chip_ok;
00048 }
00049 
00050 u08 ds1631Reset(u08 i2cAddr)
00051 {
00052     u08 buffer[1];
00053     // return the DS1631 to power-on reset defaults
00054     buffer[0] = DS1631_CMD_SWPOR;
00055     return i2cMasterSendNI(i2cAddr, 1, buffer);
00056 }
00057 
00058 void ds1631SetConfig(u08 i2cAddr, u08 config)
00059 {
00060     u08 buffer[2];
00061     // write the DS1631 configuration byte
00062     buffer[0] = DS1631_CMD_ACCESSCONFIG;
00063     buffer[1] = config;
00064     i2cMasterSendNI(i2cAddr, 2, buffer);
00065 }
00066 
00067 u08 ds1631GetConfig(u08 i2cAddr)
00068 {
00069     u08 buffer[1];
00070     // write the DS1631 configuration byte
00071     buffer[0] = DS1631_CMD_ACCESSCONFIG;
00072     i2cMasterSendNI(i2cAddr, 2, buffer);
00073     i2cMasterReceiveNI(i2cAddr, 2, buffer);
00074     return buffer[0];
00075 }
00076 
00077 void ds1631StartConvert(u08 i2cAddr)
00078 {
00079     u08 buffer[1];
00080     // send the DS1631 Start Convert command
00081     buffer[0] = DS1631_CMD_STARTCONV;
00082     i2cMasterSendNI(i2cAddr, 1, buffer);
00083 }
00084 
00085 void ds1631StopConvert(u08 i2cAddr)
00086 {
00087     u08 buffer[1];
00088     // send the DS1631 Stop Convert command
00089     buffer[0] = DS1631_CMD_STOPCONV;
00090     i2cMasterSendNI(i2cAddr, 1, buffer);
00091 }
00092 
00093 s16 ds1631ReadTemp(u08 i2cAddr)
00094 {
00095     // read the Temperature register and return the result
00096     return ds1631ReadTempReg(i2cAddr, DS1631_CMD_READTEMP);
00097 }
00098 
00099 void ds1631SetTH(u08 i2cAddr, s16 value)
00100 {
00101     // write the TH register
00102     ds1631WriteTempReg(i2cAddr, DS1631_CMD_ACCESSTH, value);
00103 }
00104 
00105 void ds1631SetTL(u08 i2cAddr, s16 value)
00106 {
00107     // write the TL register
00108     ds1631WriteTempReg(i2cAddr, DS1631_CMD_ACCESSTL, value);
00109 }
00110 
00111 s16 ds1631GetTH(u08 i2cAddr)
00112 {
00113     // read the TH register and return the result
00114     return ds1631ReadTempReg(i2cAddr, DS1631_CMD_ACCESSTH);
00115 }
00116 
00117 s16 ds1631GetTL(u08 i2cAddr)
00118 {
00119     // read the TL register and return the result
00120     return ds1631ReadTempReg(i2cAddr, DS1631_CMD_ACCESSTL);
00121 }
00122 
00123 
00124 s16 ds1631ReadTempReg(u08 i2cAddr, u08 cmd)
00125 {
00126     u08 buffer[2];
00127     s16 T;
00128 
00129     // read the temperature value from the requested register
00130     i2cMasterSendNI(i2cAddr, 1, &cmd);
00131     i2cMasterReceiveNI(i2cAddr, 2, buffer);
00132     // pack bytes
00133     T = (s16)((buffer[0]<<8) | buffer[1]);
00134     // return result
00135     return T;
00136 }
00137 
00138 void ds1631WriteTempReg(u08 i2cAddr, u08 cmd, s16 value)
00139 {
00140     u08 buffer[3];
00141 
00142     // write the requested register with a temperature value
00143     buffer[0] = cmd;
00144     buffer[1] = value>>8;
00145     buffer[2] = value;
00146     i2cMasterSendNI(i2cAddr, 3, buffer);
00147 }

Generated on Tue Sep 20 03:11:42 2005 for Procyon AVRlib by  doxygen 1.4.2