在线时间1 小时
UID61275
注册时间2008-7-25
NXP金币0
该用户从未签到
注册会员

- 积分
- 121
- 最后登录
- 1970-1-1
|
27.3.4 Channel (n) Status and Control (TPMx_CnSC)
CnSC contains the channel-interrupt-status flag and control bits used to configure the
interrupt enable, channel configuration, and pin function. When switching from one
channel mode to a different channel mode, the channel must first be disabled and this
must be acknowledged in the TPM counter clock domain.
这是手册中关于channel控制寄存器的描述。后面一句话,恐怕有人跟我一样开始并没理解clock domain指的什么。我是遇到了问题才明白为什么要写这句话。
通过SIM_SOPT2中的TPMSRC可以选择TPM的时钟。我的应用中TPM的时钟比较慢,而core的时钟比较快。部分代码如下,基本功能是测一个高电平的宽度。工作流程为:首先设置channel0捕捉上升沿,等通道事件标志为1时表示已捕捉到上升沿,记下C0V的值,即上升沿时刻。然后设置channel0捕捉下降沿,等待通道事件标志为1时记下C0V的值,即下降沿的时刻。
切换channel的模式时要先disable,这个好理解。开始TPM0_C0SC = 0x80;和TPM0_C0SC = 0x88; 这两句连着写的。发现并没如愿切换成下降沿捕捉。后来两句之间加了个等待子函数delay(10),就ok了。
我觉得是因为TPM的clock domain频率比较低,而内核运行的频率高,切换TPM通道模式时,上一个操作还没同步到TPM模块的clock domain下一个操作就来了,所以会失败。两个操作之间要等待一下较慢的那个clock domain。以上个人臆测,还望高手指教。
TPM0_C0SC = 0x84; // clear CHF, capture rising edge only
while ((TPM0_STATUS & 0x01) == 0);
TPM0_STATUS = 0x01; // clear CHF
TPM0_C0SC = 0x80; // must be disabled before changing mode
count_value = TPM0_C0V;
delay(10); // wait some time to be acknowledged in the TPM counter clock domain
// Capture falling edge
TPM0_C0SC = 0x88; // clear CHF, capture falling edge only
while ((TPM0_STATUS & 0x01) == 0);
TPM0_STATUS = 0x01; // clear CHF
TPM0_C0SC = 0x80; // must be disabled before changing mode
count_value = TPM0_C0V - count_value;
cntvalue = count_value;
|
|