找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 658|回复: 5
收起左侧

有哪位大佬知道我的TIM4的编码器模式一直无法正常测速

[复制链接]
ID:1064841 发表于 2023-6-4 10:47 | 显示全部楼层 |阅读模式

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;
}
回复

使用道具 举报

ID:123289 发表于 2023-6-5 11:07 | 显示全部楼层
想想:测速原理是什么呢?
回复

使用道具 举报

ID:1078332 发表于 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;
}
回复

使用道具 举报

ID:1064841 发表于 2023-6-19 10:20 | 显示全部楼层
yzwzfyz 发表于 2023-6-5 11:07
想想:测速原理是什么呢?

是读取计数器计数值吗(不是很懂哈哈)但是我读取的返回值一直是1和-1,不知为何
回复

使用道具 举报

ID:731755 发表于 2023-6-19 13:23 来自手机 | 显示全部楼层
测速当然用中断好一点。
回复

使用道具 举报

ID:1064841 发表于 2023-6-19 15:08 | 显示全部楼层
ZzCp 发表于 2023-6-8 16:48
void EncoderT4_Init(void)
{
        //编码器2

请问为何不需使用这个TIM_ICInitTypeDef结构体
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表