| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file ds1631.c \brief Dallas DS1631 Temperature Sensor Driver Library. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'ds1631.c' |
||
| 5 | // Title : Dallas DS1631 Temperature Sensor Driver Library |
||
| 6 | // Author : Pascal Stang - Copyright (C) 2004 |
||
| 7 | // Created : 2004.02.10 |
||
| 8 | // Revised : 2004.02.19 |
||
| 9 | // Version : 0.1 |
||
| 10 | // Target MCU : Atmel AVR Series |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
| 14 | // to be lacking in some functionality or documentation, or may not be fully |
||
| 15 | // tested. Nonetheless, you can expect most functions to work. |
||
| 16 | // |
||
| 17 | // This code is distributed under the GNU Public License |
||
| 18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
| 19 | // |
||
| 20 | //***************************************************************************** |
||
| 21 | |||
| 22 | #include <avr/io.h> |
||
| 23 | #include <avr/interrupt.h> |
||
| 24 | |||
| 25 | #include "global.h" |
||
| 26 | #include "timer.h" |
||
| 27 | #include "i2c.h" |
||
| 28 | #include "ds1631.h" |
||
| 29 | |||
| 30 | // global variables |
||
| 31 | |||
| 32 | // Functions |
||
| 33 | u08 ds1631Init(u08 i2cAddr) |
||
| 34 | { |
||
| 35 | u08 chip_ok; |
||
| 36 | // issue a reset |
||
| 37 | if(ds1631Reset(i2cAddr) == I2C_OK) |
||
| 38 | chip_ok = TRUE; |
||
| 39 | else |
||
| 40 | chip_ok = FALSE; |
||
| 41 | // set a default configuration |
||
| 42 | // (1-shot mode, T_OUT active high, and 12-bit conversion) |
||
| 43 | ds1631SetConfig(i2cAddr, |
||
| 44 | DS1631_CONFIG_1SHOT | DS1631_CONFIG_POL | |
||
| 45 | DS1631_CONFIG_R0 | DS1631_CONFIG_R1); |
||
| 46 | return chip_ok; |
||
| 47 | } |
||
| 48 | |||
| 49 | u08 ds1631Reset(u08 i2cAddr) |
||
| 50 | { |
||
| 51 | u08 buffer[1]; |
||
| 52 | // return the DS1631 to power-on reset defaults |
||
| 53 | buffer[0] = DS1631_CMD_SWPOR; |
||
| 54 | return i2cMasterSendNI(i2cAddr, 1, buffer); |
||
| 55 | } |
||
| 56 | |||
| 57 | void ds1631SetConfig(u08 i2cAddr, u08 config) |
||
| 58 | { |
||
| 59 | u08 buffer[2]; |
||
| 60 | // write the DS1631 configuration byte |
||
| 61 | buffer[0] = DS1631_CMD_ACCESSCONFIG; |
||
| 62 | buffer[1] = config; |
||
| 63 | i2cMasterSendNI(i2cAddr, 2, buffer); |
||
| 64 | } |
||
| 65 | |||
| 66 | u08 ds1631GetConfig(u08 i2cAddr) |
||
| 67 | { |
||
| 68 | u08 buffer[1]; |
||
| 69 | // write the DS1631 configuration byte |
||
| 70 | buffer[0] = DS1631_CMD_ACCESSCONFIG; |
||
| 71 | i2cMasterSendNI(i2cAddr, 2, buffer); |
||
| 72 | i2cMasterReceiveNI(i2cAddr, 2, buffer); |
||
| 73 | return buffer[0]; |
||
| 74 | } |
||
| 75 | |||
| 76 | void ds1631StartConvert(u08 i2cAddr) |
||
| 77 | { |
||
| 78 | u08 buffer[1]; |
||
| 79 | // send the DS1631 Start Convert command |
||
| 80 | buffer[0] = DS1631_CMD_STARTCONV; |
||
| 81 | i2cMasterSendNI(i2cAddr, 1, buffer); |
||
| 82 | } |
||
| 83 | |||
| 84 | void ds1631StopConvert(u08 i2cAddr) |
||
| 85 | { |
||
| 86 | u08 buffer[1]; |
||
| 87 | // send the DS1631 Stop Convert command |
||
| 88 | buffer[0] = DS1631_CMD_STOPCONV; |
||
| 89 | i2cMasterSendNI(i2cAddr, 1, buffer); |
||
| 90 | } |
||
| 91 | |||
| 92 | s16 ds1631ReadTemp(u08 i2cAddr) |
||
| 93 | { |
||
| 94 | // read the Temperature register and return the result |
||
| 95 | return ds1631ReadTempReg(i2cAddr, DS1631_CMD_READTEMP); |
||
| 96 | } |
||
| 97 | |||
| 98 | void ds1631SetTH(u08 i2cAddr, s16 value) |
||
| 99 | { |
||
| 100 | // write the TH register |
||
| 101 | ds1631WriteTempReg(i2cAddr, DS1631_CMD_ACCESSTH, value); |
||
| 102 | } |
||
| 103 | |||
| 104 | void ds1631SetTL(u08 i2cAddr, s16 value) |
||
| 105 | { |
||
| 106 | // write the TL register |
||
| 107 | ds1631WriteTempReg(i2cAddr, DS1631_CMD_ACCESSTL, value); |
||
| 108 | } |
||
| 109 | |||
| 110 | s16 ds1631GetTH(u08 i2cAddr) |
||
| 111 | { |
||
| 112 | // read the TH register and return the result |
||
| 113 | return ds1631ReadTempReg(i2cAddr, DS1631_CMD_ACCESSTH); |
||
| 114 | } |
||
| 115 | |||
| 116 | s16 ds1631GetTL(u08 i2cAddr) |
||
| 117 | { |
||
| 118 | // read the TL register and return the result |
||
| 119 | return ds1631ReadTempReg(i2cAddr, DS1631_CMD_ACCESSTL); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | s16 ds1631ReadTempReg(u08 i2cAddr, u08 cmd) |
||
| 124 | { |
||
| 125 | u08 buffer[2]; |
||
| 126 | s16 T; |
||
| 127 | |||
| 128 | // read the temperature value from the requested register |
||
| 129 | i2cMasterSendNI(i2cAddr, 1, &cmd); |
||
| 130 | i2cMasterReceiveNI(i2cAddr, 2, buffer); |
||
| 131 | // pack bytes |
||
| 132 | T = (s16)((buffer[0]<<8) | buffer[1]); |
||
| 133 | // return result |
||
| 134 | return T; |
||
| 135 | } |
||
| 136 | |||
| 137 | void ds1631WriteTempReg(u08 i2cAddr, u08 cmd, s16 value) |
||
| 138 | { |
||
| 139 | u08 buffer[3]; |
||
| 140 | |||
| 141 | // write the requested register with a temperature value |
||
| 142 | buffer[0] = cmd; |
||
| 143 | buffer[1] = value>>8; |
||
| 144 | buffer[2] = value; |
||
| 145 | i2cMasterSendNI(i2cAddr, 3, buffer); |
||
| 146 | } |
Powered by WebSVN v2.8.3