STM32F10x_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 V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 #include <stdlib.h>
00024 #include "stm32_eval_i2c_tsensor.h"
00025 #include "stm32_eval.h"
00026 
00027 #ifdef USE_STM32100E_EVAL
00028   #include "stm32100e_eval_lcd.h"
00029 #elif defined USE_STM3210E_EVAL
00030   #include "stm3210e_eval_lcd.h"
00031 #elif defined USE_STM32100B_EVAL
00032   #include "stm32100b_eval_lcd.h"
00033 #elif defined USE_STM3210B_EVAL
00034   #include "stm3210b_eval_lcd.h"
00035 #endif
00036 
00037 /** @addtogroup STM32F10x_StdPeriph_Examples
00038   * @{
00039   */
00040 
00041 /** @addtogroup I2C_TSENSOR
00042   * @{
00043   */ 
00044 
00045 /* Private typedef -----------------------------------------------------------*/
00046 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00047 
00048 /* Private define ------------------------------------------------------------*/
00049 #define TEMPERATURE_THYS    31
00050 #define TEMPERATURE_TOS     32
00051 
00052 /* Private macro -------------------------------------------------------------*/
00053 /* Private variables ---------------------------------------------------------*/
00054 static uint8_t TempCelsiusDisplay[]     = "       +abc.d C     ";
00055 static uint8_t TempFahrenheitDisplay[]  = "       +abc.d F     ";
00056 static int32_t TempValue = 0, TempValueCelsius = 0, TempValueFahrenheit = 0;
00057 __IO uint8_t SMbusAlertOccurred = 0; 
00058 uint32_t index = 0;
00059       
00060 /* Private functions ---------------------------------------------------------*/
00061 void NVIC_Configuration(void);
00062 
00063 /**
00064   * @brief   Main program
00065   * @param  None
00066   * @retval None
00067   */
00068 int main(void)
00069 {
00070   /*!< At this stage the microcontroller clock setting is already configured, 
00071        this is done through SystemInit() function which is called from startup
00072        file (startup_stm32f10x_xx.s) before to branch to application main.
00073        To reconfigure the default setting of SystemInit() function, refer to
00074        system_stm32f10x.c file
00075      */
00076  
00077   /* NVIC Configuration */
00078   NVIC_Configuration();
00079 
00080   /* Initialize the LCD */
00081 #ifdef USE_STM32100E_EVAL
00082   STM32100E_LCD_Init();
00083 #elif defined USE_STM3210E_EVAL
00084   STM3210E_LCD_Init();
00085 #elif defined USE_STM32100B_EVAL
00086   STM32100B_LCD_Init();
00087 #elif defined USE_STM3210B_EVAL
00088   STM3210B_LCD_Init();
00089 #endif
00090 
00091   
00092 #ifdef USE_STM3210E_EVAL
00093   /* Disable FSMC only for STM32 High-density and XL-density devices */
00094   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
00095 #endif /* USE_STM3210E_EVAL */
00096   
00097   /* Initialize the Temperature Sensor */
00098   LM75_Init();
00099 
00100   if (LM75_GetStatus() == SUCCESS)
00101   {    
00102 #ifdef USE_STM3210E_EVAL
00103     /* Enable FSMC */
00104     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
00105 #endif /* USE_STM3210E_EVAL */
00106     
00107     /* Clear the LCD */
00108     LCD_Clear(LCD_COLOR_WHITE);
00109     
00110     /* Set the Back Color */
00111     LCD_SetBackColor(LCD_COLOR_BLUE);
00112     
00113     /* Set the Text Color */
00114     LCD_SetTextColor(LCD_COLOR_GREEN);
00115     
00116     LCD_DisplayStringLine(LCD_LINE_0, "     Temperature    ");
00117 
00118 #ifdef USE_STM3210E_EVAL        
00119     /* Disable FSMC */
00120     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
00121 
00122     /* Initialize the Temperature Sensor */
00123     LM75_Init();
00124     
00125 #endif /* USE_STM3210E_EVAL */
00126     
00127     /* Configure the Temperature sensor device STLM75:
00128     - Thermostat mode Interrupt
00129     - Fault tolerance: 00
00130     */
00131     LM75_WriteConfReg(0x02);
00132     
00133     /* Configure the THYS and TOS inorder to use the SMbus alert interrupt */
00134     LM75_WriteReg(LM75_REG_THYS, TEMPERATURE_THYS << 8);  /*31C*/
00135     LM75_WriteReg(LM75_REG_TOS, TEMPERATURE_TOS << 8);   /*32C*/
00136     
00137     I2C_ClearITPendingBit(LM75_I2C, I2C_IT_SMBALERT);
00138     
00139     SMbusAlertOccurred = 0;
00140     
00141     /* Infinite Loop */
00142     while (1)
00143     {
00144 #ifdef USE_STM3210E_EVAL        
00145     /* Disable FSMC */
00146     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
00147 
00148     /* Initialize the Temperature Sensor */
00149     LM75_Init();
00150 #endif /* USE_STM3210E_EVAL */
00151       
00152       /* Get double of Temperature value */
00153       TempValue = LM75_ReadTemp();
00154 
00155 #ifdef USE_STM3210E_EVAL          
00156       /* Enable FSMC */
00157       RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
00158 #endif /* USE_STM3210E_EVAL */
00159       
00160       if (TempValue <= 256)
00161       {
00162         /* Positive temperature measured */
00163         TempCelsiusDisplay[7] = '+';
00164         
00165         /* Initialize the temperature sensor value*/
00166         TempValueCelsius = TempValue;
00167       }
00168       else
00169       {
00170         /* Negative temperature measured */
00171         TempCelsiusDisplay[7] = '-';
00172         /* Remove temperature value sign */
00173         TempValueCelsius = 0x200 - TempValue;
00174       }
00175       
00176       /* Calculate temperature digits in C */
00177       if ((TempValueCelsius & 0x01) == 0x01)
00178       {
00179         TempCelsiusDisplay[12] = 0x05 + 0x30;
00180         TempFahrenheitDisplay[12] = 0x05 + 0x30;
00181       }
00182       else
00183       {
00184         TempCelsiusDisplay[12] = 0x00 + 0x30;
00185         TempFahrenheitDisplay[12] = 0x00 + 0x30;
00186       }
00187       
00188       TempValueCelsius >>= 1;
00189       
00190       TempCelsiusDisplay[8] = (TempValueCelsius / 100) + 0x30;
00191       TempCelsiusDisplay[9] = ((TempValueCelsius % 100) / 10) + 0x30;
00192       TempCelsiusDisplay[10] = ((TempValueCelsius % 100) % 10) + 0x30;
00193       
00194       if (TempValue > 256)
00195       {
00196         if (((9 * TempValueCelsius) / 5) <= 32)
00197         {
00198           /* Convert temperature C to Fahrenheit */
00199           TempValueFahrenheit = abs (32 - ((9 * TempValueCelsius) / 5));
00200           
00201           /* Calculate temperature digits in F */
00202           TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
00203           TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
00204           TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
00205           /* Positive temperature measured */
00206           TempFahrenheitDisplay[7] = '+';
00207         }
00208         else
00209         {
00210           /* Convert temperature C to Fahrenheit */
00211           TempValueFahrenheit = abs(((9 * TempValueCelsius) / 5) - 32);
00212           /* Calculate temperature digits in F */
00213           TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
00214           TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
00215           TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
00216           
00217           /* Negative temperature measured */
00218           TempFahrenheitDisplay[7] = '-';
00219         }
00220       }
00221       else
00222       {
00223         /* Convert temperature C to Fahrenheit */
00224         TempValueFahrenheit = ((9 * TempValueCelsius) / 5) + 32;
00225         
00226         /* Calculate temperature digits in F */
00227         TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
00228         TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
00229         TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
00230         
00231         /* Positive temperature measured */
00232         TempFahrenheitDisplay[7] = '+';
00233       }
00234       
00235       /* Display Fahrenheit value on LCD */
00236       for (index = 0; index < 20; index++)
00237       {
00238         LCD_DisplayChar(LCD_LINE_6, (319 - (16 * index)), TempCelsiusDisplay[index]);
00239         
00240         LCD_DisplayChar(LCD_LINE_7, (319 - (16 * index)), TempFahrenheitDisplay[index]);
00241       }
00242       
00243       if (SMbusAlertOccurred == 1)
00244       {
00245         /* Set the Back Color */
00246         LCD_SetBackColor(LCD_COLOR_BLUE);
00247         /* Set the Text Color */
00248         LCD_SetTextColor(LCD_COLOR_RED);
00249         LCD_DisplayStringLine(LCD_LINE_2, "Warning: Temp exceed");
00250         LCD_DisplayStringLine(LCD_LINE_3, "        32 C        ");
00251       }
00252       if (SMbusAlertOccurred == 2)
00253       {
00254         /* Set the Back Color */
00255         LCD_SetBackColor(LCD_COLOR_WHITE);
00256         /* Set the Text Color */
00257         LCD_SetTextColor(LCD_COLOR_WHITE);
00258         LCD_ClearLine(LCD_LINE_2);
00259         LCD_ClearLine(LCD_LINE_3);
00260         SMbusAlertOccurred = 0;
00261         /* Set the Back Color */
00262         LCD_SetBackColor(LCD_COLOR_BLUE);
00263         /* Set the Text Color */
00264         LCD_SetTextColor(LCD_COLOR_GREEN);
00265       }
00266     }
00267   }
00268   else
00269   {
00270     LCD_Clear(LCD_COLOR_WHITE);
00271     LCD_DisplayStringLine(LCD_LINE_2, " LM75 not correctly ");
00272     LCD_DisplayStringLine(LCD_LINE_3, " initilalized...    ");
00273     LCD_DisplayStringLine(LCD_LINE_4, " Please restart the ");
00274     LCD_DisplayStringLine(LCD_LINE_5, " example.           ");   
00275     /* Infinite Loop */
00276     while(1)
00277     {
00278     }
00279   }
00280 }
00281 
00282 /**
00283   * @brief  Configures the different interrupt.
00284   * @param  None
00285   * @retval None
00286   */
00287 void NVIC_Configuration(void)
00288 {
00289   NVIC_InitTypeDef NVIC_InitStructure;
00290   
00291   /* Re-configure and enable I2C2 error interrupt to have the higher priority */
00292 #ifdef USE_STM32100E_EVAL
00293   NVIC_InitStructure.NVIC_IRQChannel = I2C2_ER_IRQn;
00294 #else
00295   NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
00296 #endif
00297   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00298   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00299   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00300   NVIC_Init(&NVIC_InitStructure);
00301 }
00302 
00303 #ifdef  USE_FULL_ASSERT
00304 
00305 /**
00306   * @brief  Reports the name of the source file and the source line number
00307   *         where the assert_param error has occurred.
00308   * @param  file: pointer to the source file name
00309   * @param  line: assert_param error line source number
00310   * @retval None
00311   */
00312 void assert_failed(uint8_t* file, uint32_t line)
00313 { 
00314   /* User can add his own implementation to report the file name and line number,
00315      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00316 
00317   /* Infinite loop */
00318   while (1)
00319   {
00320   }
00321 }
00322 #endif
00323 
00324 /**
00325   * @}
00326   */ 
00327 
00328 /**
00329   * @}
00330   */ 
00331 
00332 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32