AT32 RTC — 内部低频时钟校准

AT32 AT32F403A_407_Firmware_Library V2.2.2 RTC 内部低频时钟校准 示例教学

RTC — 内部低频时钟校准

固件库: AT32F403A_407_Firmware_Library V2.2.2
芯片: AT32
源文件: AT32/AT32F403A_407_Firmware_Library_V2.2.2/project/at_start_f403a/examples/rtc/lick_calibration/src/main.c


功能简介

本示例演示实时时钟(RTC)功能。RTC 是独立运行的计时模块,常用纽扣电池供电,即使主电源断电也能保持时间。

完整代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"

int main(void)
{
  tmr_input_config_type tmr_ic_init_structure;

  system_clock_config();

  at32_board_init();

  uart_print_init(115200);

  /* rtc configuration */
  rtc_configuration();

  printf("\r\n\nstart calib\r\n\r\n");

  /* get the frequency value */
  crm_clocks_freq_get(&crm_clocks);

  /* enable tmr5 apb1 clocks */
  crm_periph_clock_enable(CRM_TMR5_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);

  /* connect internally the tm5_ch4 input capture to the lick clock output */
  gpio_pin_remap_config(TMR5CH4_MUX, TRUE);

  /* tmr5 time base configuration */
  tmr_base_init(TMR5, 0xFFFF, 0);
  tmr_cnt_dir_set(TMR5, TMR_COUNT_UP);
  tmr_clock_source_div_set(TMR5, TMR_CLOCK_DIV1);

  /* tmr5 channel4 input capture mode configuration */
  tmr_input_default_para_init(&tmr_ic_init_structure);
  tmr_ic_init_structure.input_channel_select = TMR_SELECT_CHANNEL_4;
  tmr_ic_init_structure.input_polarity_select = TMR_INPUT_RISING_EDGE;
  tmr_ic_init_structure.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
  tmr_ic_init_structure.input_filter_value = 0;
  tmr_input_channel_init(TMR5, &tmr_ic_init_structure, TMR_CHANNEL_INPUT_DIV_1);

  /* reinitialize the index for the interrupt */
  operationcomplete = 0;

  /* enable the tmr5 input capture counter */
  tmr_counter_enable(TMR5, TRUE);

  /* Reset all tmr5 flags */
  tmr_flag_get(TMR5, TMR_C4_FLAG);

  /* enable the tmr5 channel 4 */
  tmr_interrupt_enable(TMR5, TMR_C4_INT, TRUE);

  /* nvic configuration */
  nvic_configuration();

  /* wait the tmr5 measuring operation to be completed */
  while(operationcomplete != 2);

  /* compute the actual frequency of the lick. (tim5_clk = 1 * pclk1)  */
  if(periodvalue != 0)
  {
    lickfreq = (uint32_t)((uint32_t)(crm_clocks.apb1_freq * 2) / (uint32_t)periodvalue);
  }

  printf("apb1_freq    = %d\r\n", crm_clocks.apb1_freq);
  printf("period_value = %d\r\n", periodvalue);
  printf("lick_freq    = %d\r\n", lickfreq);

  /* adjust the rtc prescaler value */
  rtc_divider_set((lickfreq - 1));

  /* wait for the register write to complete */
  rtc_wait_config_finish();

  /* turn on led2 */
  at32_led_on(LED2);

  while(1)
  {
  }
}

代码讲解

系统时钟初始化:配置 MCU 的主频和时钟树。不同芯片的时钟配置不同,一般由工具生成。

板级初始化:初始化开发板上的 LED、按键等基础外设。

串口初始化:配置串口波特率(这里是 115200),使能 printf 重定向。

printf 输出:通过串口打印调试信息。需要先调用 uart_print_init 完成重定向。

定时器配置:设置预分频器(PSC)和重装载值(ARR),定时时间 = (PSC+1) × (ARR+1) / 时钟频率。

中断优先级配置:NVIC 设置中断的抢占优先级和子优先级。数字越小优先级越高。

世界是你们
使用 Hugo 构建
主题 StackJimmy 设计