PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ds1722_spi_tempsense.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  * SPI example: PIC24 uC in Master mode to Maxim DS1722 Digital Thermometer
35 */
36 
37 #define CONFIG_SLAVE_ENABLE() CONFIG_RB3_AS_DIG_OUTPUT()
38 #define SLAVE_ENABLE() _LATB3 = 1 //high true assertion
39 #define SLAVE_DISABLE() _LATB3 = 0
40 
41 void configSPI1(void) {
42  //spi clock = 40MHz/1*4 = 40MHz/4 = 10MHz
43  SPI1CON1 = SEC_PRESCAL_1_1 | //1:1 secondary prescale
44  PRI_PRESCAL_4_1 | //4:1 primary prescale
45  CLK_POL_ACTIVE_HIGH | //clock active high (CKP = 0)
46  SPI_CKE_OFF | //out changes inactive to active (CKE=0)
47  SPI_MODE8_ON | //8-bit mode
48  MASTER_ENABLE_ON; //master mode
49 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
50  //nothing to do here. On this family, the SPI1 port uses dedicated
51  //pins for higher speed. The SPI2 port can be used with remappable pins.
52 #else
53 //all other families (PIC24H/PIC24F/dsPIC33F)
54  CONFIG_SDO1_TO_RP(6); //use RP6 for SDO
55  CONFIG_RP6_AS_DIG_PIN(); //Ensure that analog is disabled
56  CONFIG_SCK1OUT_TO_RP(7); //use RP7 for SCLK
57  CONFIG_RP7_AS_DIG_PIN(); //Ensure that analog is disabled
58  CONFIG_SDI1_TO_RP(5); //use RP5 for SDI
59  CONFIG_RP5_AS_DIG_PIN(); //Ensure that analog is disabled
60 #endif
61  CONFIG_SLAVE_ENABLE(); //chip select for MCP41xxx
62  SLAVE_DISABLE(); //disable the chip select
63  SPI1STATbits.SPIEN = 1; //enable SPI mode
64 }
65 
66 void writeConfigDS1722(uint8_t u8_i) {
67  SLAVE_ENABLE(); //assert chipselect
68  ioMasterSPI1(0x80); //config address
69  ioMasterSPI1(u8_i); //config value
70  SLAVE_DISABLE();
71 }
72 
73 int16_t readTempDS1722() {
74  uint16_t u16_lo, u16_hi;
75  SLAVE_ENABLE(); //assert chipselect
76  ioMasterSPI1(0x01); //LSB address
77  u16_lo = ioMasterSPI1(0x00); //read LSByte
78  u16_hi = ioMasterSPI1(0x00); //read MSByte
79  SLAVE_DISABLE();
80  return((u16_hi<<8) | u16_lo);
81 }
82 
83 int main (void) {
84  int16_t i16_temp;
85  float f_tempC,f_tempF;
86  configBasic(HELLO_MSG);
87  configSPI1();
88  writeConfigDS1722(0xE8); //10-bit mode
89  while (1) {
90  DELAY_MS(1500);
91  i16_temp = readTempDS1722();
92  f_tempC = i16_temp; //convert to floating point
93  f_tempC = f_tempC/256; //divide by precision
94  f_tempF = f_tempC*9/5 + 32;
95 #ifdef SMALLRAM
96  {
97  //use integers to avoid floating point printf which does not fit in this data space
98  int16_t i16_tempC,i16_tempF;
99  i16_tempC = f_tempC;
100  i16_tempF = f_tempF;
101  printf("Temp is: 0x%0X, %d (C), %d (F)\n", i16_temp, i16_tempC, i16_tempF);
102  }
103 #else
104  printf("Temp is: 0x%0X, %4.4f (C), %4.4f (F)\n", i16_temp, (double) f_tempC, (double) f_tempF);
105 #endif
106  }
107 }