最近在折腾这个 cube rtc 每秒中断一次功能,始终不行,
如果不使用每秒中断功能进行读取rtc时间,完全正确,代码如下?
有谁帮忙看看是哪里出问题了啊 ???
唤醒中断 没有进入到 RTC_WKUP_IRQHandler(void) 这个中断函数里面来 ????????????
两个闹钟的中断完全正常。
#define RTC_SET_INIT_
tiME (0x32F2)
u8 RTC_ReadFlag=0; //一秒读一次时间 =1读取数据, =0不读
RTC_HandleTypeDef RtcHandle;
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_Al
ARMTypeDef sAlarm;
void MX_RTC_Init(void)
{
RtcHandle.Instance = RTC;
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
RtcHandle.Init.AsynchPrediv = 127;
RtcHandle.Init.SynchPrediv = 255;
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&RtcHandle);
if(HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0) != RTC_SET_INIT_TIME)
{
RTC_SetDate(15, 12, 28, 1); // 设置日期 2015-12-28 星期1
RTC_SetTime(22, 25, 45); // 22:25:45
/*##-3- Writes a data in a RTC Backup data Register0 #######################*/
HAL_RTCEx_BKUPWrite(&RtcHandle,RTC_BKP_DR0, RTC_SET_INIT_TIME);
}
else
{
/* Check if the Power On Reset flag is set */
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
{
#if RTC_DEBUG_SWITCH > 0u
/* Initialization Error */
printf("
RTC Initialization Error 1 ...");
#endif
}
/* Check if Pin Reset flag is set */
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
{
#if RTC_DEBUG_SWITCH > 0u
/* Initialization Error */
printf("
RTC Initialization Error 2 ...");
#endif
}
/* Clear Reset Flag */
__HAL_RCC_CLEAR_RESET_FLAGS();
}
/**Enable the WakeUp
*/
HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
}
void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
if(hrtc->Instance==RTC)
{
__HAL_RCC_RTC_ENABLE();
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 0x0E, 0);
HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
}
}
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
{
if(hrtc->Instance==RTC)
{
__HAL_RCC_RTC_DISABLE();
/* Peripheral interrupt Deinit*/
HAL_NVIC_DisableIRQ(RTC_WKUP_IRQn);
HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
}
}
//设置时间后,保存时调用
void RTC_SetDate(u8 year, u8 month, u8 date, u8 week)
{
/*##-1- Configure the Date #################################################*/
sDate.Year = year;
sDate.Month = month;
sDate.Date = date;
sDate.WeekDay = week;
if(HAL_RTC_SetDate(&RtcHandle,&sDate,RTC_FORMAT_BIN) != HAL_OK)
{
#if RTC_DEBUG_SWITCH > 0u
/* Initialization Error */
printf("
RTC Initialization Error 3 ...");
#endif
}
}
void RTC_SetTime(u8 hour, u8 minute, u8 second)
{
/*##-2- Configure the Time #################################################*/
sTime.Hours = hour;
sTime.Minutes = minute;
sTime.Seconds = second;
sTime.TimeFormat = RTC_HOURFORMAT12_AM;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if(HAL_RTC_SetTime(&RtcHandle,&sTime,RTC_FORMAT_BIN) != HAL_OK)
{
#if RTC_DEBUG_SWITCH > 0u
/* Initialization Error */
printf("
RTC Initialization Error 4 ...");
#endif
}
}
// rtc 时间 显示,使用lcd
void RTC_Display(void)
{
char datdat[10];
// /* Get the RTC current Time */
HAL_RTC_GetTime(&RtcHandle, &sTime, RTC_FORMAT_BIN); // 时间与日期需要同时获取,切记切记
// /* Get the RTC current Date */
HAL_RTC_GetDate(&RtcHandle, &sDate, RTC_FORMAT_BIN);
tFont.FontCode = FC_ST_16; // 字体代码 16点阵
tFont.FrontColor = CL_WHITE; // 字体颜 {MOD}
tFont.BackColor = CL_GREY;
tFont.Space = 0;
sprintf((char *)&datdat[0],
"20%02d/%02d/%02d",
sDate.Year,
sDate.Month,
sDate.Date);
LCD_DispStr( 136, 2, ( uint8_t *)&datdat[0], &tFont);
sprintf((char *)&datdat[0],
"%02d:%02d:%02d",
sTime.Hours,
sTime.Minutes,
sTime.Seconds);
LCD_DispStr( 224, 2, ( uint8_t *)&datdat[0], &tFont);
switch(sDate.WeekDay)
{
case 1: LCD_DispStr( 296, 2, (uint8_t *)("一"), &tFont); break;
case 2: LCD_DispStr( 296, 2, (uint8_t *)("二"), &tFont); break;
case 3: LCD_DispStr( 296, 2, (uint8_t *)("三"), &tFont); break;
case 4: LCD_DispStr( 296, 2, (uint8_t *)("四"), &tFont); break;
case 5: LCD_DispStr( 296, 2, (uint8_t *)("五"), &tFont); break;
case 6: LCD_DispStr( 296, 2, (uint8_t *)("六"), &tFont); break;
case 7: LCD_DispStr( 296, 2, (uint8_t *)("日"), &tFont); break;
}
}
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
RTC_ReadFlag = 1; // 时钟读取标志位 置 1
HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 1, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
}
/**
* @brief This function handles RTC wake-up interrupt through EXTI line 22.
*/
void RTC_WKUP_IRQHandler(void)
{
HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
int main(void)
{
SystemClock_Config();
MX_RTC_Init();
while(1)
{
// HAL_RTC_GetTime(&RtcHandle, &sTime, RTC_FORMAT_BIN);
// HAL_RTC_GetDate(&RtcHandle, &sDate, RTC_FORMAT_BIN);
// if(RtcSecondBak != sTime.Seconds) // 采用这个查询法是完全可以的
if(RTC_ReadFlag)
{
RTC_Display();
RtcSecondBak = sTime.Seconds;
RTC_ReadFlag = 0;
}
}
}
我的理解是进: RTC global interrupt 中断号是19
* @brief This function handles RTC global interrupt.
*/
void RTC_IRQHandler(void)
{
/* USER CODE BEGIN RTC_IRQn 0 */
/* USER CODE END RTC_IRQn 0 */
HAL_RTCEx_RTCIRQHandler(&hrtc);
/* USER CODE BEGIN RTC_IRQn 1 */
/* USER CODE END RTC_IRQn 1 */
}
一周热门 更多>