找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1424|回復(fù): 5
收起左側(cè)

有哪位大佬知道我的TIM4的編碼器模式一直無法正常測速

[復(fù)制鏈接]
ID:1064841 發(fā)表于 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;
}
回復(fù)

使用道具 舉報

ID:123289 發(fā)表于 2023-6-5 11:07 | 顯示全部樓層
想想:測速原理是什么呢?
回復(fù)

使用道具 舉報

ID:1078332 發(fā)表于 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,計數(shù)器最大范圍為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;
}
回復(fù)

使用道具 舉報

ID:1064841 發(fā)表于 2023-6-19 10:20 | 顯示全部樓層
yzwzfyz 發(fā)表于 2023-6-5 11:07
想想:測速原理是什么呢?

是讀取計數(shù)器計數(shù)值嗎(不是很懂哈哈)但是我讀取的返回值一直是1和-1,不知為何
回復(fù)

使用道具 舉報

ID:731755 發(fā)表于 2023-6-19 13:23 來自手機 | 顯示全部樓層
測速當(dāng)然用中斷好一點。
回復(fù)

使用道具 舉報

ID:1064841 發(fā)表于 2023-6-19 15:08 | 顯示全部樓層
ZzCp 發(fā)表于 2023-6-8 16:48
void EncoderT4_Init(void)
{
        //編碼器2

請問為何不需使用這個TIM_ICInitTypeDef結(jié)構(gòu)體
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表