使用QTMER 脉冲输出功能脉冲数多发问题

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

使用QTMER 脉冲输出功能脉冲数多发问题

64,601 Views
798821723
Contributor I
大家好,我按手册46.7.5.12章的脉冲输出功能 Pulse-Output Mode,按照例程编写代码,用官方的开发板测试,能够发送脉冲,但是我用逻辑分析仪查看脉冲数有时正确,有时会多发一个,不知是什么问题。 请大家指点谢谢。
0 Kudos
Reply
26 Replies

7,001 Views
stefanct
Contributor IV

Hi,

have you ever figured out what the underlying problem was? I have a very similar situation on an RT1020 but I am not using Keil.

0 Kudos
Reply

61,254 Views
798821723
Contributor I

7B2415983990D2CE30F5E4277823E5BB.jpg7A0A71D6C762B86B60239444F5CD1639.jpg

0 Kudos
Reply

61,263 Views
798821723
Contributor I

void PulseChxStream_Init(TMR_Type *TIMx,IRQn_Type IRQn,uint32_t Plusefreq,uint16_t PluseData,uint8_t TimChanel,uint8_t OutChanel,uint16_t OutMode)
{
TIMx->CHANNEL[OutChanel].CTRL&=~TMR_CTRL_CM_MASK; /* 复位计数器 */
TIMx->CHANNEL[TimChanel].CTRL&=~TMR_CTRL_CM_MASK; /* 复位定时器 */
//选择IP_bus_clk / 16作为定时器3的时钟源
/* TMR3_CTRL:CM = 0,PCS = 0x0C,SCS = 0,ONCE = 0,LENGTH = 1,DIR = 0,COINIT = 0,OUTMODE = 3 */
// TMR3->CHANNEL[1].COMP1=CLOCK_GetFreq(kCLOCK_IpgClk)/128/Plusefreq; /* (16 * 37500)/ 60e6 = 0.01秒 */
if(Plusefreq<10000)
{
TIMx->CHANNEL[TimChanel].CTRL=0x1E23; /* 设置模式 */
TIMx->CHANNEL[TimChanel].COMP1=IpFreq/128/(Plusefreq);
}
else
{
TIMx->CHANNEL[TimChanel].CTRL=0x1023; /* 设置模式 */
TIMx->CHANNEL[TimChanel].COMP1=IpFreq/1/(Plusefreq);
}
/* TMR3_SCTRL:TCF = 0,TCFIE = 0,TOF = 0,TOFIE = 0,IEF = 0,IEFIE = 0,IPS = 0,INPUT = 0,
功能说明
Capture_Mode = 0,MSTR = 0,EEOF = 0,VAL = 0,FORCE = 0,OPS = 0,OEN = 0
*/
TIMx->CHANNEL[TimChanel].SCTRL=0x00;
TIMx->CHANNEL[TimChanel].LOAD=0x00; /* 重置加载寄存器 */
/* TMR3_CSCTRL: DBG_EN=0,FAULT=0,ALT_LOAD=0,ROC=0,TCI=0,UP=0,OFLAG=0,
TCF2EN=0,TCF1EN=0,TCF2=0,TCF1=0,CL2=0,CL1=0 */
TIMx->CHANNEL[TimChanel].CSCTRL=0x00; /* 设置比较器控制寄存器 */
// 定时器3的输出是该定时器的时钟源。
/* TMR1_CTRL: CM=0,PCS=7,SCS=0,ONCE=1,LENGTH=1,DIR=0,COINIT=0,OUTMODE=7 */
TIMx->CHANNEL[OutChanel].CTRL=OutMode; /* 0x0A67 设置模式 TMR3的定时器1作为通道0的时钟源 */
/* TMR1_SCTRL: TCF=0,TCFIE=0,TOF=0,TOFIE=0,IEF=0,IEFIE=0,IPS=0,INPUT=0,
Capture_Mode=0,MSTR=0,EEOF=0,VAL=0,FORCE=0,OPS=0,OEN=1 */
TIMx->CHANNEL[OutChanel].SCTRL=0x0001;
TIMx->CHANNEL[OutChanel].CNTR=0x00; /* 重置计数器寄存器 */
TIMx->CHANNEL[OutChanel].LOAD=0x00; /* 重置加载寄存器 */
TIMx->CHANNEL[OutChanel].COMP1=PluseData; /* 设置比较1个寄存器 输出的脉冲数*/
// 设置为在最后一个脉冲之后中断
/* TMR1_CSCTRL: DBG_EN=0,FAULT=0,ALT_LOAD=0,ROC=0,TCI=0,UP=0,OFLAG=0,
TCF2EN=0,TCF1EN=1,TCF2=0,TCF1=0,CL2=0,CL1=0 */
TIMx->CHANNEL[OutChanel].CSCTRL=0x40; /* 设置比较器控制寄存器 */
// 最后,启动计数器运行
TIMx->CHANNEL[TimChanel].CNTR=0; /* 重置计数器 */
/*设置中断优先级,*/
set_IRQn_Priority(IRQn,Group4_PreemptPriority_6, Group4_SubPriority_0);
/*开启中断 */
EnableIRQ(IRQn);

TIMx->CHANNEL[TimChanel].CTRL|=(2<<13); /* 运行源时钟计数器 */
TIMx->CHANNEL[OutChanel].CTRL|=(2<<13); /* 运行计数器 */
}

测试重复调用并发送脉冲,还是会多发。不知什么原因?

大家可以测试一下。

0 Kudos
Reply

61,248 Views
jeremyzhou
NXP Employee
NXP Employee

Hi,

你所说的多发现象是每次都会发生吗?还是存在固定的比率或发生次序呢?

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

61,244 Views
798821723
Contributor I

您好,这种现象不是有规律的发生,有时会发送数量正确,有时就连续发送多次都错误(总是多发1个)。

还有时会发送一次数量是对的,再发一次数量多一个,这样交替。

但是,每次复位芯片后第一次发送数量是对的,在发送一次就不对了。

0 Kudos
Reply

61,242 Views
798821723
Contributor I

我是想用这种方式来控制步进电机做角度控制,如果发送的脉冲数量不正确肯定会影响电机转动角度的精度。

我反复测试,感觉是 QTMER通道0在计数比较后没有及时的关闭输出,造成输出通道多发送了一个脉冲。

感觉还是芯片硬件上的问题,我的程序是严格按照手册的示例来做的。您也可以用1052-EVK实际测试一下。

如果你有代码示例,发我,我来用逻辑分析仪测试。

                                     谢谢!

0 Kudos
Reply

61,234 Views
jeremyzhou
NXP Employee
NXP Employee

Hi,

请分享完整的代码工程,我用MIMXRT1050 板子测试一下,看看能否复现问题。

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

61,207 Views
798821723
Contributor I

分析仪测量GPIO2的Pin6

0 Kudos
Reply

61,206 Views
798821723
Contributor I

使用的KEIL5.27开发环境

Tags (1)
0 Kudos
Reply

61,197 Views
jeremyzhou
NXP Employee
NXP Employee

Hi,
我根据你分享的代码,修改SDK库中evkbimxrt1050_qtmr_timer代码来实现Pulse-Output Mode,测试下来功能是正常的。
PWM 输出脚为GPIO_AD_B1_00, MIMXRT1050中J23-6.

jeremyzhou_0-1616056266688.png


附件是我的代码。

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

61,157 Views
798821723
Contributor I

您使用的是KEIL吗?我的是KEIL5.27,怎么打不开您的功能文件呢?

0 Kudos
Reply

61,150 Views
798821723
Contributor I

/*
* Copyright 2017-2020 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

/*******************************************************************************
* Includes
******************************************************************************/
#include "fsl_debug_console.h"
#include "board.h"

#include "bsp_tmr.h"
//#include "fsl_qtmr.h"

#include "pin_mux.h"
#include "clock_config.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/* The QTMR instance/channel used for board */
#define BOARD_QTMR_BASEADDR TMR3
#define BOARD_FIRST_QTMR_CHANNEL kQTMR_Channel_0
#define BOARD_SECOND_QTMR_CHANNEL kQTMR_Channel_1
#define QTMR_ClockCounterOutput kQTMR_ClockCounter0Output


/* Interrupt number and interrupt handler for the QTMR instance used */
#define QTMR_IRQ_ID TMR3_IRQn
#define QTMR_IRQ_HANDLER TMR3_IRQHandler

/* QTMR Clock source divider for Ipg clock source, the value of two macros below should be aligned. */
#define QTMR_PRIMARY_SOURCE (kQTMR_ClockDivide_128)
#define QTMR_CLOCK_SOURCE_DIVIDER (128U)
/* The frequency of the source clock after divided. */
#define QTMR_SOURCE_CLOCK (CLOCK_GetFreq(kCLOCK_IpgClk) / QTMR_CLOCK_SOURCE_DIVIDER)

/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/

volatile bool qtmrIsrFlag = false;

/*******************************************************************************
* Code
******************************************************************************/
void QTMR_IRQ_HANDLER(void)
{
/* Clear interrupt flag.*/
QTMR_ClearStatusFlags(BOARD_QTMR_BASEADDR, BOARD_SECOND_QTMR_CHANNEL, kQTMR_CompareFlag);

qtmrIsrFlag = true;
}

 

/*!
* @brief Main function
*/
int main(void)
{
uint32_t i = 0;
qtmr_config_t qtmrConfig;

gpio_pin_config_t gpt_config;

gpt_config.direction = kGPIO_DigitalOutput;
gpt_config.outputLogic = 0;
gpt_config.interruptMode = kGPIO_NoIntmode;


/* Board pin, clock, debug console init */
BOARD_ConfigMPU();
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();

GPIO_PinInit(GPIO1, 16, &gpt_config);

QTMR_GetDefaultConfig(&qtmrConfig);

/* Initial the input channel. */
qtmrConfig.primarySource = QTMR_PRIMARY_SOURCE;
QTMR_Init(BOARD_QTMR_BASEADDR, BOARD_SECOND_QTMR_CHANNEL, &qtmrConfig);

IpFreq=CLOCK_GetFreq(kCLOCK_IpgClk);
//PlsueCh0Init();
PulseChxStream_Init(TMR3,TMR3_IRQn,100000,4,1,0,0x0A67);
for(i=0;i<100000000;i++);

PulseChxStream_Init(TMR3,TMR3_IRQn,100000,4,1,0,0x0A67);
for(i=0;i<100000000;i++);

PulseChxStream_Init(TMR3,TMR3_IRQn,100000,4,1,0,0x0A67);
for(i=0;i<100000000;i++);

PulseChxStream_Init(TMR3,TMR3_IRQn,100000,4,1,0,0x0A67);

while (1)
{
}
}

我把您的代码移植到qtmr_timer的例程里替换掉源文件,编译下载到板卡仿真,用逻辑分析仪抓取不到波形

0 Kudos
Reply

61,136 Views
jeremyzhou
NXP Employee
NXP Employee

Hi,
我使用的是MCUXpreeso IDE,因为我的输出是在GPIO_AD_B1_00, MIMXRT1050中J23-6,所以需要对其进行相关的引脚初始化,对应代码在BOARD_InitPins()函数中。
Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

61,133 Views
798821723
Contributor I

您有没有发送后在仿真状态下用软件复位在次发送呢?我用KEIL软件复位后再次执行代码就会发送不正确的脉冲数了,给板卡上电后第一次运行代码才能正常发送正确的脉冲数量。

另外你是否方便用keil做一下测试呢?或者看一下我发您的KEIL例程,用这个例程测试一下。

0 Kudos
Reply

61,149 Views
798821723
Contributor I

引脚是J23的6

0 Kudos
Reply

61,148 Views
798821723
Contributor I

板卡IMXRT1050-EVKB

0 Kudos
Reply

61,142 Views
798821723
Contributor I

仿真时不出波形,板卡手动复位能抓到波形,但是波形也不对,脉冲数量也不对。

0 Kudos
Reply

61,088 Views
jeremyzhou
NXP Employee
NXP Employee

Hi,

你这个波形是使用我分享的例程测测试出来的吗?如果不是,请直接下载我的工程用MCUXpresso IDE测试。

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply

61,141 Views
798821723
Contributor I

我发现代码下载到板卡每次按下板卡复位按键逻辑分析仪能抓到波形,每次按下后抓的波形和数量都是正确的,但是在keil调试状态用软件上的复位后在运行时用逻辑分析仪抓到的脉冲数量就会多一个,您可以试一下。看看什么原因。

0 Kudos
Reply

61,131 Views
798821723
Contributor I
 
0 Kudos
Reply