00001 /********************************************************************* 00002 * 00003 * Thermo Sensor with SPI(TM) Interface 00004 * 00005 ********************************************************************* 00006 * FileName: temperature.c 00007 * Dependencies: See INCLUDES section below 00008 * Processor: PIC18 00009 * Compiler: C18 2.30.01+ 00010 * Company: Microchip Technology, Inc. 00011 * 00012 * Software License Agreement 00013 * 00014 * The software supplied herewith by Microchip Technology Incorporated 00015 * (the “Company”) for its PICmicro® Microcontroller is intended and 00016 * supplied to you, the Company’s customer, for use solely and 00017 * exclusively on Microchip PICmicro Microcontroller products. The 00018 * software is owned by the Company and/or its supplier, and is 00019 * protected under applicable copyright laws. All rights are reserved. 00020 * Any use in violation of the foregoing restrictions may subject the 00021 * user to criminal sanctions under applicable laws, as well as to 00022 * civil liability for the breach of the terms and conditions of this 00023 * license. 00024 * 00025 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, 00026 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 00027 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00028 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 00029 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 00030 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 00031 * 00032 * Author Date Comment 00033 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00034 * Rawin Rojvanit 11/19/04 Original. 00035 ********************************************************************/ 00036 00037 /** I N C L U D E S **********************************************************/ 00038 #include <p18cxxx.h> 00039 #include "system\typedefs.h" 00040 #include <spi.h> 00041 #include "io_cfg.h" 00042 #include "user\temperature.h" 00043 00044 /** V A R I A B L E S ********************************************************/ 00045 #pragma udata 00046 WORD temperature; // Raw data format 00047 char tempString[10]; // Buffer for storing data in ASCII format 00048 00049 /** P R I V A T E P R O T O T Y P E S ***************************************/ 00050 00051 /** D E C L A R A T I O N S **************************************************/ 00052 #pragma code 00053 /****************************************************************************** 00054 * Function: void InitTempSensor(void) 00055 * 00056 * PreCondition: None 00057 * 00058 * Input: None 00059 * 00060 * Output: None 00061 * 00062 * Side Effects: None 00063 * 00064 * Overview: Initializes SPI interface & chip select line 00065 * 00066 * Note: None 00067 *****************************************************************************/ 00068 void InitTempSensor(void) 00069 { 00070 cs_temp_sensor = 1; 00071 tris_cs_temp_sensor = OUTPUT_PIN; 00072 OpenSPI(SPI_FOSC_64, MODE_11, SMPMID); 00073 00074 // Initialize readable values - default to room temperature 00075 temperature._word = 0x0C87; // 25 degree celsius 00076 UpdateCelsiusASCII(); 00077 00078 }//end InitTempSensor 00079 00080 /****************************************************************************** 00081 * Function: void AcquireTemperature(void) 00082 * 00083 * PreCondition: None 00084 * 00085 * Input: None 00086 * 00087 * Output: None 00088 * 00089 * Side Effects: None 00090 * 00091 * Overview: None 00092 * 00093 * Note: None 00094 *****************************************************************************/ 00095 BOOL AcquireTemperature(void) 00096 { 00097 cs_temp_sensor = 0; 00098 MSB(temperature) = ReadSPI(); 00099 LSB(temperature) = ReadSPI(); 00100 cs_temp_sensor = 1; 00101 00102 if(temperature.LowB.b2 == 0) 00103 return FAIL; 00104 00105 return OK; 00106 }//end AcquireTemperature 00107 00108 /****************************************************************************** 00109 * Function: void UpdateCelsiusASCII(void) 00110 * 00111 * PreCondition: None 00112 * 00113 * Input: None 00114 * 00115 * Output: None 00116 * 00117 * Side Effects: None 00118 * 00119 * Overview: This routine converts data output to ASCII string 00120 * 00121 * Note: None 00122 *****************************************************************************/ 00123 void UpdateCelsiusASCII(void) 00124 { 00125 WORD temp; 00126 byte i; 00127 00128 temp._word = temperature._word >> 3; 00129 00130 if(temp.HighB.b4 == 0) 00131 { 00132 temp.HighB._byte &= 0b00011111; 00133 tempString[0] = '+'; 00134 } 00135 else 00136 { 00137 temp.HighB._byte |= 0b11100000; 00138 tempString[0] = '-'; 00139 temp._word = temp._word ^ 0xFFFF; // Negate 00140 temp._word++; 00141 }//end if 00142 00143 temp._word = (temp._word*10U) >> 4; // Turn into celsius xx.x 00144 00145 /* Populate string */ 00146 for(i=4;i>0;i--) 00147 { 00148 tempString[i] = (((char)(temp._word % 10)) & 0x0F) | 0x30; 00149 temp._word /= 10; 00150 }//end for 00151 00152 /* Turn leading zeros into spaces */ 00153 if(tempString[1] == '0') 00154 { 00155 tempString[1] = ' '; 00156 if(tempString[2] == '0') 00157 tempString[2] = ' '; 00158 }//end if 00159 00160 /* Adjust decimal digit */ 00161 tempString[5] = tempString[4]; 00162 tempString[4]='.'; 00163 tempString[6]=0xF8; // Degree symbol 00164 tempString[7]='C'; 00165 tempString[8]='\r'; 00166 tempString[9]=0x00; // Null-Terminated 00167 00168 }//end UpdateCelsiusASCII 00169 00170 /** EOF temperature.c ********************************************************/