STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/I2C/I2C_TSENSOR/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file I2C/I2C_TSENSOR/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 00029 /* Includes ------------------------------------------------------------------*/ 00030 #include "main.h" 00031 00032 /** @addtogroup STM32F0xx_StdPeriph_Examples 00033 * @{ 00034 */ 00035 00036 /** @addtogroup I2C_TSENSOR 00037 * @{ 00038 */ 00039 00040 /* Private typedef -----------------------------------------------------------*/ 00041 /* Private define ------------------------------------------------------------*/ 00042 #define TEMPERATURE_THYS 31 00043 #define TEMPERATURE_TOS 32 00044 00045 /* Private macro -------------------------------------------------------------*/ 00046 /* Private variables ---------------------------------------------------------*/ 00047 static uint8_t TempCelsiusDisplay[] = " +abc.d C "; 00048 static uint8_t TempFahrenheitDisplay[] = " +abc.d F "; 00049 static int32_t TempValue = 0, TempValueCelsius = 0, TempValueFahrenheit = 0; 00050 __IO uint8_t SMbusAlertOccurred = 0; 00051 00052 /* Private function prototypes -----------------------------------------------*/ 00053 static void NVIC_Config(void); 00054 00055 /* Private functions ---------------------------------------------------------*/ 00056 00057 /** 00058 * @brief Main program. 00059 * @param None 00060 * @retval None 00061 */ 00062 int main(void) 00063 { 00064 /*!< At this stage the microcontroller clock setting is already configured, 00065 this is done through SystemInit() function which is called from startup 00066 file (startup_stm32f0xx.s) before to branch to application main. 00067 To reconfigure the default setting of SystemInit() function, refer to 00068 system_stm32f0xx.c file 00069 */ 00070 00071 uint32_t index = 0; 00072 00073 /* NVIC Configuration */ 00074 NVIC_Config(); 00075 00076 /* Initialize the LCD */ 00077 #ifdef USE_STM320518_EVAL 00078 STM320518_LCD_Init(); 00079 #else 00080 STM32072B_LCD_Init(); 00081 #endif /* USE_STM320518_EVAL */ 00082 00083 /* Initialize the Temperature Sensor */ 00084 LM75_Init(); 00085 00086 if (LM75_GetStatus() == SUCCESS) 00087 { 00088 /* Clear the LCD */ 00089 LCD_Clear(LCD_COLOR_WHITE); 00090 00091 /* Set the Back Color */ 00092 LCD_SetBackColor(LCD_COLOR_BLUE); 00093 /* Set the Text Color */ 00094 LCD_SetTextColor(LCD_COLOR_GREEN); 00095 00096 LCD_DisplayStringLine(LCD_LINE_0, " Temperature "); 00097 00098 /* Configure the Temperature sensor device STLM75: 00099 - Thermostat mode Interrupt 00100 - Fault tolerance: 00 00101 */ 00102 LM75_WriteConfReg(0x02); 00103 00104 /* Configure the THYS and TOS in order to use the SMbus alert interrupt */ 00105 LM75_WriteReg(LM75_REG_THYS, TEMPERATURE_THYS << 8); /*31�C*/ 00106 LM75_WriteReg(LM75_REG_TOS, TEMPERATURE_TOS << 8); /*32�C*/ 00107 00108 /* Enables the I2C SMBus Alert feature */ 00109 I2C_SMBusAlertCmd(LM75_I2C, ENABLE); 00110 I2C_ClearFlag(LM75_I2C, I2C_FLAG_ALERT); 00111 00112 SMbusAlertOccurred = 0; 00113 00114 /* Enable SMBus Alert interrupt */ 00115 I2C_ITConfig(LM75_I2C, I2C_IT_ERRI, ENABLE); 00116 00117 /* Infinite Loop */ 00118 while (1) 00119 { 00120 /* Get double of Temperature value */ 00121 TempValue = LM75_ReadTemp(); 00122 00123 if (TempValue <= 256) 00124 { 00125 /* Positive temperature measured */ 00126 TempCelsiusDisplay[7] = '+'; 00127 /* Initialize the temperature sensor value*/ 00128 TempValueCelsius = TempValue; 00129 } 00130 else 00131 { 00132 /* Negative temperature measured */ 00133 TempCelsiusDisplay[7] = '-'; 00134 /* Remove temperature value sign */ 00135 TempValueCelsius = 0x200 - TempValue; 00136 } 00137 00138 /* Calculate temperature digits in �C */ 00139 if ((TempValueCelsius & 0x01) == 0x01) 00140 { 00141 TempCelsiusDisplay[12] = 0x05 + 0x30; 00142 TempFahrenheitDisplay[12] = 0x05 + 0x30; 00143 } 00144 else 00145 { 00146 TempCelsiusDisplay[12] = 0x00 + 0x30; 00147 TempFahrenheitDisplay[12] = 0x00 + 0x30; 00148 } 00149 00150 TempValueCelsius >>= 1; 00151 00152 TempCelsiusDisplay[8] = (TempValueCelsius / 100) + 0x30; 00153 TempCelsiusDisplay[9] = ((TempValueCelsius % 100) / 10) + 0x30; 00154 TempCelsiusDisplay[10] = ((TempValueCelsius % 100) % 10) + 0x30; 00155 00156 if (TempValue > 256) 00157 { 00158 if (((9 * TempValueCelsius) / 5) <= 32) 00159 { 00160 /* Convert temperature �C to Fahrenheit */ 00161 TempValueFahrenheit = abs (32 - ((9 * TempValueCelsius) / 5)); 00162 /* Calculate temperature digits in �F */ 00163 TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30; 00164 TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30; 00165 TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30; 00166 /* Positive temperature measured */ 00167 TempFahrenheitDisplay[7] = '+'; 00168 } 00169 else 00170 { 00171 /* Convert temperature �C to Fahrenheit */ 00172 TempValueFahrenheit = abs(((9 * TempValueCelsius) / 5) - 32); 00173 /* Calculate temperature digits in �F */ 00174 TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30; 00175 TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30; 00176 TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30; 00177 /* Negative temperature measured */ 00178 TempFahrenheitDisplay[7] = '-'; 00179 } 00180 } 00181 else 00182 { 00183 /* Convert temperature �C to Fahrenheit */ 00184 TempValueFahrenheit = ((9 * TempValueCelsius) / 5) + 32; 00185 00186 /* Calculate temperature digits in �F */ 00187 TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30; 00188 TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30; 00189 TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30; 00190 00191 /* Positive temperature measured */ 00192 TempFahrenheitDisplay[7] = '+'; 00193 } 00194 00195 /* Display Fahrenheit value on LCD */ 00196 for (index = 0; index < 20; index++) 00197 { 00198 LCD_DisplayChar(LCD_LINE_6, (319 - (16 * index)), TempCelsiusDisplay[index]); 00199 00200 LCD_DisplayChar(LCD_LINE_7, (319 - (16 * index)), TempFahrenheitDisplay[index]); 00201 } 00202 00203 if (SMbusAlertOccurred == 1) 00204 { 00205 /* Set the Back Color */ 00206 LCD_SetBackColor(LCD_COLOR_BLUE); 00207 /* Set the Text Color */ 00208 LCD_SetTextColor(LCD_COLOR_RED); 00209 LCD_DisplayStringLine(LCD_LINE_2, "Warning: Temp exceed"); 00210 LCD_DisplayStringLine(LCD_LINE_3, " 32 C "); 00211 } 00212 if (SMbusAlertOccurred == 2) 00213 { 00214 /* Set the Back Color */ 00215 LCD_SetBackColor(LCD_COLOR_WHITE); 00216 /* Set the Text Color */ 00217 LCD_SetTextColor(LCD_COLOR_WHITE); 00218 LCD_ClearLine(LCD_LINE_2); 00219 LCD_ClearLine(LCD_LINE_3); 00220 SMbusAlertOccurred = 0; 00221 /* Set the Back Color */ 00222 LCD_SetBackColor(LCD_COLOR_BLUE); 00223 /* Set the Text Color */ 00224 LCD_SetTextColor(LCD_COLOR_GREEN); 00225 } 00226 } 00227 } 00228 else 00229 { 00230 LCD_Clear(LCD_COLOR_WHITE); 00231 LCD_DisplayStringLine(LCD_LINE_2, " LM75 not correctly "); 00232 LCD_DisplayStringLine(LCD_LINE_3, " initialized... "); 00233 LCD_DisplayStringLine(LCD_LINE_4, " Please restart the "); 00234 LCD_DisplayStringLine(LCD_LINE_5, " example. "); 00235 /* Infinite Loop */ 00236 while(1) 00237 { 00238 } 00239 } 00240 } 00241 00242 /** 00243 * @brief Configures the different interrupt. 00244 * @param None 00245 * @retval None 00246 */ 00247 static void NVIC_Config(void) 00248 { 00249 NVIC_InitTypeDef NVIC_InitStructure; 00250 00251 /* Reconfigure and enable I2C1 error interrupt to have the higher priority */ 00252 NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn; 00253 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00254 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00255 NVIC_Init(&NVIC_InitStructure); 00256 } 00257 00258 #ifdef USE_FULL_ASSERT 00259 00260 /** 00261 * @brief Reports the name of the source file and the source line number 00262 * where the assert_param error has occurred. 00263 * @param file: pointer to the source file name 00264 * @param line: assert_param error line source number 00265 * @retval None 00266 */ 00267 void assert_failed(uint8_t* file, uint32_t line) 00268 { 00269 /* User can add his own implementation to report the file name and line number, 00270 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00271 00272 /* Infinite loop */ 00273 while (1) 00274 { 00275 } 00276 } 00277 #endif 00278 00279 /** 00280 * @} 00281 */ 00282 00283 /** 00284 * @} 00285 */ 00286 00287 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/