打印

调试GD32F407xx分享(二)

[复制链接]
866|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
QQ2224043166|  楼主 | 2021-12-20 19:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
2.2sysTick
void systick_config(void)
{
  /* setup systick timer for 1000Hz interrupts */
  if (SysTick_Config(SystemCoreClock / 1000U)){
      /* capture error */
      while (1){
      }
  }
//    /* configure the systick handler priority */
//    NVIC_SetPriority(SysTick_IRQn, 0x00U);
}

2.3.RTC#define RTC_CLOCK_SOURCE_LXTAL
#define BKP_VALUE    0x32F0
__IO uint32_t prescaler_a = 0, prescaler_s = 0;

typedef struct {
  unsigned short  year;
  unsigned char   mon;
  unsigned char   day;   
  unsigned char   week;   
  unsigned char   hour;
  unsigned char   min;
  unsigned char   sec;
}tm;

/**
* @brief  Converts a 2 digit decimal to BCD format.
* @param  Value Byte to be converted
* @retval Converted byte
*/
uint8_t RTC_ByteToBcd2(uint8_t Value)
{
  uint32_t bcdhigh = 0U;

  while(Value >= 10U)
  {
    bcdhigh++;
    Value -= 10U;
  }

  return  ((uint8_t)(bcdhigh << 4U) | Value);
}       
         
/**
* @brief  Converts from 2 digit BCD to Binary.
* @param  Value BCD value to be converted
* @retval Converted word
*/
uint8_t RTC_Bcd2ToByte(uint8_t Value)
{
    uint32_t tmp = 0U;
    tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
    return (tmp + (Value & (uint8_t)0x0F));
}

void rtc_setup(void)
{
  rtc_parameter_struct   rtc_initpara;       

  /* setup RTC time value */
  rtc_initpara.factor_asyn = prescaler_a;
  rtc_initpara.factor_syn = prescaler_s;
  rtc_initpara.year = RTC_ByteToBcd2(FIRST_YEAR - 2000);
  rtc_initpara.day_of_week = RTC_SATURDAY;
  rtc_initpara.month = RTC_ByteToBcd2(FIRST_MONTH);
  rtc_initpara.date  = RTC_ByteToBcd2(FIRST_MDAY);
  rtc_initpara.display_format = RTC_24HOUR;
  rtc_initpara.am_pm = RTC_AM;

  rtc_initpara.hour   = 0x08;
  rtc_initpara.minute = 0x00;
  rtc_initpara.second = 0x00;

  /* RTC current time configuration */
  if(ERROR == rtc_init(&rtc_initpara))
  {
  //        printf("\n\r** RTC time configuration failed! **\n\r");
  }
  else
  {
  //        printf("\n\r** RTC time configuration success! **\n\r");
          RTC_BKP0 = BKP_VALUE;
  }

  //    rtc_alarm_disable(RTC_ALARM0);
}

void RTC_Initialize(void)
{
  /* enable PMU clock */
  rcu_periph_clock_enable(RCU_PMU);
  /* enable the access of the RTC registers */
  pmu_backup_write_enable();

  #if defined (RTC_CLOCK_SOURCE_IRC32K)       
  rcu_osci_on(RCU_IRC32K);
  rcu_osci_stab_wait(RCU_IRC32K);
  rcu_rtc_clock_config(RCU_RTCSRC_IRC32K);

  prescaler_s = 0x13F;
  prescaler_a = 0x63;
  #elif defined (RTC_CLOCK_SOURCE_LXTAL)       
  rcu_osci_on(RCU_LXTAL);
  rcu_osci_stab_wait(RCU_LXTAL);
  rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
  prescaler_s = 0xFF;
  prescaler_a = 0x7F;       
  #else
  #error RTC clock source should be defined.
  #endif /* RTC_CLOCK_SOURCE_IRC32K */

  rcu_periph_clock_enable(RCU_RTC);
  rtc_register_sync_wait();

  /* check if RTC has aready been configured */
  if (BKP_VALUE != RTC_BKP0)
  {
    rtc_setup();
  }
  else
  {
    /* detect the reset source */
    if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){
  //            printf("power on reset occurred....\n\r");
    }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){
  //            printf("external reset occurred....\n\r");
    }
  //        printf("no need to configure RTC....\n\r");
  }       

  rcu_all_reset_flag_clear();       
  RTC_Get(&rtc_timer);
}

void RTC_Get(tm* t)
{
  rtc_parameter_struct rtc_initpara;                       
//    uint32_t time_subsecond = 0;       

  rtc_current_time_get(&rtc_initpara);
  /* get the subsecond value of current time, and convert it into fractional format */
//    time_subsecond = rtc_subsecond_get();

  t->year = RTC_Bcd2ToByte(rtc_initpara.year) + 2000;
  t->mon  = RTC_Bcd2ToByte(rtc_initpara.month);
  t->day  = RTC_Bcd2ToByte(rtc_initpara.date);       
  t->hour = RTC_Bcd2ToByte(rtc_initpara.hour);
  t->min  = RTC_Bcd2ToByte(rtc_initpara.minute);
  t->sec  = RTC_Bcd2ToByte(rtc_initpara.second);
}

void RTC_Set( tm* t )
{
  rtc_parameter_struct   rtc_initpara;       

  /* setup RTC time value */
  rtc_initpara.factor_asyn = prescaler_a;
  rtc_initpara.factor_syn  = prescaler_s;
  rtc_initpara.year = RTC_ByteToBcd2(t->year - 2000);
  rtc_initpara.day_of_week = RTC_ByteToBcd2(t->week);
  rtc_initpara.month = RTC_ByteToBcd2(t->mon);
  rtc_initpara.date  = RTC_ByteToBcd2(t->day);
  rtc_initpara.display_format = RTC_24HOUR;       

  rtc_initpara.hour   = RTC_ByteToBcd2(t->hour);
  rtc_initpara.am_pm  = (t->hour < 12) ? RTC_AM : RTC_PM;
  rtc_initpara.minute = RTC_ByteToBcd2(t->min);
  rtc_initpara.second = RTC_ByteToBcd2(t->sec);

  /* RTC current time configuration */
  if(ERROR == rtc_init(&rtc_initpara))
  {
//        printf("\n\r** RTC time configuration failed! **\n\r");
  }
  else
  {
//        printf("\n\r** RTC time configuration success! **\n\r");
      RTC_BKP0 = BKP_VALUE;
  }
}



使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

71

主题

219

帖子

1

粉丝