STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/RTC/RTC_Timer/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file RTC/RTC_Timer/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 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup RTC_Timer 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private macro -------------------------------------------------------------*/ 00041 /* Private variables ---------------------------------------------------------*/ 00042 __IO uint8_t ALARM_Occured = 0; 00043 __IO uint32_t RTCAlarmCount = 0; 00044 00045 /* Private function prototypes -----------------------------------------------*/ 00046 static void RTC_Config(void); 00047 static void RTC_AlarmConfig(void); 00048 00049 /* Private functions ---------------------------------------------------------*/ 00050 00051 /** 00052 * @brief Main program. 00053 * @param None 00054 * @retval None 00055 */ 00056 int main(void) 00057 { 00058 /*!< At this stage the microcontroller clock setting is already configured, 00059 this is done through SystemInit() function which is called from startup 00060 file (startup_stm32f0xx.s) before to branch to application main. 00061 To reconfigure the default setting of SystemInit() function, refer to 00062 system_stm32f0xx.c file 00063 */ 00064 00065 /* Initialize the LCD */ 00066 #ifdef USE_STM320518_EVAL 00067 STM320518_LCD_Init(); 00068 #else 00069 STM32072B_LCD_Init(); 00070 #endif /* USE_STM320518_EVAL */ 00071 00072 /* Enable The Display */ 00073 LCD_DisplayOn(); 00074 00075 /* Clear the Background Layer */ 00076 LCD_Clear(LCD_COLOR_WHITE); 00077 00078 /* Clear the LCD */ 00079 LCD_Clear(White); 00080 00081 /* Set the LCD Back Color */ 00082 LCD_SetBackColor(Blue); 00083 00084 /* Set the LCD Text Color */ 00085 LCD_SetTextColor(White); 00086 00087 /* Displays MESSAGE1 on line 0 */ 00088 LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1); 00089 00090 /* RTC configuration */ 00091 RTC_Config(); 00092 00093 /* Set the LCD Text Color */ 00094 LCD_SetTextColor(Red); 00095 00096 /* Displays a rectangle on the LCD */ 00097 LCD_DrawRect(80, 280, 25, 240 ); 00098 00099 /* Configure the external interrupt "WAKEUP" and "TAMPER" buttons */ 00100 STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI); 00101 STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_EXTI); 00102 00103 /* Configure RTC AlarmA register to generate 8 interrupts per 1 Second */ 00104 RTC_AlarmConfig(); 00105 00106 /* set LCD Font */ 00107 LCD_SetFont(&Font12x12); 00108 00109 /* Set the LCD Back Color */ 00110 LCD_SetBackColor(White); 00111 00112 /* Set the LCD Text Color */ 00113 LCD_SetTextColor(Black); 00114 00115 /* Set the Back Color */ 00116 LCD_SetBackColor(LCD_COLOR_CYAN); 00117 /* Displays MESSAGE2 and MESSAGE3 on the LCD */ 00118 LCD_DisplayStringLine(LINE(18), (uint8_t *)MESSAGE2); 00119 LCD_DisplayStringLine(LINE(19), (uint8_t *)MESSAGE3); 00120 00121 /* Infinite loop */ 00122 while (1) 00123 { 00124 uint32_t tmp =0; 00125 /* ALARM Interrupt */ 00126 if (ALARM_Occured) 00127 { 00128 if(RTCAlarmCount != 480) 00129 { 00130 /* Increament the counter of Alarma interrupts */ 00131 RTCAlarmCount++; 00132 00133 /* Set the LCD Back Color */ 00134 LCD_SetTextColor(Green); 00135 00136 /* Draw rectangle on the LCD */ 00137 LCD_DrawFullRect(81, 359, 80+ (((RTCAlarmCount)-1)/2) , 24); 00138 00139 /* Set the LCD text color */ 00140 LCD_SetTextColor(Red); 00141 00142 /* Display rectangle on the LCD */ 00143 LCD_DrawRect(80, 280, 25, 240 ); 00144 00145 /* Define the rate of Progress bar */ 00146 tmp = (RTCAlarmCount * 100)/ 480; 00147 00148 /* Set the LCD Font */ 00149 LCD_SetFont(&Font16x24); 00150 00151 /* Display Char on the LCD : XXX% */ 00152 LCD_DisplayChar(LINE(2),200, (tmp / 100) +0x30); 00153 LCD_DisplayChar(LINE(2),180, ((tmp % 100 ) / 10) +0x30); 00154 LCD_DisplayChar(LINE(2),160, (tmp % 10) +0x30); 00155 LCD_DisplayChar(LINE(2),140, 0x25); 00156 } 00157 else 00158 { 00159 /* Disable the RTC Clock */ 00160 RCC_RTCCLKCmd(DISABLE); 00161 00162 } 00163 /* Reinitialize the ALARM variable */ 00164 ALARM_Occured = 0; 00165 } 00166 } 00167 } 00168 00169 /** 00170 * @brief Configures the RTC peripheral and select the clock source. 00171 * @param None 00172 * @retval None 00173 */ 00174 static void RTC_Config(void) 00175 { 00176 RTC_InitTypeDef RTC_InitStructure; 00177 RTC_TimeTypeDef RTC_TimeStruct; 00178 00179 /* Enable the PWR clock */ 00180 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 00181 00182 /* Allow access to RTC */ 00183 PWR_BackupAccessCmd(ENABLE); 00184 00185 /* Reset RTC Domain */ 00186 RCC_BackupResetCmd(ENABLE); 00187 RCC_BackupResetCmd(DISABLE); 00188 00189 /* Enable the LSE OSC */ 00190 RCC_LSEConfig(RCC_LSE_ON); 00191 00192 /* Wait till LSE is ready */ 00193 while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) 00194 {} 00195 00196 /* Select the RTC Clock Source */ 00197 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); 00198 00199 /* Configure the RTC data register and RTC prescaler */ 00200 /* ck_spre(1Hz) = RTCCLK(LSI) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/ 00201 RTC_InitStructure.RTC_AsynchPrediv = 0x7F; 00202 RTC_InitStructure.RTC_SynchPrediv = 0xFF; 00203 RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; 00204 RTC_Init(&RTC_InitStructure); 00205 00206 /* Set the time to 00h 00mn 00s AM */ 00207 RTC_TimeStruct.RTC_H12 = RTC_H12_AM; 00208 RTC_TimeStruct.RTC_Hours = 0x00; 00209 RTC_TimeStruct.RTC_Minutes = 0x00; 00210 RTC_TimeStruct.RTC_Seconds = 0x00; 00211 RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct); 00212 } 00213 00214 /** 00215 * @brief Configures the RTC Alarm. 00216 * @param None 00217 * @retval None 00218 */ 00219 static void RTC_AlarmConfig(void) 00220 { 00221 EXTI_InitTypeDef EXTI_InitStructure; 00222 RTC_AlarmTypeDef RTC_AlarmStructure; 00223 NVIC_InitTypeDef NVIC_InitStructure; 00224 00225 RTC_AlarmStructInit(&RTC_AlarmStructure); 00226 00227 /* EXTI configuration */ 00228 EXTI_ClearITPendingBit(EXTI_Line17); 00229 EXTI_InitStructure.EXTI_Line = EXTI_Line17; 00230 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 00231 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 00232 EXTI_InitStructure.EXTI_LineCmd = ENABLE; 00233 EXTI_Init(&EXTI_InitStructure); 00234 00235 /* Enable the RTC Alarm Interrupt */ 00236 NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; 00237 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00238 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00239 NVIC_Init(&NVIC_InitStructure); 00240 00241 /* Set the alarmA Masks */ 00242 RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All; 00243 RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure); 00244 00245 /* Set AlarmA subseconds and enable SubSec Alarm : generate 8 interripts per Second */ 00246 RTC_AlarmSubSecondConfig(RTC_Alarm_A, 0xFF, RTC_AlarmSubSecondMask_SS14_5); 00247 00248 /* Enable AlarmA interrupt */ 00249 RTC_ITConfig(RTC_IT_ALRA, ENABLE); 00250 } 00251 00252 #ifdef USE_FULL_ASSERT 00253 00254 /** 00255 * @brief Reports the name of the source file and the source line number 00256 * where the assert_param error has occurred. 00257 * @param file: pointer to the source file name 00258 * @param line: assert_param error line source number 00259 * @retval None 00260 */ 00261 void assert_failed(uint8_t* file, uint32_t line) 00262 { 00263 /* User can add his own implementation to report the file name and line number, 00264 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00265 00266 /* Infinite loop */ 00267 while (1) 00268 { 00269 } 00270 } 00271 #endif 00272 00273 /** 00274 * @} 00275 */ 00276 00277 /** 00278 * @} 00279 */ 00280 00281 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/