找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

STMF103單片機(jī)讀AB編碼器程序有問題嗎

[復(fù)制鏈接]
ID:990057 發(fā)表于 2022-2-10 15:58 | 顯示全部樓層 |閱讀模式
函數(shù)功能:把TIM2初始化為編碼器接口模式
入口參數(shù):無
返回  值:無
**************************************************************************/
void Encoder_Init_TIM2(void)
{
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
  TIM_ICInitTypeDef TIM_ICInitStructure;  
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能定時器4的時鐘
//  RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//使能PB端口時鐘
//      
//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;        //端口配置
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //浮空輸入
//  GPIO_Init(GPIOA, &GPIO_InitStructure);     
        //根據(jù)設(shè)定參數(shù)初始化GPIOB
          RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);//使能PB端口時鐘

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;        //端口配置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //浮空輸入
  GPIO_Init(GPIOB, &GPIO_InitStructure);         

  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 預(yù)分頻器
  TIM_TimeBaseStructure.TIM_Period = 1024; //設(shè)定計數(shù)器自動重裝值
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//選擇時鐘分頻:不分頻
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM向上計數(shù)  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用編碼器模式3
  TIM_ICStructInit(&TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_ICFilter = 10;
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清除TIM的更新標(biāo)志位
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  //Reset counter
  TIM_SetCounter(TIM2,0);
  TIM_Cmd(TIM2, ENABLE);
}

回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

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