最近正在用STM32的定时器进行编码器的配置进行脉冲的读取,在网上查很多资料,我配置后通过软件仿真但总是没有反应,计数器的值没有什么变化,求高人指点迷津。
配置程序如下:
#include "stm32f10x_lib.h"
#define ENCODER_TIMER TIM3
#define ENCODER_PPR (u16)(500)
#define ENCODER_TIM_PERIOD 2000
#define MAX_COUNT 2300
#define SPEED_SAMPLING_TIME 4000
#define COUNTER_RESET (u16)0
#define ICx_FILTER (u8) 6
#define TIMx_PRE_EMPTION_PRIORITY 1
#define TIMx_SUB_PRIORITY 0
#define SPEED_SAMPLING_FREQ ((u16)(2000/(SPEED_SAMPLING_TIME+1)))
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
volatile s32 CurrentCount ;
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
void GPIO_Configuration(void);
void ENC_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Encoder unit connected to TIM3, 4X mode */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM3 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable the TIM3 Update Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
//TIM_EncoderMode:触发源 TIM_ IC1Polarity:TI1 极性 TIM_ IC2Polarity:TI2 极性
TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIMx:x 可以是 2,3 或者 4,来选择 TIM 外设
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; //设定滤波值
TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
//Reset counter
TIM3->CNT = COUNTER_RESET;
// ENC_Clear_Speed_Buffer();
TIM_Cmd(ENCODER_TIMER, ENABLE);
}
int main(void)
{
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
ENC_Init() ;
while(1)
{
CurrentCount= TIM_GetCounter(TIM3);
}
} |