关于stm32的tim1的计数模式

2019-07-14 17:16发布

我现在要用tiM1产生频率为20kHz的PWM,我选用TIM1的中央对齐模式:
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAlignedl;
为了提高定时器的分辨率,直接将72 MHz作为定时器的计数时钟,没有任何预分频。假设PWM频率为frq = 20k,则可以计算时间基的计数值tpwm:
   
    TIM_CLOCK是定时器的计数频率,本系统为72 MHz。频率设为20 kHz,则计数器的值则为3600。
问题:那请问我向TIM_TimeBaseStructure.TIM_Period(TIM1_ARR)中应该赋值为1800还是1800-1啊?为什么?求高手帮忙指导
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
18条回答
Xavier_1995
1楼-- · 2019-07-15 11:24
楼上正解。
亲爱的娜娜
2楼-- · 2019-07-15 12:35
对定时器理解不是很多,但是从参考手册上看好像中央对齐模式不需要减1,因为向上和向下都把0计算在内了,但中央对齐好像有点不同。


leilijiao
3楼-- · 2019-07-15 12:40
djw200988 发表于 2018-9-29 18:51
0~1800-1,刚好1800个数,两倍就是3600

但是手册上写的是:In center-aligned mode, the counter counts from 0 to the auto-reload value (content of the
TIMx_ARR register) – 1, generates a counter overflow event, then counts from the autoreload value down to 1 and generates a counter underflow event. Then it restarts counting from 0.(在中央对齐模式,计数器从0开始计数到自动加载的值(TIMx_ARR寄存器)−1,产生一个计数器溢出事件,然后向下计数到1并且产生一个计数器下溢事件;然后再从0开始重新计数。)
这样的话,如果里面装1800-1,上升时计数器从0开始计数到1798(上升计了1799个数),然后向下计数到1(1798个数),总共就不是3600了。不知我这样分析对不?
leilijiao
4楼-- · 2019-07-15 16:23
 精彩回答 2  元偷偷看……
亲爱的娜娜
5楼-- · 2019-07-15 20:11
leilijiao 发表于 2018-9-29 19:33
但是手册上写的是:In center-aligned mode, the counter counts from 0 to the auto-reload value (content of the
TIMx_ARR register) – 1, generates a counter overflow event, then counts from the autoreload value down to 1 and generates a counter underflow event. Then it restarts counting from 0.(在中央 ...

我也是这样理解的,手册上的例子也给出了类似的说明。
在向上和向下模式时是从0-1799总共计时1800次
在中央对齐时是0-1798 总共1799,然后再从1799到1又是1799,所以总共计时3598.
TOPCB
6楼-- · 2019-07-15 22:02
如果实在确定不了的,可以示波器测试一下波形。

一周热门 更多>