初中生
最后登录1970-1-1
在线时间 小时
注册时间2017-2-17
|
最近在做一个项目,就是遇到读取编码器脉冲数值的问题,做法是用基本定时器6定时每100ms读取一次采集的脉冲数,使用通用定时器4的编码器模式进行脉冲的捕捉,编码器是1000线的,但是转速很慢,目前我在测试代码。并没有使用编码器来测试而是使用单片机输出PWM波来进行测试,具体是使用定时器3输出PWM,利用信号线将PWM连接到PB6引脚,PWM的周期是1ms,我使用定时器6定时100ms,而100ms内定时器4只能捕捉到100次脉冲,我再定时器6的中断函数中读取定时器4的计数器的值,原理上每次读取的值应该为100,但是每次读取到的值都不对,不知道错误在哪。恳请大神指点
#include "TimerConfig.h"
static void TIM6_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM6_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);
TIM_TimeBaseStructure.TIM_Period = 9999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
//TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseStructure);
TIM6_NVIC_Configuration();
TIM_ClearFlag(TIM6, TIM_FLAG_Update);
//TIM_UpdateRequestConfig(TIM6,TIM_UpdateSource_Regular);
TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM6,ENABLE);
}
void TIM4_Mode_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
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_ResetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7);
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 9999 ;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
//ÅäÖñàÂëÆ÷Ä£ê½′¥·¢Ô′oí¼«DÔ
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
//TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
//TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
//TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
//TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIM4,&TIM_ICInitStructure);
//TIMx_NVIC_Configuration();
TIM_ITConfig(TIM4, TIM_IT_Update |TIM_IT_CC1, ENABLE);
TIM4->CNT = 0;
TIM_Cmd(TIM4,ENABLE);
}
void TIM6_IRQHandler(void)
{
if ( TIM_GetITStatus ( TIM6, TIM_IT_Update) != RESET )
{
Value = TIM_GetCounter(TIM4);
printf("\r\n The current value = 0x%04X \r\n", Value);
TIM_SetCounter(TIM4,0);
TIM_ClearITPendingBit ( TIM6, TIM_FLAG_Update );
}
}
|
|