[技术问答] 新唐M0516定时器例程疑问

[复制链接]
2383|2
 楼主| 代码民工 发表于 2015-11-25 20:15 | 显示全部楼层 |阅读模式
本帖最后由 代码民工 于 2015-11-25 20:57 编辑
  1. /* Peripheral clock source */
  2.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL |
  3.                    CLK_CLKSEL1_TMR0_S_HXT | CLK_CLKSEL1_TMR1_S_HCLK | CLK_CLKSEL1_TMR2_S_HIRC | CLK_CLKSEL1_TMR3_S_HXT;
  4. /* Open Timer3 frequency to 4 Hz in periodic mode, and enable interrupt */
  5.     TIMER_Open(TIMER3, TIMER_PERIODIC_MODE, 8);
  6.     TIMER_EnableInt(TIMER3);


???这里设置的不是定时器工作频率为8吗,又没有分频,为什么注释里写的是4Hz呢,没理解过来,望高手解析。。。
 楼主| 代码民工 发表于 2015-11-25 20:21 | 显示全部楼层
  1. /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url]      Open Timer in specified mode and frequency
  3.   *
  4.   * @param[in]  timer       The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  5.   * @param[in]  u32Mode     Operation mode. Possible options are
  6.   *                         - \ref TIMER_ONESHOT_MODE
  7.   *                         - \ref TIMER_PERIODIC_MODE
  8.   *                         - \ref TIMER_TOGGLE_MODE
  9.   *                         - \ref TIMER_CONTINUOUS_MODE
  10.   * @param[in]  u32Freq     Target working frequency
  11.   *
  12.   * [url=home.php?mod=space&uid=266161]@return[/url]     Real Timer working frequency
  13.   *
  14.   * [url=home.php?mod=space&uid=1543424]@Details[/url]    This API is used to configure timer to operate in specified mode and frequency.
  15.   *             If timer cannot work in target frequency, a closest frequency will be chose and returned.
  16.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]       After calling this API, Timer is \b NOT running yet. But could start timer running be calling
  17.   *             \ref TIMER_Start macro or program registers directly.
  18.   */
  19. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
  20. {
  21.     uint32_t u32Clk = TIMER_GetModuleClock(timer);
  22.     uint32_t u32Cmpr = 0, u32Prescale = 0;

  23.     // Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, pre-scale = 0.
  24.     if(u32Freq > (u32Clk / 2))
  25.     {
  26.         u32Cmpr = 2;
  27.     }
  28.     else
  29.     {
  30.         if(u32Clk >= 0x4000000)
  31.         {
  32.             u32Prescale = 7;    // real prescaler value is 8
  33.             u32Clk >>= 3;
  34.         }
  35.         else if(u32Clk >= 0x2000000)
  36.         {
  37.             u32Prescale = 3;    // real prescaler value is 4
  38.             u32Clk >>= 2;
  39.         }
  40.         else if(u32Clk >= 0x1000000)
  41.         {
  42.             u32Prescale = 1;    // real prescaler value is 2
  43.             u32Clk >>= 1;
  44.         }

  45.         u32Cmpr = u32Clk / u32Freq;
  46.     }

  47.     timer->TCSR = u32Mode | u32Prescale;
  48.     timer->TCMPR = u32Cmpr;

  49.     return(u32Clk / (u32Cmpr * (u32Prescale + 1)));
  50. }



补充说明,用的新唐的M051开发板,12MHZ晶振
wangwang2015 发表于 2015-11-27 11:38 | 显示全部楼层
我司的技术支持QQ:2355898184,欢迎询问FAE!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部