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