打印
[技术问答]

新唐M0516定时器例程疑问

[复制链接]
2170|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
代码民工|  楼主 | 2015-11-25 20:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 代码民工 于 2015-11-25 20:57 编辑
/* Peripheral clock source */
    CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL |
                   CLK_CLKSEL1_TMR0_S_HXT | CLK_CLKSEL1_TMR1_S_HCLK | CLK_CLKSEL1_TMR2_S_HIRC | CLK_CLKSEL1_TMR3_S_HXT;
/* Open Timer3 frequency to 4 Hz in periodic mode, and enable interrupt */
    TIMER_Open(TIMER3, TIMER_PERIODIC_MODE, 8);
    TIMER_EnableInt(TIMER3);


???这里设置的不是定时器工作频率为8吗,又没有分频,为什么注释里写的是4Hz呢,没理解过来,望高手解析。。。
沙发
代码民工|  楼主 | 2015-11-25 20:21 | 只看该作者
/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]      Open Timer in specified mode and frequency
  *
  * @param[in]  timer       The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  * @param[in]  u32Mode     Operation mode. Possible options are
  *                         - \ref TIMER_ONESHOT_MODE
  *                         - \ref TIMER_PERIODIC_MODE
  *                         - \ref TIMER_TOGGLE_MODE
  *                         - \ref TIMER_CONTINUOUS_MODE
  * @param[in]  u32Freq     Target working frequency
  *
  * [url=home.php?mod=space&uid=266161]@return[/url]     Real Timer working frequency
  *
  * [url=home.php?mod=space&uid=1543424]@Details[/url]    This API is used to configure timer to operate in specified mode and frequency.
  *             If timer cannot work in target frequency, a closest frequency will be chose and returned.
  * [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
  *             \ref TIMER_Start macro or program registers directly.
  */
uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
{
    uint32_t u32Clk = TIMER_GetModuleClock(timer);
    uint32_t u32Cmpr = 0, u32Prescale = 0;

    // Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, pre-scale = 0.
    if(u32Freq > (u32Clk / 2))
    {
        u32Cmpr = 2;
    }
    else
    {
        if(u32Clk >= 0x4000000)
        {
            u32Prescale = 7;    // real prescaler value is 8
            u32Clk >>= 3;
        }
        else if(u32Clk >= 0x2000000)
        {
            u32Prescale = 3;    // real prescaler value is 4
            u32Clk >>= 2;
        }
        else if(u32Clk >= 0x1000000)
        {
            u32Prescale = 1;    // real prescaler value is 2
            u32Clk >>= 1;
        }

        u32Cmpr = u32Clk / u32Freq;
    }

    timer->TCSR = u32Mode | u32Prescale;
    timer->TCMPR = u32Cmpr;

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



补充说明,用的新唐的M051开发板,12MHZ晶振

使用特权

评论回复
板凳
wangwang2015| | 2015-11-27 11:38 | 只看该作者
我司的技术支持QQ:2355898184,欢迎询问FAE!

使用特权

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

本版积分规则

1

主题

2

帖子

0

粉丝