1、开发平台:
STM32L1-Discovery Keil 4
2、RTC Calendar和wake up
STM32L152RC 带有一个硬件的实时时钟模块(RTC),可为用户提供日历功能,RTC也可以提供一个自动定时的功能,相当于是一个睡眠定时器,下面就看看该如何配置日历和定时:
void SYS_RTCInit(void)
{
static NVIC_InitTypeDef NVIC_InitStructure;
static EXTI_InitTypeDef EXTI_InitStructure;
static RTC_InitTypeDef RTC_InitStructure;
static RTC_TimeTypeDef RTC_TimeStructure;
static uint32_t AsynchPrediv = 0, SynchPrediv = 0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/*!< Allow access to RTC */
PWR_RTCAccessCmd(ENABLE);
/*!< 使能外部低速时钟 */
RCC_LSEConfig(RCC_LSE_ON);
/*!< 等待外部低速时钟就绪 */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
/*!< 配置RTC时钟来源 */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/*!< 使能RTC时钟 */
RCC_RTCCLKCmd(ENABLE);
/*!< Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
//配置日历
/* 配置RTC数据寄存器以及时钟分频 */
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;//分频为1Hz
RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;//24小时制
/* 检查RTC初始化 */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
printf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
}
//配置时间 9点15分01秒
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
RTC_TimeStructure.RTC_Hours = 0x09;
RTC_TimeStructure.RTC_Minutes = 0x15;
RTC_TimeStructure.RTC_Seconds = 0x01;
RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
//配置自动定时功能
/* 中断配置 *******************************************************/
EXTI_ClearITPendingBit(EXTI_Line20);
EXTI_InitStructure.EXTI_Line = EXTI_Line20;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/*使能 RTC Wakeup Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* RTC 唤醒中端配置: Clock Source: RTCDiv_16, Wakeup Time Base: 4s */
RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
RTC_SetWakeUpCounter(0x1FFF);//0x1FFF = 8191; 4s = (8191+1)*(1/(32768/16))
/* 使能 the Wakeup Interrupt */
RTC_ITConfig(RTC_IT_WUT, ENABLE);
}
使能了RTC的定时中断,那么就要编写中断服务子程序,如下:
void RTC_WKUP_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_WUT) != RESET)
{
RTC_ClearITPendingBit(RTC_IT_WUT);
EXTI_ClearITPendingBit(EXTI_Line20);
}
}
上一篇:STM32L152 外部中断配置
下一篇:STM32L152RC 在keil4中使用printf()和scanf() 函数
推荐阅读最新更新时间:2024-03-16 16:13
设计资源 培训 开发板 精华推荐
- stvd+stm8s: ERROR:before starting debug session, please, select a target
- stvd+stm8s: #error cpstm8 sx1276.c:70(35) missing prototype
- stvd+stm8s: The Flash Memory Read-out protection option must be disabled
- stvd+stm8l051F3(1):stvd一步一步建立stm8l工程项目
- ATmega48 外部复位
- ATmega48 掉电检测
- ATmega48 看门狗复位
- ATmega48 控制和状态寄存器MCUCSR
- ATmega48 基准电压使能信号和启动时间
- ATmega48 看门狗定时器