comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/I2C/I2C_TSENSOR/main.c @ 0:c59513fd84fb

Initial commit of STM32 test code.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 03 Oct 2011 21:19:15 +1030
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c59513fd84fb
1 /**
2 ******************************************************************************
3 * @file I2C/I2C_TSENSOR/main.c
4 * @author MCD Application Team
5 * @version V3.5.0
6 * @date 08-April-2011
7 * @brief Main program body
8 ******************************************************************************
9 * @attention
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
20 */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x.h"
24 #include <stdlib.h>
25 #include "stm32_eval_i2c_tsensor.h"
26 #include "stm32_eval.h"
27
28 #ifdef USE_STM32100E_EVAL
29 #include "stm32100e_eval_lcd.h"
30 #elif defined USE_STM3210E_EVAL
31 #include "stm3210e_eval_lcd.h"
32 #elif defined USE_STM32100B_EVAL
33 #include "stm32100b_eval_lcd.h"
34 #elif defined USE_STM3210B_EVAL
35 #include "stm3210b_eval_lcd.h"
36 #endif
37
38 /** @addtogroup STM32F10x_StdPeriph_Examples
39 * @{
40 */
41
42 /** @addtogroup I2C_TSENSOR
43 * @{
44 */
45
46 /* Private typedef -----------------------------------------------------------*/
47 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
48
49 /* Private define ------------------------------------------------------------*/
50 #define TEMPERATURE_THYS 31
51 #define TEMPERATURE_TOS 32
52
53 /* Private macro -------------------------------------------------------------*/
54 /* Private variables ---------------------------------------------------------*/
55 static uint8_t TempCelsiusDisplay[] = " +abc.d C ";
56 static uint8_t TempFahrenheitDisplay[] = " +abc.d F ";
57 static int32_t TempValue = 0, TempValueCelsius = 0, TempValueFahrenheit = 0;
58 __IO uint8_t SMbusAlertOccurred = 0;
59 uint32_t index = 0;
60
61 /* Private functions ---------------------------------------------------------*/
62 void NVIC_Configuration(void);
63
64 /**
65 * @brief Main program
66 * @param None
67 * @retval None
68 */
69 int main(void)
70 {
71 /*!< At this stage the microcontroller clock setting is already configured,
72 this is done through SystemInit() function which is called from startup
73 file (startup_stm32f10x_xx.s) before to branch to application main.
74 To reconfigure the default setting of SystemInit() function, refer to
75 system_stm32f10x.c file
76 */
77
78 /* NVIC Configuration */
79 NVIC_Configuration();
80
81 /* Initialize the LCD */
82 #ifdef USE_STM32100E_EVAL
83 STM32100E_LCD_Init();
84 #elif defined USE_STM3210E_EVAL
85 STM3210E_LCD_Init();
86 #elif defined USE_STM32100B_EVAL
87 STM32100B_LCD_Init();
88 #elif defined USE_STM3210B_EVAL
89 STM3210B_LCD_Init();
90 #endif
91
92
93 #ifdef USE_STM3210E_EVAL
94 /* Disable FSMC only for STM32 High-density and XL-density devices */
95 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
96 #endif /* USE_STM3210E_EVAL */
97
98 /* Initialize the Temperature Sensor */
99 LM75_Init();
100
101 if (LM75_GetStatus() == SUCCESS)
102 {
103 #ifdef USE_STM3210E_EVAL
104 /* Enable FSMC */
105 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
106 #endif /* USE_STM3210E_EVAL */
107
108 /* Clear the LCD */
109 LCD_Clear(LCD_COLOR_WHITE);
110
111 /* Set the Back Color */
112 LCD_SetBackColor(LCD_COLOR_BLUE);
113
114 /* Set the Text Color */
115 LCD_SetTextColor(LCD_COLOR_GREEN);
116
117 LCD_DisplayStringLine(LCD_LINE_0, " Temperature ");
118
119 #ifdef USE_STM3210E_EVAL
120 /* Disable FSMC */
121 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
122
123 /* Initialize the Temperature Sensor */
124 LM75_Init();
125
126 #endif /* USE_STM3210E_EVAL */
127
128 /* Configure the Temperature sensor device STLM75:
129 - Thermostat mode Interrupt
130 - Fault tolerance: 00
131 */
132 LM75_WriteConfReg(0x02);
133
134 /* Configure the THYS and TOS in order to use the SMbus alert interrupt */
135 LM75_WriteReg(LM75_REG_THYS, TEMPERATURE_THYS << 8); /*31İC*/
136 LM75_WriteReg(LM75_REG_TOS, TEMPERATURE_TOS << 8); /*32İC*/
137
138 I2C_ClearITPendingBit(LM75_I2C, I2C_IT_SMBALERT);
139
140 SMbusAlertOccurred = 0;
141
142 /* Infinite Loop */
143 while (1)
144 {
145 #ifdef USE_STM3210E_EVAL
146 /* Disable FSMC */
147 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
148
149 /* Initialize the Temperature Sensor */
150 LM75_Init();
151 #endif /* USE_STM3210E_EVAL */
152
153 /* Get double of Temperature value */
154 TempValue = LM75_ReadTemp();
155
156 #ifdef USE_STM3210E_EVAL
157 /* Enable FSMC */
158 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
159 #endif /* USE_STM3210E_EVAL */
160
161 if (TempValue <= 256)
162 {
163 /* Positive temperature measured */
164 TempCelsiusDisplay[7] = '+';
165
166 /* Initialize the temperature sensor value*/
167 TempValueCelsius = TempValue;
168 }
169 else
170 {
171 /* Negative temperature measured */
172 TempCelsiusDisplay[7] = '-';
173 /* Remove temperature value sign */
174 TempValueCelsius = 0x200 - TempValue;
175 }
176
177 /* Calculate temperature digits in İC */
178 if ((TempValueCelsius & 0x01) == 0x01)
179 {
180 TempCelsiusDisplay[12] = 0x05 + 0x30;
181 TempFahrenheitDisplay[12] = 0x05 + 0x30;
182 }
183 else
184 {
185 TempCelsiusDisplay[12] = 0x00 + 0x30;
186 TempFahrenheitDisplay[12] = 0x00 + 0x30;
187 }
188
189 TempValueCelsius >>= 1;
190
191 TempCelsiusDisplay[8] = (TempValueCelsius / 100) + 0x30;
192 TempCelsiusDisplay[9] = ((TempValueCelsius % 100) / 10) + 0x30;
193 TempCelsiusDisplay[10] = ((TempValueCelsius % 100) % 10) + 0x30;
194
195 if (TempValue > 256)
196 {
197 if (((9 * TempValueCelsius) / 5) <= 32)
198 {
199 /* Convert temperature İC to Fahrenheit */
200 TempValueFahrenheit = abs (32 - ((9 * TempValueCelsius) / 5));
201
202 /* Calculate temperature digits in İF */
203 TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
204 TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
205 TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
206 /* Positive temperature measured */
207 TempFahrenheitDisplay[7] = '+';
208 }
209 else
210 {
211 /* Convert temperature İC to Fahrenheit */
212 TempValueFahrenheit = abs(((9 * TempValueCelsius) / 5) - 32);
213 /* Calculate temperature digits in İF */
214 TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
215 TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
216 TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
217
218 /* Negative temperature measured */
219 TempFahrenheitDisplay[7] = '-';
220 }
221 }
222 else
223 {
224 /* Convert temperature İC to Fahrenheit */
225 TempValueFahrenheit = ((9 * TempValueCelsius) / 5) + 32;
226
227 /* Calculate temperature digits in İF */
228 TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
229 TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
230 TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
231
232 /* Positive temperature measured */
233 TempFahrenheitDisplay[7] = '+';
234 }
235
236 /* Display Fahrenheit value on LCD */
237 for (index = 0; index < 20; index++)
238 {
239 LCD_DisplayChar(LCD_LINE_6, (319 - (16 * index)), TempCelsiusDisplay[index]);
240
241 LCD_DisplayChar(LCD_LINE_7, (319 - (16 * index)), TempFahrenheitDisplay[index]);
242 }
243
244 if (SMbusAlertOccurred == 1)
245 {
246 /* Set the Back Color */
247 LCD_SetBackColor(LCD_COLOR_BLUE);
248 /* Set the Text Color */
249 LCD_SetTextColor(LCD_COLOR_RED);
250 LCD_DisplayStringLine(LCD_LINE_2, "Warning: Temp exceed");
251 LCD_DisplayStringLine(LCD_LINE_3, " 32 C ");
252 }
253 if (SMbusAlertOccurred == 2)
254 {
255 /* Set the Back Color */
256 LCD_SetBackColor(LCD_COLOR_WHITE);
257 /* Set the Text Color */
258 LCD_SetTextColor(LCD_COLOR_WHITE);
259 LCD_ClearLine(LCD_LINE_2);
260 LCD_ClearLine(LCD_LINE_3);
261 SMbusAlertOccurred = 0;
262 /* Set the Back Color */
263 LCD_SetBackColor(LCD_COLOR_BLUE);
264 /* Set the Text Color */
265 LCD_SetTextColor(LCD_COLOR_GREEN);
266 }
267 }
268 }
269 else
270 {
271 LCD_Clear(LCD_COLOR_WHITE);
272 LCD_DisplayStringLine(LCD_LINE_2, " LM75 not correctly ");
273 LCD_DisplayStringLine(LCD_LINE_3, " initialized... ");
274 LCD_DisplayStringLine(LCD_LINE_4, " Please restart the ");
275 LCD_DisplayStringLine(LCD_LINE_5, " example. ");
276 /* Infinite Loop */
277 while(1)
278 {
279 }
280 }
281 }
282
283 /**
284 * @brief Configures the different interrupt.
285 * @param None
286 * @retval None
287 */
288 void NVIC_Configuration(void)
289 {
290 NVIC_InitTypeDef NVIC_InitStructure;
291
292 /* Re-configure and enable I2C2 error interrupt to have the higher priority */
293 #ifdef USE_STM32100E_EVAL
294 NVIC_InitStructure.NVIC_IRQChannel = I2C2_ER_IRQn;
295 #else
296 NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
297 #endif
298 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
299 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
300 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
301 NVIC_Init(&NVIC_InitStructure);
302 }
303
304 #ifdef USE_FULL_ASSERT
305
306 /**
307 * @brief Reports the name of the source file and the source line number
308 * where the assert_param error has occurred.
309 * @param file: pointer to the source file name
310 * @param line: assert_param error line source number
311 * @retval None
312 */
313 void assert_failed(uint8_t* file, uint32_t line)
314 {
315 /* User can add his own implementation to report the file name and line number,
316 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
317
318 /* Infinite loop */
319 while (1)
320 {
321 }
322 }
323
324 #endif
325
326 /**
327 * @}
328 */
329
330 /**
331 * @}
332 */
333
334 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/