Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Skool_wkshp_lib2015 by
rtc.cpp
00001 #include "rtc.h" 00002 #include "menu.h" 00003 #include "serial_lcd.h" 00004 #include "keypad.h" 00005 00006 HAL_StatusTypeDef HAL_status; 00007 HAL_LockTypeDef HAL_lock; 00008 RTC_HandleTypeDef rtch; 00009 HAL_RTCStateTypeDef rtc_state; 00010 RTC_InitTypeDef rtc_init; 00011 RTC_TimeTypeDef rtc_time; 00012 RTC_DateTypeDef rtc_date; 00013 RTC_AlarmTypeDef rtc_alarm; 00014 volatile int tmp_date, tmp_sec; 00015 volatile int AlarmA_Enabled = 0; 00016 volatile int AlarmA_triggered = 0; 00017 extern FSM_State Main_FSM_States; 00018 00019 const char BCD2HEX[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 00020 const char DOW[8][3] = { {' ',' ',' '}, {'H',' ',' '}, {'K',' ',' '}, {'S','z','e'}, {'C','s',' '}, {'P',' ',' '}, {'S','z','o'}, {'V',' ',' '} }; 00021 const char Yes_No[16] = {'*',':','O','K',' ',' ',' ',' ',' ','#',':','M',0x82,'g','s','e'};//"*:OK #:Mégse"}; 00022 00023 void RTC_AlarmAEventCallback(void) { 00024 AlarmA_triggered = 1; 00025 } 00026 00027 void RTC_AlarmIRQHandler(void) { 00028 if(RTC->ISR & RTC_ISR_ALRAF) { 00029 /* Get the status of the Interrupt */ 00030 if ((RTC->CR & RTC_IT_ALRA) != 0) { 00031 /* AlarmA callback */ 00032 RTC_AlarmAEventCallback(); 00033 00034 /* Clear the Alarm interrupt pending bit */ 00035 RTC->ISR &= ~(RTC_FLAG_ALRAF); 00036 } 00037 } 00038 00039 /* Clear the EXTI's line Flag for RTC Alarm */ 00040 EXTI->PR = RTC_EXTI_LINE_ALARM_EVENT; 00041 __HAL_RTC_CLEAR_FLAG(RTC_EXTI_LINE_ALARM_EVENT); 00042 00043 /* Change RTC state */ 00044 rtch.State = HAL_RTC_STATE_READY; 00045 } 00046 00047 void ShowAlarm(void) { 00048 char tt[16]; 00049 00050 write_ser_lcd(0x01, false); // Clear display 00051 wait_us(1100); 00052 write_ser_lcd(0x06, false); // Entry mode set 00053 wait_us(30); 00054 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line 00055 tt[0] = BCD2HEX[((rtc_alarm.AlarmTime.Hours & 0xF0) >> 4)]; 00056 tt[1] = BCD2HEX[(rtc_alarm.AlarmTime.Hours & 0x0F)]; 00057 tt[2] = ':'; 00058 tt[3] = BCD2HEX[((rtc_alarm.AlarmTime.Minutes & 0xF0) >> 4)]; 00059 tt[4] = BCD2HEX[(rtc_alarm.AlarmTime.Minutes & 0x0F)]; 00060 tt[5] = ':'; 00061 tt[6] = BCD2HEX[((rtc_alarm.AlarmTime.Seconds & 0xF0) >> 4)]; 00062 tt[7] = BCD2HEX[(rtc_alarm.AlarmTime.Seconds & 0x0F)]; 00063 tt[8] = ' '; 00064 tt[9] = DOW[rtc_alarm.AlarmDateWeekDay][0]; 00065 tt[10] = DOW[rtc_alarm.AlarmDateWeekDay][1]; 00066 tt[11] = DOW[rtc_alarm.AlarmDateWeekDay][2]; 00067 write_ser_text(tt, 12); 00068 write_ser_lcd(0xC0, false); 00069 wait_us(30); 00070 write_ser_text(Yes_No, 16); 00071 } 00072 00073 void ShowTime(void) { 00074 char tt[16]; 00075 00076 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line 00077 tt[0] = BCD2HEX[((rtc_time.Hours & 0xF0) >> 4)]; 00078 tt[1] = BCD2HEX[(rtc_time.Hours & 0x0F)]; 00079 tt[2] = ':'; 00080 tt[3] = BCD2HEX[((rtc_time.Minutes & 0xF0) >> 4)]; 00081 tt[4] = BCD2HEX[(rtc_time.Minutes & 0x0F)]; 00082 tt[5] = ':'; 00083 tt[6] = BCD2HEX[((rtc_time.Seconds & 0xF0) >> 4)]; 00084 tt[7] = BCD2HEX[(rtc_time.Seconds & 0x0F)]; 00085 tmp_sec = rtc_time.Seconds; 00086 tt[8] = ' '; 00087 if (AlarmA_Enabled) { 00088 tt[9] = 0x01; 00089 } else { 00090 tt[9] = ' '; 00091 } 00092 write_ser_text(tt, 10); 00093 } 00094 00095 void ShowDate(void) { 00096 char tt[16]; 00097 00098 write_ser_lcd(0xC0, false); // set DDRAM addr to 0x00, beginning of 1st line 00099 tt[0] = BCD2HEX[2]; 00100 tt[1] = BCD2HEX[0]; 00101 tt[2] = BCD2HEX[((rtc_date.Year & 0xF0) >> 4)]; 00102 tt[3] = BCD2HEX[(rtc_date.Year & 0x0F)]; 00103 tt[4] = '.'; 00104 tt[5] = BCD2HEX[((rtc_date.Month & 0xF0) >> 4)]; 00105 tt[6] = BCD2HEX[(rtc_date.Month & 0x0F)]; 00106 tt[7] = '.'; 00107 tt[8] = BCD2HEX[((rtc_date.Date & 0xF0) >> 4)]; 00108 tt[9] = BCD2HEX[(rtc_date.Date & 0x0F)]; 00109 tt[10] = '.'; 00110 tmp_date = rtc_date.Date; 00111 tt[11] = ' '; 00112 tt[12] = DOW[rtc_date.WeekDay][0]; 00113 tt[13] = DOW[rtc_date.WeekDay][1]; 00114 tt[14] = DOW[rtc_date.WeekDay][2]; 00115 write_ser_text(tt,15); 00116 } 00117 00118 void ShowTimeforSet(char * tt) { 00119 write_ser_lcd(0x01, false); // Clear display 00120 wait_us(1100); 00121 write_ser_lcd(0x06, false); // Entry mode set 00122 wait_us(30); 00123 write_ser_lcd(0x0E, false); // Display ON, Cursor ON, Blink OFF 00124 wait_us(30); 00125 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line 00126 tt[0] = BCD2HEX[((rtc_time.Hours & 0xF0) >> 4)]; 00127 tt[1] = BCD2HEX[(rtc_time.Hours & 0x0F)]; 00128 tt[2] = ':'; 00129 tt[3] = BCD2HEX[((rtc_time.Minutes & 0xF0) >> 4)]; 00130 tt[4] = BCD2HEX[(rtc_time.Minutes & 0x0F)]; 00131 tt[5] = ':'; 00132 tt[6] = BCD2HEX[((rtc_time.Seconds & 0xF0) >> 4)]; 00133 tt[7] = BCD2HEX[(rtc_time.Seconds & 0x0F)]; 00134 write_ser_text(tt, 8); 00135 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line 00136 } 00137 00138 void ShowAlarmforSet(char * tt) { 00139 write_ser_lcd(0x01, false); // Clear display 00140 wait_us(1100); 00141 write_ser_lcd(0x06, false); // Entry mode set 00142 wait_us(30); 00143 write_ser_lcd(0x0E, false); // Display ON, Cursor ON, Blink OFF 00144 wait_us(30); 00145 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line 00146 tt[0] = BCD2HEX[((rtc_alarm.AlarmTime.Hours & 0xF0) >> 4)]; 00147 tt[1] = BCD2HEX[(rtc_alarm.AlarmTime.Hours & 0x0F)]; 00148 tt[2] = ':'; 00149 tt[3] = BCD2HEX[((rtc_alarm.AlarmTime.Minutes & 0xF0) >> 4)]; 00150 tt[4] = BCD2HEX[(rtc_alarm.AlarmTime.Minutes & 0x0F)]; 00151 tt[5] = ':'; 00152 tt[6] = BCD2HEX[((rtc_alarm.AlarmTime.Seconds & 0xF0) >> 4)]; 00153 tt[7] = BCD2HEX[(rtc_alarm.AlarmTime.Seconds & 0x0F)]; 00154 tt[8] = ' '; 00155 tt[9] = DOW[rtc_date.WeekDay][0]; 00156 tt[10] = DOW[rtc_date.WeekDay][1]; 00157 tt[11] = DOW[rtc_date.WeekDay][2]; 00158 write_ser_text(tt, 8); 00159 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line 00160 } 00161 00162 void ShowDateforSet(char * tt) { 00163 write_ser_lcd(0x01, false); // Clear display 00164 wait_us(1100); 00165 write_ser_lcd(0x06, false); // Entry mode set 00166 wait_us(30); 00167 write_ser_lcd(0x0E, false); // Display ON, Cursor ON, Blink OFF 00168 wait_us(30); 00169 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line 00170 tt[0] = BCD2HEX[2]; 00171 tt[1] = BCD2HEX[0]; 00172 tt[2] = BCD2HEX[((rtc_date.Year & 0xF0) >> 4)]; 00173 tt[3] = BCD2HEX[(rtc_date.Year & 0x0F)]; 00174 tt[4] = '.'; 00175 tt[5] = BCD2HEX[((rtc_date.Month & 0xF0) >> 4)]; 00176 tt[6] = BCD2HEX[(rtc_date.Month & 0x0F)]; 00177 tt[7] = '.'; 00178 tt[8] = BCD2HEX[((rtc_date.Date & 0xF0) >> 4)]; 00179 tt[9] = BCD2HEX[(rtc_date.Date & 0x0F)]; 00180 tt[10] = '.'; 00181 tt[11] = ' '; 00182 tt[12] = DOW[rtc_date.WeekDay][0]; 00183 tt[13] = DOW[rtc_date.WeekDay][1]; 00184 tt[14] = DOW[rtc_date.WeekDay][2]; 00185 write_ser_text(tt,15); 00186 write_ser_lcd(0x82, false); // set DDRAM addr to 0x00, beginning of 1st line 00187 } 00188 00189 void InitRTC(void) { 00190 uint32_t dummy; 00191 00192 rtc_init.AsynchPrediv = 0x7F; 00193 rtc_init.SynchPrediv = 0xFF; 00194 rtc_init.HourFormat = RTC_HOURFORMAT_24; 00195 rtc_init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; 00196 rtc_init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; 00197 rtc_init.OutPut = RTC_OUTPUT_DISABLE; 00198 00199 rtc_date.Date = 27; 00200 rtc_date.Month = 05; 00201 rtc_date.WeekDay = 2; 00202 rtc_date.Year = 14; 00203 tmp_date = 27; 00204 00205 rtc_time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; 00206 rtc_time.Hours = 00; 00207 rtc_time.Minutes = 25; 00208 rtc_time.Seconds = 00; 00209 rtc_time.StoreOperation =RTC_STOREOPERATION_SET; 00210 tmp_sec = 0; 00211 00212 rtch.Instance = RTC; 00213 rtch.Init = rtc_init; 00214 rtch.Lock = HAL_lock; 00215 rtch.State = rtc_state; 00216 00217 __PWR_CLK_ENABLE(); 00218 HAL_PWR_EnableBkUpAccess(); 00219 __HAL_RCC_LSE_CONFIG(RCC_LSE_ON); 00220 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); 00221 dummy = RCC->BDCR; 00222 dummy |= RCC_BDCR_RTCEN; 00223 RCC->BDCR = dummy; 00224 // __HAL_RCC_RTC_ENABLE(); 00225 EXTI->PR = RTC_EXTI_LINE_ALARM_EVENT; 00226 RTC->ISR &= ~(RTC_FLAG_ALRAF); 00227 HAL_RTC_Init(&rtch); 00228 dummy = (RTC->BKP15R & 0x00000007); 00229 AlarmA_Enabled = ((dummy & 0x00000004) == 1); 00230 if (dummy == 0x0) { 00231 HAL_RTC_SetDate(&rtch, &rtc_date, FORMAT_BIN); 00232 HAL_RTC_SetTime(&rtch, &rtc_time, FORMAT_BIN); 00233 } 00234 } 00235 00236 void AlarmA_Enable(void) { 00237 int keypress; 00238 00239 HAL_RTC_GetAlarm(&rtch, &rtc_alarm, RTC_ALARM_A, FORMAT_BCD); 00240 //if (rtc_alarm.AlarmDateWeekDay == 0) { rtc_alarm.AlarmDateWeekDay = 1;} // There was no alarm set yet 00241 ShowAlarm(); 00242 keypress = Poll_keypad_yesno(); 00243 if (keypress == 0x0C) { 00244 HAL_RTC_DeactivateAlarm(&rtch, RTC_ALARM_A); 00245 HAL_RTC_SetAlarm_IT(&rtch, &rtc_alarm, FORMAT_BCD); 00246 NVIC_SetVector(RTC_Alarm_IRQn, (uint32_t)RTC_AlarmIRQHandler); 00247 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); 00248 AlarmA_Enabled = 1; 00249 RTC->BKP15R |= 0x00000004; 00250 } 00251 Main_FSM_States = FINISH; 00252 } 00253 00254 void AlarmA_Disable(void) { 00255 HAL_RTC_DeactivateAlarm(&rtch, RTC_ALARM_A); 00256 AlarmA_Enabled = 0; 00257 RTC->BKP15R &= ~(0x00000004); 00258 Main_FSM_States = FINISH; 00259 } 00260 00261 void SetRTCTime(void) { 00262 int keypress; 00263 char TT[8]; 00264 int index = 0; 00265 int exit = 0; 00266 00267 HAL_RTC_GetTime(&rtch, &rtc_time, FORMAT_BCD); 00268 ShowTimeforSet(TT); 00269 00270 while (exit == 0) { 00271 keypress = Poll_keypad_full(); 00272 switch (keypress) { 00273 case '*': 00274 if (index > 0) {index--;} 00275 if ((index == 2) | (index == 5)) {index--;} 00276 break; 00277 case '#': 00278 if (index < 8) {index++;} 00279 if ((index == 2) | (index == 5)) {index++;} 00280 if (index == 8) {exit = 1;} 00281 break; 00282 default: 00283 TT[index] = keypress; 00284 if (index < 8) {index++;} 00285 if ((index == 2) | (index == 5)) {index++;} 00286 if (index == 8) {exit = 1;} 00287 break; 00288 } 00289 write_ser_lcd(0x80, false); 00290 wait_us(30); 00291 write_ser_text(TT,8); 00292 write_ser_lcd(0x80 + index, false); 00293 } 00294 rtc_time.Hours = ((TT[0] - 0x30) << 4) + (TT[1] - 0x30); 00295 rtc_time.Minutes = ((TT[3] - 0x30) << 4) + (TT[4] - 0x30); 00296 rtc_time.Seconds = ((TT[6] - 0x30) << 4) + (TT[7] - 0x30); 00297 HAL_RTC_SetTime(&rtch, &rtc_time, FORMAT_BCD); 00298 RTC->BKP15R |= 0x00000001; 00299 Main_FSM_States = FINISH; 00300 } 00301 00302 void SetRTCAlarm(void) { 00303 int keypress; 00304 char TT[12]; 00305 int index = 0; 00306 int exit = 0; 00307 int DoW; 00308 00309 HAL_RTC_GetAlarm(&rtch, &rtc_alarm, RTC_ALARM_A, FORMAT_BCD); 00310 DoW = rtc_alarm.AlarmDateWeekDay; 00311 rtc_alarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY; // We only used weekday alarm 00312 ShowAlarmforSet(TT); 00313 00314 while (exit == 0) { 00315 keypress = Poll_keypad_full(); 00316 switch (keypress) { 00317 case '*': 00318 if (index > 0) {index--;} 00319 if ((index == 2) | (index == 5)) {index--;} 00320 if (index == 8) {index--;} 00321 break; 00322 case '#': 00323 if (index < 10) {index++;} 00324 if ((index == 2) | (index == 5)) {index++;} 00325 if (index == 8) {index++;} 00326 if (index == 10) {exit = 1;} 00327 break; 00328 default: 00329 if (index < 8) {TT[index] = keypress;} else { 00330 DoW = (keypress - 0x30); 00331 TT[9] = DOW[DoW][0]; 00332 TT[10] = DOW[DoW][1]; 00333 TT[11] = DOW[DoW][2]; 00334 } 00335 if (index < 10) {index++;} 00336 if ((index == 2) | (index == 5)) {index++;} 00337 if (index == 8) {index++;} 00338 if (index == 10) {exit = 1;} 00339 break; 00340 } 00341 write_ser_lcd(0x80, false); 00342 wait_us(30); 00343 write_ser_text(TT,12); 00344 write_ser_lcd(0x80 + index, false); 00345 } 00346 rtc_alarm.AlarmTime.Hours = ((TT[0] - 0x30) << 4) + (TT[1] - 0x30); 00347 rtc_alarm.AlarmTime.Minutes = ((TT[3] - 0x30) << 4) + (TT[4] - 0x30); 00348 rtc_alarm.AlarmTime.Seconds = ((TT[6] - 0x30) << 4) + (TT[7] - 0x30); 00349 rtc_alarm.AlarmDateWeekDay = DoW; 00350 if (DoW == 0x00) { 00351 rtc_alarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; // Alarm is triggered every day, no date or weekday match 00352 } else { 00353 rtc_alarm.AlarmMask = RTC_ALARMMASK_NONE; // Alarm is triggered when everything match 00354 } 00355 HAL_RTC_DeactivateAlarm(&rtch, RTC_ALARM_A); 00356 HAL_RTC_SetAlarm_IT(&rtch, &rtc_alarm, FORMAT_BCD); 00357 NVIC_SetVector(RTC_Alarm_IRQn, (uint32_t)RTC_AlarmIRQHandler); 00358 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); 00359 RTC->BKP15R |= 0x00000006; 00360 AlarmA_Enabled = 1; 00361 Main_FSM_States = FINISH; 00362 } 00363 00364 void SetRTCDate(void) { 00365 int keypress; 00366 char TT[15]; 00367 int index = 2; 00368 int exit = 0; 00369 int DoW; 00370 00371 HAL_RTC_GetDate(&rtch, &rtc_date, FORMAT_BCD); 00372 DoW = rtc_date.WeekDay; 00373 ShowDateforSet(TT); 00374 00375 while (exit == 0) { 00376 keypress = Poll_keypad_full(); 00377 switch (keypress) { 00378 case '*': 00379 if (index > 2) {index--;} 00380 if ((index == 4) | (index == 7)) {index--;} 00381 if (index == 11) {index -= 2;} 00382 break; 00383 case '#': 00384 if (index < 13) {index++;} 00385 if ((index == 4) | (index == 7)) {index++;} 00386 if (index == 10) {index += 2;} 00387 if (index == 13) {exit = 1;} 00388 break; 00389 default: 00390 if (index < 10) {TT[index] = keypress;} else { 00391 DoW = (keypress - 0x30); 00392 TT[12] = DOW[DoW][0]; 00393 TT[13] = DOW[DoW][1]; 00394 TT[14] = DOW[DoW][2]; 00395 } 00396 if (index < 13) {index++;} 00397 if ((index == 4) | (index == 7)) {index++;} 00398 if (index == 10) {index += 2;} 00399 if (index == 13) {exit = 1;} 00400 break; 00401 } 00402 write_ser_lcd(0x80, false); 00403 wait_us(30); 00404 write_ser_text(TT,15); 00405 write_ser_lcd(0x80 + index, false); 00406 } 00407 rtc_date.Year = ((TT[2] - 0x30) << 4) + (TT[3] - 0x30); 00408 rtc_date.Month = ((TT[5] - 0x30) << 4) + (TT[6] - 0x30); 00409 rtc_date.Date = ((TT[8] - 0x30) << 4) + (TT[9] - 0x30); 00410 rtc_date.WeekDay = DoW; 00411 HAL_RTC_SetDate(&rtch, &rtc_date, FORMAT_BCD); 00412 RTC->BKP15R |= 0x00000001; 00413 Main_FSM_States = FINISH; 00414 }
Generated on Thu Jul 14 2022 17:13:24 by
1.7.2
