Stm8l RTC 調(diào)試心得 經(jīng)過(guò)兩天的摸索,終于把stm8l05b13的RCT 自動(dòng)喚醒調(diào)通了。分別有庫(kù)和寄存器來(lái)實(shí)現(xiàn)。給大家分享一下。這里只說(shuō)函數(shù),原理自己看使用手冊(cè),廢話不多說(shuō),程序呈上, RTC 初始化.
void RTC_Config(uint16_t time)
{ RTC_DeInit(); //初始化默認(rèn)狀態(tài)
CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE); //允許RTC時(shí)鐘
CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_2); //選擇RTC時(shí)鐘源LSI、2=19K
RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); //19k/16=1.1875KHz t=0.85ms
RTC_ITConfig(RTC_IT_WUT, ENABLE); //開(kāi)啟中斷
RTC_SetWakeUpCounter(time); //設(shè)置RTC Weakup計(jì)算器初值
RTC_WakeUpCmd(ENABLE); //使能自動(dòng)喚醒
}
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接一個(gè)按鍵到地,指示燈滅,進(jìn)入active-halt.1.7s后自動(dòng)喚醒,指示燈亮 使用STVD 開(kāi)發(fā)環(huán)境庫(kù)函數(shù)。stm8l系列容易發(fā)溢出。有時(shí)候代碼沒(méi)多少就已經(jīng)溢出了。刪除庫(kù)函數(shù)中的不要的部分,可能會(huì)得到一些空間但還是不夠,最直接的辦法就是改用寄存器操作
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; }
中斷函數(shù) @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; }
關(guān)于RTC_Config中的while語(yǔ)句對(duì)應(yīng)手冊(cè)里的一句話關(guān)于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.