OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 5296|回复: 4

STM32 RTC实现500ms定时器

[复制链接]

1

主题

5

帖子

0

精华

新手入门

积分
14
金钱
14
注册时间
2016-4-12
在线时间
3 小时
发表于 2016-4-12 20:12:33 | 显示全部楼层 |阅读模式
4金钱
急,最近小弟在做STM32F4, 想要通过RTC实现500ms的定时器, 现目前我采用的是官方提供的demo代码,但未能实现定时,还望大侠指点啊!


/**
  * @brief  Configures the RTC peripheral and select the clock source.
  * @param  None
  * @retval None
  */
static void RTC_Config(void)
{
  /* Enable the PWR clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  /* Allow access to RTC */
  PWR_BackupAccessCmd(ENABLE);
  /* Reset RTC Domain */
  RCC_BackupResetCmd(ENABLE);
  RCC_BackupResetCmd(DISABLE);
  /* Enable the LSE OSC */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */  
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  /* Configure the RTC data register and RTC prescaler */
  /* ck_spre(1Hz) = RTCCLK(LSI) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/
  RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
  RTC_InitStructure.RTC_SynchPrediv  = 0xFF;
  RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
  RTC_Init(&RTC_InitStructure);
  
  /* Set the time to 00h 00mn 00s AM */
  RTC_TimeStruct.RTC_H12     = RTC_H12_AM;
  RTC_TimeStruct.RTC_Hours   = 0;
  RTC_TimeStruct.RTC_Minutes = 0;
  RTC_TimeStruct.RTC_Seconds = 0;  
  RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct);
}
/**
  * @brief  Configures the RTC Alarm.
  * @param  None
  * @retval None
  */
static void RTC_AlarmConfig(void)
{
  EXTI_InitTypeDef EXTI_InitStructure;
  RTC_AlarmTypeDef RTC_AlarmStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* EXTI configuration */
  EXTI_ClearITPendingBit(EXTI_Line17);
  EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  
  /* Enable the RTC Alarm Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Set the alarm A Masks */
  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All;
  RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  RTC_AlarmStructure.RTC_AlarmDateWeekDay = RTC_Weekday_Monday;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = 0x0;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes =0x0;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds =0x0;
  RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
  
  /* Set alarm A sub seconds and enable SubSec Alarm : generate 8 interrupts per Second */
  RTC_AlarmSubSecondConfig(RTC_Alarm_A, 0xFF, RTC_AlarmSubSecondMask_SS14_5);
  /* Enable alarm A interrupt */
  RTC_ITConfig(RTC_IT_ALRA, ENABLE);
}

void RTC_Alarm_IRQHandler(void)
{
        if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)   /* Check on the Alarm A flag and on the number of interrupts per Second (60*8) */
        {
             /* Clear RTC AlarmA Flags */
            RTC_ClearITPendingBit(RTC_IT_ALRA);
        }

        EXTI_ClearITPendingBit(EXTI_Line17);  /* Clear the EXTI line 17 */
  }

}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

1

主题

5

帖子

0

精华

新手入门

积分
14
金钱
14
注册时间
2016-4-12
在线时间
3 小时
 楼主| 发表于 2016-4-12 20:16:46 | 显示全部楼层
有没有知道的朋友啊,小弟先谢过了
回复

使用道具 举报

1

主题

5

帖子

0

精华

新手入门

积分
14
金钱
14
注册时间
2016-4-12
在线时间
3 小时
 楼主| 发表于 2016-4-13 14:38:05 | 显示全部楼层
还望大神指点
回复

使用道具 举报

1

主题

5

帖子

0

精华

新手入门

积分
14
金钱
14
注册时间
2016-4-12
在线时间
3 小时
 楼主| 发表于 2016-4-13 15:40:38 | 显示全部楼层
我采用的芯片是STM32F427, 希望能在STOP模式下LED能够闪烁,必须要用到RTC 的alarm吗?
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165377
金钱
165377
注册时间
2010-12-1
在线时间
2111 小时
发表于 2016-4-13 21:09:01 | 显示全部楼层
就是利用周期性唤醒就行了,参考我们407的代码很容易搞定啊。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-26 22:18

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表