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