使用场景: 用qtimer对正交编码器进行计数, 需要32bit,所以采用两个qtimer级联的方式。
代码设置如下:目前存在几个问题,需要请教下:
1: 此时怎样对正交编码器的方向进行确认? 发现timer_0(第一级qtimer)减到0之后,就不再变化了;
2:第一级timer 计数到最大值后(65535), 有时第二级timer计数没有加1, 不知道这个是什么原因?
void init_qtimer()
{
qtmr_config_t qtmrConfig;
/*
* qtmrConfig.debugMode = kQTMR_RunNormalInDebug;
* qtmrConfig.enableExternalForce = false;
* qtmrConfig.enableMasterMode = false;
* qtmrConfig.faultFilterCount = 0;
* qtmrConfig.faultFilterPeriod = 0;
* qtmrConfig.primarySource = kQTMR_ClockDivide_2;
* qtmrConfig.secondarySource = kQTMR_Counter0InputPin;
*/
QTMR_GetDefaultConfig(&qtmrConfig);
/* Enable at the NVIC */
//EnableIRQ(QTMR_IRQ_ID);
qtmrConfig.primarySource = BOARD_FIRST_QTMR_CHANNEL;
qtmrConfig.secondarySource = BOARD_SECOND_QTMR_CHANNEL;
QTMR_Init(TMR3, BOARD_FIRST_QTMR_CHANNEL, &qtmrConfig);
/* Init the second channel to use output of the first channel as we are chaining the first channel
* and the second channel */
qtmrConfig.primarySource = kQTMR_ClockCounter0Output;
qtmrConfig.secondarySource = kQTMR_Counter1InputPin;
QTMR_Init(TMR3, BOARD_SECOND_QTMR_CHANNEL, &qtmrConfig);
QTMR_SetTimerPeriod(TMR3, BOARD_FIRST_QTMR_CHANNEL, 65535);
QTMR_SetTimerPeriod(TMR3, BOARD_SECOND_QTMR_CHANNEL, 65535);
//TMR3->CHANNEL[BOARD_FIRST_QTMR_CHANNEL].CNTR = 0;
//TMR3->CHANNEL[BOARD_SECOND_QTMR_CHANNEL].CNTR = 0;
/* Enable the second channel compare interrupt */
//QTMR_EnableInterrupts(TMR3, BOARD_SECOND_QTMR_CHANNEL, kQTMR_CompareInterruptEnable);
/* Start the second channel in cascase mode, chained to the first channel as set earlier via the primary source
* selection */
QTMR_StartTimer(TMR3, BOARD_SECOND_QTMR_CHANNEL, kQTMR_CascadeCount);
/* Start the first channel to count the quad count */
QTMR_StartTimer(TMR3, BOARD_FIRST_QTMR_CHANNEL, kQTMR_QuadCountMode);
}
楼主你好!
刚才我看到与非网那边有一位网友分享了他的一段级联代码,你可以试试看,是否能够工作。
https://www.nxpic.org.cn/module/forum/thread-623120-1-1.html
1: 此时怎样对正交编码器的方向进行确认? 发现timer_0(第一级qtimer)减到0之后,就不再变化了;
答:关于级联, 第一级减到0之后,如果方向还是需要减的,应该会减到第二级。
2:第一级timer 计数到最大值后(65535), 有时第二级timer计数没有加1, 不知道这个是什么原因?
答:应该是代码配置有问题,看上去,你的级联没有准确工作,你试试看网友的分享代码是否工作?
如果还有问题,欢迎继续交流!
Best Regards,
Kerry