由于stm32f103定时器2和5的编码器模式引脚在TIM2上,所以正常读取编码器值时不准确,会出现干扰。
将定时器2的输入服用到PB3,PA15才能正常使用。
但要注意PB3、PA15是JTAG的功能引脚。需要关闭JTAG功能。
现上传一个可用stm32f103 TIM2~5 读取4路编码器初始化,希望对大家有用。
单片机源程序如下:
- #include "Encoder.h"
- /**************************************************************************
- 函数功能:把TIM3初始化为编码器接口模式
- 入口参数:psc 设定计数器自动重装值
- 返回 值:无
- **************************************************************************/
- void Encoder_Init_TIM3(u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能定时器4的时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能PB端口时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
- TIM_TimeBaseStructure.TIM_Period = psc; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM向上计数
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用编码器模式3
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM3, &TIM_ICInitStructure);
- TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清除TIM的更新标志位
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
- //Reset counter
- TIM_SetCounter(TIM3,0);
- TIM_Cmd(TIM3, ENABLE);
- }
- /**************************************************************************
- 函数功能:把TIM4初始化为编码器接口模式
- 入口参数:psc 设定计数器自动重装值
- 返回 值:无
- **************************************************************************/
- void Encoder_Init_TIM4(u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能定时器4的时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能PB端口时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
- GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
- TIM_TimeBaseStructure.TIM_Period = psc; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM向上计数
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用编码器模式3
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM4, &TIM_ICInitStructure);
- TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清除TIM的更新标志位
- TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
- //Reset counter
- TIM_SetCounter(TIM4,0);
- TIM_Cmd(TIM4, ENABLE);
- }
- /**************************************************************************
- 函数功能:把TIM2初始化为编码器接口模式
- 入口参数:psc 设定计数器自动重装值
- 返回 值:无
- **************************************************************************/
- void Encoder_Init_TIM2(u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- //PA15 ch1, PB3 ch2
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // 关闭JTAG功能
- GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE); // 重映射TIM2的CH1、CH2到PA15和PB3
-
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- TIM_DeInit(TIM2);
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Period = psc;
- TIM_TimeBaseStructure.TIM_Prescaler = 0;
- TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM2, &TIM_ICInitStructure);
- TIM_ClearFlag(TIM2, TIM_FLAG_Update);
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- //Reset counter
- TIM2->CNT = 0;
- TIM_Cmd(TIM2, ENABLE);
- }
- /**************************************************************************
- 函数功能:把TIM5初始化为编码器接口模式
- 入口参数:psc 设定计数器自动重装值
- 返回 值:无
- **************************************************************************/
- void Encoder_Init_TIM5(u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);//使能定时器4的时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能PB端口时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB
-
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
- TIM_TimeBaseStructure.TIM_Period = psc; //设定计数器自动重装值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//选择时钟分频:不分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM向上计数
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用编码器模式3
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 10;
- TIM_ICInit(TIM5, &TIM_ICInitStructure);
- TIM_ClearFlag(TIM5, TIM_FLAG_Update);//清除TIM的更新标志位
- TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
- //Reset counter
- TIM_SetCounter(TIM5,0);
- TIM_Cmd(TIM5, ENABLE);
- }
复制代码
以上2个文件51hei下载地址:
tim0.zip
(1.52 KB, 下载次数: 42)
|