STM32 怎样在程序运行中改写RTC计数器寄存器的值呢?我用以下方法总是清0,请高手指点。
RCC->APB1ENR |= RCC_APB1ENR_PWREN; // enable clock for Power interface
PWR->CR |= PWR_CR_DBP; // enable access to RTC, BDC registers
if ((0x00000100 & RCC_BDCR_RTCSEL) == 0x00000100) {
RCC->BDCR |= RCC_BDCR_LSEON; // enable LSE
while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0);
}
RTC->CRL |= RTC_CRL_CNF; // set configuration mode
RTC->CNTH = (((17 *3600UL)+(31 *60UL)+(1))>>16) & 0xFFFF;
RTC->CNTL = (((17 *3600UL)+(31 *60UL)+(1))>>16) & 0xFFFF;
RTC->ALRH = (((17 *3600UL)+(30 *60UL)+(1))>>16) & 0xFFFF;
RTC->ALRL = (((17 *3600UL)+(30 *60UL)+(1))>>16) & 0xFFFF;
RTC->CRL &= ~RTC_CRL_CNF; // reset configuration mode
RTC->CRH = 0x00000003; // enable RTC interrupts
NVIC->Enable[0] |= (1 << (RTC_IRQChannel & 0x1F)); // enable interrupt
while ((RTC->CRL & RTC_CRL_RTOFF) == 0); // wait until write is finished
PWR->CR &= ~PWR_CR_DBP; // disable access to RTC registers
/*------------------------------------------------------------------------------
* 函数: void RTC_Config(void) 这个是修改好的例程
* 功能: RTC配置(修改的数值在RTCTime里面)
* 参数: 无
* 返回: 无
*------------------------------------------------------------------------------*/
void RTC_Config(void)
{
RCC->APB1ENR |= RCC_APB1ENR_PWREN; // enable clock for Power interface 打开电源管理
PWR->CR |= PWR_CR_DBP; // enable access to RTC, BDC registers 使能RTC和备份寄存器的访问
RCC->BDCR |= RCC_BDCR_LSEON; // enable LSE外部低速振荡器使能
while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0); // Wait for LSERDY = 1 (LSE is ready)
RCC->BDCR |= (0x00000100 | RCC_BDCR_RTCEN); // set RTC clock source, enable RTC clock选择LSE位RTC时钟,使能RTC时钟
RTC->CRL &= (u16)~RTC_CRL_RSF;//Clear RSF flag [清RSF标志]
while ((RTC->CRL & RTC_CRL_RSF) == (u16)RESET);//Loop until RSF flag is set [循环到RSF标志置位]
while ((RTC->CRL & RTC_CRL_RTOFF) == 0); // wait until write is finished
RTC->CRH = 0x00000003; // enable RTC interrupts
NVIC->Enable[0] |= (1 << (RTC_IRQChannel & 0x1F)); // enable interrupt
while ((RTC->CRL & RTC_CRL_RTOFF) == 0); // wait until write is finished
RTC->CRL |= RTC_CRL_CNF; // set configuration mode
RTC->PRLH = ((0x000003E8*32768/1000-1)>>16) & 0x00FF; // set prescaler load register high
RTC->PRLL = ((0x000003E8*32768/1000-1)) & 0xFFFF; // set prescaler load register low
while ((RTC->CRL & RTC_CRL_RTOFF) == 0); // wait until write is finished
RTC->CNTH = RTCTime >> 16;/* Set RTC COUNTER MSB word [设置RTC计数器高半字节]*/
RTC->CNTL = (RTCTime & 0xFFFF);/* Set RTC COUNTER LSB word [设置RTC计数器低半字节]*/
RTC->CRL &= ~RTC_CRL_CNF; // reset configuration mode
while ((RTC->CRL & RTC_CRL_RTOFF) == 0); // wait until write is finished
}
用户377235 2012-5-16 10:44
我遇到的问题是:
原来写入过RTC计数器,后来再也不能写入了
用户1017182 2008-10-30 14:14
用户1090342 2008-8-12 22:34
用户603227 2008-3-12 22:24