00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 00030 #include "pic24_all.h" 00031 #include <stdio.h> 00032 00039 #define DS1631ADDR 0x90 //DS1631 address with all pins tied low 00040 #define ACCESS_CONFIG 0xAC 00041 #define START_CONVERT 0x51 00042 #define READ_TEMP 0xAA 00043 00044 void writeConfigDS1631(uint8 u8_i) { 00045 write2I2C1(DS1631ADDR, ACCESS_CONFIG, u8_i); 00046 } 00047 00048 void startConversionDS1631() { 00049 write1I2C1(DS1631ADDR, START_CONVERT); 00050 } 00051 00052 int16 readTempDS161() { 00053 uint8 u8_lo, u8_hi; 00054 int16 i16_temp; 00055 write1I2C1(DS1631ADDR, READ_TEMP); 00056 read2I2C1 (DS1631ADDR, &u8_hi, &u8_lo); 00057 i16_temp = u8_hi; 00058 return ((i16_temp<<8)|u8_lo); 00059 } 00060 00061 int main (void) { 00062 int16 i16_temp; 00063 float f_tempC,f_tempF; 00064 configBasic(HELLO_MSG); 00065 configI2C1(400); //configure I2C for 400 KHz 00066 writeConfigDS1631(0x0C); //continuous conversion, 12-bit mode 00067 startConversionDS1631(); //start conversions 00068 while (1) { 00069 DELAY_MS(750); 00070 i16_temp = readTempDS161(); 00071 f_tempC = i16_temp; //convert to floating point 00072 f_tempC = f_tempC/256; //divide by precision 00073 f_tempF = f_tempC*9/5 + 32; 00074 printf("Temp is: 0x%0X, %4.4f (C), %4.4f (F)\n", i16_temp, (double) f_tempC, (double) f_tempF); 00075 } 00076 00077 }