STM32编码器模式函数,求投票
时间:10-02
整理:3721RD
点击:
![](../imgqa/ele/mcu/mcu-3721rd.com-28795qthy3kb3caq.jpg)
#include "include.h"
/*******************************************************************************
±à???÷2?×?oˉêy3?ê??ˉ
//encoder.INPUT_B---PB4(TIM3_CH1)
//encoder.INPUT_A---PB5(TIM3_CH2)
*******************************************************************************/
void Encoder_Configuration(void)
{
GPIO_InitTypeDef gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
gpio.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4;
gpio.GPIO_Speed = GPIO_Speed_2MHz;
gpio.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB,&gpio);
TIM_EncoderInterfaceConfig(TIM3,
TIM_EncoderMode_TI12,
TIM_ICPolarity_Falling,
TIM_ICPolarity_Falling);
TIM_Cmd(TIM3,ENABLE);
}
/*******************************************************************************
TIM3?D??′|àíoˉêy
*******************************************************************************/
void TIM3_IRQHandler(void)
{
/* Clear the interrupt pending flag */
if (TIM_GetITStatus(TIM3,TIM_IT_Update)!= RESET)
{
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
}
}
void Encoder_Start(void)
{
TIM3->CNT = 0x7fff;
}
int Encoder_Get_CNT(void)
{
int cnt = 0;
cnt = (TIM3->CNT)-0x7fff;
TIM3->CNT = 0x7fff;
return cnt;
}