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/interrupt.h>
    00024
    00025 #include "global.h"
    00026 #include "timer.h"
    00027 #include "i2c.h"
    00028 #include "ds1631.h"
    00029
    00030 // global variables
    00031
    00032 // Functions
    00033 u08 ds1631Init(u08 i2cAddr)
    00034 {
    00035     u08 chip_ok;
    00036     // issue a reset
    00037     if(ds1631Reset(i2cAddr) == I2C_OK)
    00038         chip_ok = TRUE;
    00039     else
    00040         chip_ok = FALSE;
    00041     // set a default configuration
    00042     // (1-shot mode, T_OUT active high, and 12-bit conversion)
    00043     ds1631SetConfig(i2cAddr,
    00044         DS1631_CONFIG_1SHOT | DS1631_CONFIG_POL |
    00045         DS1631_CONFIG_R0 | DS1631_CONFIG_R1);
    00046     return chip_ok;
    00047 }
    00048
    00049 u08 ds1631Reset(u08 i2cAddr)
    00050 {
    00051     u08 buffer[1];
    00052     // return the DS1631 to power-on reset defaults
    00053     buffer[0] = DS1631_CMD_SWPOR;
    00054     return i2cMasterSendNI(i2cAddr, 1, buffer);
    00055 }
    00056
    00057 void ds1631SetConfig(u08 i2cAddr, u08 config)
    00058 {
    00059     u08 buffer[2];
    00060     // write the DS1631 configuration byte
    00061     buffer[0] = DS1631_CMD_ACCESSCONFIG;
    00062     buffer[1] = config;
    00063     i2cMasterSendNI(i2cAddr, 2, buffer);
    00064 }
    00065
    00066 u08 ds1631GetConfig(u08 i2cAddr)
    00067 {
    00068     u08 buffer[1];
    00069     // write the DS1631 configuration byte
    00070     buffer[0] = DS1631_CMD_ACCESSCONFIG;
    00071     i2cMasterSendNI(i2cAddr, 2, buffer);
    00072     i2cMasterReceiveNI(i2cAddr, 2, buffer);
    00073     return buffer[0];
    00074 }
    00075
    00076 void ds1631StartConvert(u08 i2cAddr)
    00077 {
    00078     u08 buffer[1];
    00079     // send the DS1631 Start Convert command
    00080     buffer[0] = DS1631_CMD_STARTCONV;
    00081     i2cMasterSendNI(i2cAddr, 1, buffer);
    00082 }
    00083
    00084 void ds1631StopConvert(u08 i2cAddr)
    00085 {
    00086     u08 buffer[1];
    00087     // send the DS1631 Stop Convert command
    00088     buffer[0] = DS1631_CMD_STOPCONV;
    00089     i2cMasterSendNI(i2cAddr, 1, buffer);
    00090 }
    00091
    00092 s16 ds1631ReadTemp(u08 i2cAddr)
    00093 {
    00094     // read the Temperature register and return the result
    00095     return ds1631ReadTempReg(i2cAddr, DS1631_CMD_READTEMP);
    00096 }
    00097
    00098 void ds1631SetTH(u08 i2cAddr, s16 value)
    00099 {
    00100     // write the TH register
    00101     ds1631WriteTempReg(i2cAddr, DS1631_CMD_ACCESSTH, value);
    00102 }
    00103
    00104 void ds1631SetTL(u08 i2cAddr, s16 value)
    00105 {
    00106     // write the TL register
    00107     ds1631WriteTempReg(i2cAddr, DS1631_CMD_ACCESSTL, value);
    00108 }
    00109
    00110 s16 ds1631GetTH(u08 i2cAddr)
    00111 {
    00112     // read the TH register and return the result
    00113     return ds1631ReadTempReg(i2cAddr, DS1631_CMD_ACCESSTH);
    00114 }
    00115
    00116 s16 ds1631GetTL(u08 i2cAddr)
    00117 {
    00118     // read the TL register and return the result
    00119     return ds1631ReadTempReg(i2cAddr, DS1631_CMD_ACCESSTL);
    00120 }
    00121
    00122
    00123 s16 ds1631ReadTempReg(u08 i2cAddr, u08 cmd)
    00124 {
    00125     u08 buffer[2];
    00126     s16 T;
    00127
    00128     // read the temperature value from the requested register
    00129     i2cMasterSendNI(i2cAddr, 1, &cmd);
    00130     i2cMasterReceiveNI(i2cAddr, 2, buffer);
    00131     // pack bytes
    00132     T = (s16)((buffer[0]<<8) | buffer[1]);
    00133     // return result
    00134     return T;
    00135 }
    00136
    00137 void ds1631WriteTempReg(u08 i2cAddr, u08 cmd, s16 value)
    00138 {
    00139     u08 buffer[3];
    00140
    00141     // write the requested register with a temperature value
    00142     buffer[0] = cmd;
    00143     buffer[1] = value>>8;
    00144     buffer[2] = value;
    00145     i2cMasterSendNI(i2cAddr, 3, buffer);
    00146 }