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 00037 #define CONFIG_SLAVE_ENABLE() CONFIG_RB3_AS_DIG_OUTPUT() 00038 #define SLAVE_ENABLE() _LATB3 = 1 //high true assertion 00039 #define SLAVE_DISABLE() _LATB3 = 0 00040 00041 void configSPI1(void) { 00042 //spi clock = 40MHz/1*4 = 40MHz/4 = 10MHz 00043 SPI1CON1 = SEC_PRESCAL_1_1 | //1:1 secondary prescale 00044 PRI_PRESCAL_4_1 | //4:1 primary prescale 00045 CLK_POL_ACTIVE_HIGH | //clock active high (CKP = 0) 00046 SPI_CKE_OFF | //out changes inactive to active (CKE=0) 00047 SPI_MODE8_ON | //8-bit mode 00048 MASTER_ENABLE_ON; //master mode 00049 //configure pins. Only need SDO, SCLK since POT is output only 00050 CONFIG_SDO1_TO_RP(6); //use RP6 for SDO 00051 CONFIG_RP6_AS_DIG_PIN(); //Ensure that analog is disabled 00052 CONFIG_SCK1OUT_TO_RP(7); //use RP7 for SCLK 00053 CONFIG_RP7_AS_DIG_PIN(); //Ensure that analog is disabled 00054 CONFIG_SDI1_TO_RP(5); //use RP5 for SDI 00055 CONFIG_RP5_AS_DIG_PIN(); //Ensure that analog is disabled 00056 CONFIG_SLAVE_ENABLE(); //chip select for MCP41xxx 00057 SLAVE_DISABLE(); //disable the chip select 00058 SPI1STATbits.SPIEN = 1; //enable SPI mode 00059 } 00060 00061 void writeConfigDS1722(uint8 u8_i) { 00062 SLAVE_ENABLE(); //assert chipselect 00063 ioMasterSPI1(0x80); //config address 00064 ioMasterSPI1(u8_i); //config value 00065 SLAVE_DISABLE(); 00066 } 00067 00068 int16 readTempDS1722() { 00069 uint16 u16_lo, u16_hi; 00070 SLAVE_ENABLE(); //assert chipselect 00071 ioMasterSPI1(0x01); //LSB address 00072 u16_lo = ioMasterSPI1(0x00); //read LSByte 00073 u16_hi = ioMasterSPI1(0x00); //read MSByte 00074 SLAVE_DISABLE(); 00075 return((u16_hi<<8) | u16_lo); 00076 } 00077 00078 int main (void) { 00079 int16 i16_temp; 00080 float f_tempC,f_tempF; 00081 configBasic(HELLO_MSG); 00082 configSPI1(); 00083 writeConfigDS1722(0xE8); //10-bit mode 00084 while (1) { 00085 DELAY_MS(1500); 00086 i16_temp = readTempDS1722(); 00087 f_tempC = i16_temp; //convert to floating point 00088 f_tempC = f_tempC/256; //divide by precision 00089 f_tempF = f_tempC*9/5 + 32; 00090 printf("Temp is: 0x%0X, %4.4f (C), %4.4f (F)\n", i16_temp, (double) f_tempC, (double) f_tempF); 00091 } 00092 }