ST意法半导体
直播中

HCPcry

8年用户 1172经验值
擅长:处理器/DSP
私信 关注
[问答]

更新STM32F098中的RTC_TR 和 RTC_DR寄存器问题如何解决?

我想在STM32中使用RTC模块。尽管我尝试更新日期和时间寄存器,但它们的值并未更新。我已经给出了我的代码。如果有人能指出我的程序有什么问题,我将不胜感激。
  •         uint32_t _time_Tens=0,_Time_Units=0,_Time_Value=0;
  •         //Must remove
  •         _Time_Hours=12;
  •         _Time_Minutes=20;
  •         _Time_Seconds = 10;
  •         _Date_Day=20;
  •         _Date_Month=5;
  •         _Date_Year=2020;
  •         //End of remove
  •         //Configure RTC clock domain
  •         RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  •         PWR->CR |= PWR_CR_DBP;
  •         RCC->BDCR=0;
  •         RCC->BDCR |= RCC_BDCR_BDRST;
  •         RCC->BDCR &= ~RCC_BDCR_BDRST;
  •         RCC->CSR &= ~RCC_CSR_LSION;
  •         RCC->BDCR |= RCC_BDCR_LSEON;
  •         while((RCC->BDCR & RCC_BDCR_LSERDY)!=RCC_BDCR_LSERDY);
  •         RCC->BDCR |= RCC_BDCR_RTCSEL_LSE;//Select LSE oscillator as the RTC clock source
  •         RCC->BDCR |= RCC_BDCR_RTCEN;//Enable RTC clock
  •         //End of RTC clock domain configuration
  •         //Configure RTC module
  •         RTC->WPR = 0xCA; //Enable write access for RTC registers
  •         RTC->WPR = 0x53; //Enable write access for RTC registers
  •         RTC->ISR |= RTC_ISR_INIT;//Set INIT=1 tp enter initialitation mode
  •         while((RTC->ISR & RTC_ISR_INITF)!=RTC_ISR_INITF);//wait until module enters the initialization mode
  •         //Set Hours
  •         _Time_Units = (uint32_t)(_Time_Hours % (uint32_t)10);
  •         _Time_Tens = (uint32_t)(_Time_Hours/(uint32_t)10);
  •         _Time_Value = (uint32_t)(((uint32_t)(_Time_Tens<<4) | _Time_Units)<<16);
  •     RTC->TR = _Time_Value;
  •     _Time_Tens=0,_Time_Units=0,_Time_Value=0;
  •     //Set Minutes
  •         _Time_Units = (uint32_t)(_Time_Minutes % (uint32_t)10);
  •         _Time_Tens = (uint32_t)(_Time_Minutes/(uint32_t)10);
  •         _Time_Value = (uint32_t)(((uint32_t)(_Time_Tens<<4) | _Time_Units)<<8);
  •         RTC->TR |= _Time_Value;
  •         _Time_Tens=0,_Time_Units=0,_Time_Value=0;
  •         //Set Seconds
  •         _Time_Units = (uint32_t)(_Time_Seconds % (uint32_t)10);
  •         _Time_Tens = (uint32_t)(_Time_Seconds/(uint32_t)10);
  •         _Time_Value = ((uint32_t)(_Time_Tens<<4) | _Time_Units);
  •         RTC->TR |= _Time_Value;
  •         _Time_Tens=0,_Time_Units=0,_Time_Value=0;
  •         RTC->TR |= RTC_TR_PM;
  •         RTC->ISR &= ~RTC_ISR_INIT;//Set INIT=0 t0 enter de-initialize timer
  •         while((RTC->ISR & RTC_ISR_INITF)==RTC_ISR_INITF);//wait until module exits the initialization mode
  •         RTC->WPR = 0xFE;
  •         RTC->WPR = 0x64;
  •         PWR->CR &= ~PWR_CR_DBP;
  •         RTC->ISR &= ~RTC_ISR_RSF;
  •         while((RTC->ISR & RTC_ISR_RSF)!=RTC_ISR_RSF);
  •         _Time_Value = (uint32_t)RTC->TR;
  •         _Time_Units = (uint32_t)RTC->DR;
  •     if((_Time_Value & 0x00120000)==0x00120000){
  •             while(1){
  •                     HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
  •                     HAL_Delay(500);
  •                     HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
  •                     HAL_Delay(500);
  •             }
  •     }







回帖(1)

龙献益

2023-2-8 14:00:23
> RTC->TR |= _Time_Value;
TR 和 RD 寄存器的行为并不像您预期的那样,它们不是“类内存”寄存器,到目前为止不是。
特别是,在初始化期间,当 RTC_ISR.INIT 设置为 1 时,它们会读取时间/日期的冻结值。
这就是为什么你不能 |= 进入它们的原因。
将要写入的值组装到一个辅助变量中,并执行一次写入。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分