void RTC_Config(uint16_t time)
{ RTC_DeInit(); //初始化默认状态
CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE); //允许RTC时钟
CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_2); //选择RTC时钟源LSI、2=19K
RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); //19k/16=1.1875KHz t=0.85ms
RTC_ITConfig(RTC_IT_WUT, ENABLE); //开启中断
RTC_SetWakeUpCounter(time); //设置RTC Weakup计算器初值
RTC_WakeUpCmd(ENABLE); //使能自动唤醒
}
RTC 中断 @far @interrupt
void RTC_CSSLSE_IRQHandler (void)
{ RTC_WakeUpCmd(DISABLE);
RTC_ClearITPendingBit(RTC_IT_WUT);
}
Main()
{ GPIO_config();
RTC_Config(2000); //2000*0.85ms=1.7s _asm("rim");
while(1)
{ if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3))
{ RTC_WakeUpCmd(ENABLE); LED(OFF); _asm("halt"); } LED(ON);; } }
PB3接一个按键到地,指示灯灭,进入active-halt.1.7s后自动唤醒,指示灯亮 使用STVD 开发环境库函数。stm8l系列容易发溢出。有时候代码没多少就已经溢出了。删除库函数中的不要的部分,可能会得到一些空间但还是不够,最直接的办法就是改用寄存器操作
void RTC_Config(uint16_t time)
{ uint16_t wutwfcount = 0;
CLK->PCKENR2 =0x04; //CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
CLK->CRTCR =0x24;//CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_2);//LSI=19K RTC->WPR = RTC_WPR_DisableKey1;
RTC->WPR = RTC_WPR_DisableKey2; RTC->CR2 &=~0x04;
while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != 0xffff)) { wutwfcount ; }
RTC->CR1=0x00;
RTC->CR2 &=~0x04;
wutwfcount = 0;
while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != 0xffff))
{ wutwfcount ; } RTC->WUTRH = (uint8_t)(time>> 8);
RTC->WUTRL = (uint8_t)time;
RTC->CR2 =0x04; RTC->WPR=0xff; }
中断函数 @far @interrupt
void RTC_IRQHandler(void)
{ RTC->WPR = RTC_WPR_DisableKey1;
RTC->WPR = RTC_WPR_DisableKey2;
RTC->CR2 &=~0x04; RTC->WPR=0xff;
RTC->ISR2&=~0x04; }
关于RTC_Config中的while语句对应手册里的一句话关于RTC->ISR1中的WUTWF位 This bit is set by hardware when the wakeup timer values can be changed, after the WUTE bit has been set to 0 in RTC_CR2 0: Wakeup timer update not allowed. 1: Wakeup timer update allowed.