Stm8l RTC 調試心得 經過兩天的摸索,終于把stm8l05b13的RCT 自動喚醒調通了。分別有庫和寄存器來實現。給大家分享一下。這里只說函數,原理自己看使用手冊,廢話不多說,程序呈上, RTC 初始化.
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.