标题:
有哪位大佬知道我的TIM4的编码器模式一直无法正常测速
[打印本页]
作者:
WangMC
时间:
2023-6-4 10:47
标题:
有哪位大佬知道我的TIM4的编码器模式一直无法正常测速
void EncoderT4_Init(void)
{
//编码器2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//编码器A相B相初始化
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//定时器4初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1; //ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
//定时器4编码器模式初始化
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM4, ENABLE);
}
int16_t Encoder_Get_L(void)
{
int16_t Temp_L;
Temp_L = TIM_GetCounter(TIM4);
TIM_SetCounter(TIM4, 0);
return Temp_L;
}
作者:
yzwzfyz
时间:
2023-6-5 11:07
想想:测速原理是什么呢?
作者:
ZzCp
时间:
2023-6-8 16:48
void EncoderT4_Init(void)
{
//编码器2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//编码器A相B相初始化
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
//定时器4初始化(每个脉冲1us,计数器最大范围为65535)
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 65535;
TIM_TimeBaseInitStructure.TIM_Prescaler = 84 - 1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
//定时器4编码器模式初始化
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM4, ENABLE);
}
int16_t Encoder_Get_L(void)
{
int16_t Temp_L;
Temp_L = TIM_GetCounter(TIM4);
TIM_SetCounter(TIM4, 0);
return Temp_L;
}
作者:
WangMC
时间:
2023-6-19 10:20
yzwzfyz 发表于 2023-6-5 11:07
想想:测速原理是什么呢?
是读取计数器计数值吗(不是很懂哈哈)但是我读取的返回值一直是1和-1,不知为何
作者:
13872888583
时间:
2023-6-19 13:23
测速当然用中断好一点。
作者:
WangMC
时间:
2023-6-19 15:08
ZzCp 发表于 2023-6-8 16:48
void EncoderT4_Init(void)
{
//编码器2
请问为何不需使用这个TIM_ICInitTypeDef结构体
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1