📄 lpc177x_8x_qei.c
字号:
if (QEIReloadStruct->ReloadOption == QEI_TIMERRELOAD_TICKVAL)
{
pQei->LOAD = QEIReloadStruct->ReloadValue - 1;
}
else
{
#if 1
pclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
pclk = (pclk /(1000000/QEIReloadStruct->ReloadValue)) - 1;
pQei->LOAD = (uint32_t)pclk;
#else
ld = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
if (ld/1000000 > 0)
{
ld /= 1000000;
ld *= QEIReloadStruct->ReloadValue;
ld -= 1;
}
else
{
ld *= QEIReloadStruct->ReloadValue;
ld /= 1000000;
ld -= 1;
}
pQei->LOAD = ld;
#endif
}
}
/*********************************************************************//**
* @brief Get current timer counter in QEI peripheral
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @return Current timer counter in QEI peripheral
**********************************************************************/
uint32_t QEI_GetTimer(uint8_t qeiId)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
return (pQei->TIME);
}
/*********************************************************************//**
* @brief Get current velocity pulse counter in current time period
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @return Current velocity pulse counter value
**********************************************************************/
uint32_t QEI_GetVelocity(uint8_t qeiId)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
return (pQei->VEL);
}
/*********************************************************************//**
* @brief Get the most recently measured velocity of the QEI. When
* the Velocity timer in QEI is over-flow, the current velocity
* value will be loaded into Velocity Capture register.
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @return The most recently measured velocity value
**********************************************************************/
uint32_t QEI_GetVelocityCap(uint8_t qeiId)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
return (pQei->CAP);
}
/*********************************************************************//**
* @brief Set Velocity Compare value for QEI peripheral
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @param[in] ulVelComp Compare Velocity value to set
* @return None
**********************************************************************/
void QEI_SetVelocityComp(uint8_t qeiId, uint32_t ulVelComp)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
pQei->VELCOMP = ulVelComp;
}
/*********************************************************************//**
* @brief Set value of sampling count for the digital filter in
* QEI peripheral
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @param[in] ulSamplingPulse Value of sampling count to set
* @return None
**********************************************************************/
void QEI_SetDigiFilter(uint8_t qeiId, st_Qei_FilterCfg FilterVal)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
pQei->FILTERPHA = FilterVal.PHA_FilterVal;
pQei->FILTERPHB = FilterVal.PHB_FilterVal;
pQei->FILTERINX = FilterVal.INX_FilterVal;
}
/*********************************************************************//**
* @brief Check whether if specified interrupt flag status in QEI
* peripheral is set or not
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @param[in] ulIntType Interrupt Flag Status type, should be:
- QEI_INTFLAG_INX_Int: index pulse was detected interrupt
- QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
- QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
- QEI_INTFLAG_DIR_Int: Change of direction interrupt
- QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
- QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
- QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
current position interrupt
- QEI_INTFLAG_REV_Int: Index compare value is equal to the current
index count interrupt
- QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
- QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
- QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
* @return New State of specified interrupt flag status (SET or RESET)
**********************************************************************/
FlagStatus QEI_GetIntStatus(uint8_t qeiId, uint32_t ulIntType)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
return((pQei->INTSTAT & ulIntType) ? SET : RESET);
}
/*********************************************************************//**
* @brief Enable/Disable specified interrupt in QEI peripheral
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @param[in] ulIntType Interrupt Flag Status type, should be:
- QEI_INTFLAG_INX_Int: index pulse was detected interrupt
- QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
- QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
- QEI_INTFLAG_DIR_Int: Change of direction interrupt
- QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
- QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
- QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
current position interrupt
- QEI_INTFLAG_REV_Int: Index compare value is equal to the current
index count interrupt
- QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
- QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
- QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
* @param[in] NewState New function state, should be:
* - DISABLE
* - ENABLE
* @return None
**********************************************************************/
void QEI_IntCmd(uint8_t qeiId, uint32_t ulIntType, FunctionalState NewState)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
if (NewState == ENABLE)
{
pQei->IES = ulIntType;
}
else
{
pQei->IEC = ulIntType;
}
}
/*********************************************************************//**
* @brief Sets (forces) specified interrupt in QEI peripheral
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @param[in] ulIntType Interrupt Flag Status type, should be:
- QEI_INTFLAG_INX_Int: index pulse was detected interrupt
- QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
- QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
- QEI_INTFLAG_DIR_Int: Change of direction interrupt
- QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
- QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
- QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
current position interrupt
- QEI_INTFLAG_REV_Int: Index compare value is equal to the current
index count interrupt
- QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
- QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
- QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
* @return None
**********************************************************************/
void QEI_IntSet(uint8_t qeiId, uint32_t ulIntType)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
pQei->SET = ulIntType;
}
/*********************************************************************//**
* @brief Clear (force) specified interrupt (pending) in QEI peripheral
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @param[in] ulIntType Interrupt Flag Status type, should be:
- QEI_INTFLAG_INX_Int: index pulse was detected interrupt
- QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
- QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
- QEI_INTFLAG_DIR_Int: Change of direction interrupt
- QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
- QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
- QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
current position interrupt
- QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
current position interrupt
- QEI_INTFLAG_REV_Int: Index compare value is equal to the current
index count interrupt
- QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
- QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
- QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
* @return None
**********************************************************************/
void QEI_IntClear(uint8_t qeiId, uint32_t ulIntType)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
pQei->CLR = ulIntType;
}
/*********************************************************************//**
* @brief Calculates the actual velocity in RPM passed via velocity
* capture value and Pulse Per Round (of the encoder) value
* parameter input.
* @param[in] qeiId The Id of the expected QEI component
* It should be 0 (zero) always with LPC177x_8x
*
* @param[in] ulVelCapValue Velocity capture input value that can
* be got from QEI_GetVelocityCap() function
* @param[in] ulPPR Pulse per round of encoder
* @return The actual value of velocity in RPM (Round per minute)
**********************************************************************/
uint32_t QEI_CalculateRPM(uint8_t qeiId, uint32_t ulVelCapValue, uint32_t ulPPR)
{
LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
uint64_t rpm, clock, Load, edges;
// Get current Clock rate for timer input
clock = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
// Get Timer load value (velocity capture period)
Load = (uint64_t)(pQei->LOAD + 1);
// Get Edge
edges = (uint64_t)((pQei->CONF & QEI_CONF_CAPMODE) ? 4 : 2);
// Calculate RPM
rpm = ((clock * ulVelCapValue * 60) / (Load * ulPPR * edges));
return (uint32_t)(rpm);
}
/**
* @}
*/
#endif /*_QEI*/
/**
* @}
*/
/* --------------------------------- End Of File ------------------------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -