使用 编码器正交的示例代码,对于 输入 我 已经 生成了 它们之间90度相移 的方波脉冲, 现在 我 需要 找到 方向 以及 RPM , 我会发送 我的来源 如果 任何 修改 需要请更正我
#include "HL_sys_common.h"
/* USER CODE BEGIN (1) */
#include "HL_sys_core.h"
#include "math.h"
#include "HL_sci.h"
#include "HL_eqep.h"
#include "HL_etpwm.h"
#define UART sciREG1
void sciDisplayText(sciBASE_t *sci, uint8 *text, uint32 length);
uint8 ppr = 30U;
uint16 frequency = 2000U;
uint16 RPM= 0;
uint8 deltaT = 0U;
uint8 velocity = 0;
uint8 speed = 0;
double pi = 3.14;
uint32 channelA = 0U;
uint32 channelB = 0U;
uint8 direction;
uint32 prevchannelA = 0U;
uint32 prevchannelB = 0U;
/* USER CODE END */
/** @fn void main(void)
* @brief Application main function
* @note This function is empty by default.
*
* This function is called after startup.
* The user can use this function to implement the application.
*/
/* USER CODE BEGIN (2) */
void pulse_width(void)
{
etpwmInit();
etpwmSetClkDiv(etpwmREG1, ClkDiv_by_1, HspClkDiv_by_1);
etpwmSetTimebasePeriod(etpwmREG1, 37499U);
etpwmSetCount(etpwmREG1, 19750U);
etpwmSetCounterMode(etpwmREG1, CounterMode_Up);
etpwmStartTBCLK();
etpwmInit();
etpwmSetClkDiv(etpwmREG2, ClkDiv_by_1, HspClkDiv_by_1);
etpwmSetTimebasePeriod(etpwmREG2, 37499U);
etpwmSetCount(etpwmREG2, 20008U);
etpwmSetCounterMode(etpwmREG2, CounterMode_Up);
etpwmStartTBCLK();
}
/* USER CODE END */
int main(void)
{
/* USER CODE BEGIN (3) */
QEPInit();
sciInit();
_enable_interrupt_();
pulse_width();
/* EQEP initialization based on GUI Configuration. */
/* Enable Position Counter */
eqepEnableCounter(eqepREG2);
/* Enable Unit Timer. */
eqepEnableUnitTimer(eqepREG2);
/* Enable capture timer and capture period latch. */
eqepEnableCapture(eqepREG2);
while(1)
{
/* Status flag is set to indicate that a new value is latched in the QCPRD register. */
if((eqepREG2->QEPSTS & 0x80U) != 0U)
{
deltaT = eqepREG2->QCPRD;
velocity = (ppr / deltaT);
RPM = ((frequency * 60)/ ppr);
speed = (( RPM * ( 0.92 * pi) *60 )/1000) ;
/* Clear the Status flag. */
eqepREG2->QEPSTS |= 0x80U;
}
channelA = etpwmREG1->TBCTR ;
channelB = etpwmREG2->TBCTR ;
if (channelA > channelB) {
if (prevchannelA <= prevchannelB)
{
direction = 1; /* if its channelA is greater than channel B its moving Forward direction */
}
} else if (channelA < channelB) {
if (prevchannelA >= prevchannelB)
{
direction = 2; /* if its channelA is less than channel B its moving Reverse direction */
}
}
prevchannelA = channelA;
prevchannelB = channelB;

