PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ds1631_i2c.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 #include <stdio.h>
32 
33 /** \file
34 I2C Example: Demonstrates I2C functions by reading/writing
35 from a Maxim DS1631 Digital Thermometer.
36 
37 */
38 
39 #define DS1631ADDR 0x90 //DS1631 address with all pins tied low
40 #define ACCESS_CONFIG 0xAC
41 #define START_CONVERT 0x51
42 #define READ_TEMP 0xAA
43 
44 void writeConfigDS1631(uint8_t u8_i) {
45  write2I2C1(DS1631ADDR, ACCESS_CONFIG, u8_i);
46 }
47 
48 void startConversionDS1631() {
49  write1I2C1(DS1631ADDR, START_CONVERT);
50 }
51 
52 int16_t readTempDS1631() {
53  uint8_t u8_lo, u8_hi;
54  int16_t i16_temp;
55  write1I2C1(DS1631ADDR, READ_TEMP);
56  read2I2C1 (DS1631ADDR, &u8_hi, &u8_lo);
57  i16_temp = u8_hi;
58  return ((i16_temp<<8)|u8_lo);
59 }
60 
61 int main (void) {
62  int16_t i16_temp;
63  float f_tempC,f_tempF;
64  configBasic(HELLO_MSG);
65  configI2C1(400); //configure I2C for 400 KHz
66  writeConfigDS1631(0x0C); //continuous conversion, 12-bit mode
67  startConversionDS1631(); //start conversions
68  while (1) {
69  DELAY_MS(750);
70  i16_temp = readTempDS1631();
71  f_tempC = i16_temp; //convert to floating point
72  f_tempC = f_tempC/256; //divide by precision
73  f_tempF = f_tempC*9/5 + 32;
74 #ifdef SMALLRAM
75  {
76  //use integers to avoid floating point printf which does not fit in this data space
77  int16_t i16_tempC,i16_tempF;
78  i16_tempC = f_tempC;
79  i16_tempF = f_tempF;
80  printf("Temp is: 0x%0X, %d (C), %d (F)\n", i16_temp, i16_tempC, i16_tempF);
81  }
82 
83 #else
84  printf("Temp is: 0x%0X, %4.4f (C), %4.4f (F)\n", i16_temp, (double) f_tempC, (double) f_tempF);
85 #endif
86  }
87 
88 }