2019-07-20 22:47发布
[mw_shl_code=applescript,true]#include "STM32F1.h" #define ENCODER_TIMER TIM4 // Encoder unit connected to TIM4 #define ENCODER_PPR (u16)(2000) // number of pulses per revolution
//#define ENCODER_PPR (u16)(10) // number of pulses per revolution
#define COUNTER_RESET (u16)0 #define ICx_FILTER (u8) 6 // 6<-> 670nsec///问题1:这是怎么得来的
extern u16 n_counter; /* stm32的编码器模式其实就是捕获模式,只不过是把编码器的两路AB信号接到定时器的输入脚。 */ static void TIM4_Mode_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure;/* Encoder unit connected to TIM3, 4X mode */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能TIM时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟 GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//PA6 PA7浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_DeInit(ENCODER_TIMER);/* Timer configuration in Encoder mode */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling //不分频,陈锡爱 TIM_TimeBaseStructure.TIM_Period = 390*4; //设定计数器重装值 TIMx_ARR = 390*4 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置时钟分割 T_dts = T_ck_int TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数,陈锡爱 TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数 TIM_ICStructInit(&TIM_ICInitStructure);//将结构体中的内容缺省输入 TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; // 为什么是6???,选择输入比较滤波器 TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);//将TIM_ICInitStructure中的指定参数初始化TIM4 // TIM_ARRPreloadConfig(TIM4, ENABLE);//使能预装载 // Clear all pending interrupts TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);//清除TIM3的更新标志位 TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE); TIM4->CNT = COUNTER_RESET;//Reset counter //ENC_Clear_Speed_Buffer(); TIM_Cmd(ENCODER_TIMER, ENABLE); //启动TIM定时器 }
void TIM4_Init(void) { TIM4_Mode_Config(); }
void TIM4_IRQHandler(void) { if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查TIM4更新中断发生与否 { TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx更新中断标志 n_counter++; } }
[/mw_shl_code]
最多设置5个标签!
C:UserszsongDesktop1.bmp为什么编码器一直没转,还会显示?
[mw_shl_code=applescript,true]#include "STM32F1.h"
#define ENCODER_TIMER TIM4 // Encoder unit connected to TIM4
#define ENCODER_PPR (u16)(2000) // number of pulses per revolution
//#define ENCODER_PPR (u16)(10) // number of pulses per revolution
#define COUNTER_RESET (u16)0
#define ICx_FILTER (u8) 6 // 6<-> 670nsec///问题1:这是怎么得来的
extern u16 n_counter;
/*
stm32的编码器模式其实就是捕获模式,只不过是把编码器的两路AB信号接到定时器的输入脚。
*/
static void TIM4_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;/* Encoder unit connected to TIM3, 4X mode */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能TIM时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟
GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;//PA6 PA7浮空输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(ENCODER_TIMER);/* Timer configuration in Encoder mode */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling //不分频,陈锡爱
TIM_TimeBaseStructure.TIM_Period = 390*4; //设定计数器重装值 TIMx_ARR = 390*4
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置时钟分割 T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数,陈锡爱
TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数
TIM_ICStructInit(&TIM_ICInitStructure);//将结构体中的内容缺省输入
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; // 为什么是6???,选择输入比较滤波器
TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);//将TIM_ICInitStructure中的指定参数初始化TIM4
// TIM_ARRPreloadConfig(TIM4, ENABLE);//使能预装载
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);//清除TIM3的更新标志位
TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
TIM4->CNT = COUNTER_RESET;//Reset counter
//ENC_Clear_Speed_Buffer();
TIM_Cmd(ENCODER_TIMER, ENABLE); //启动TIM定时器
}
void TIM4_Init(void)
{
TIM4_Mode_Config();
}
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查TIM4更新中断发生与否
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx更新中断标志
n_counter++;
}
}
[/mw_shl_code]
一周热门 更多>