请高人讲一下STM32 定时器的编码器接口部分,计数方向与编码器信号关系图怎么看,搞不懂啊!

2019-08-17 00:20发布

 

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
24条回答
HC0821
2019-08-19 04:07
回复【15楼】kkingkeen:
---------------------------------
如果还需要的话,代码如下:
void TIM_Configuration() //PWM???? ??????·???????±à???ù?? ??????·???
{
TIM_ICInitTypeDef  TIM_ICInitStructure;   //hc
TIM_OCInitTypeDef  TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

/*Configure TIM1_CH1 ~ CH3 and TIM8_CH3 as 4 WM output channels*/
/*TIM base Configuration*/
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //?ò??????
TIM_TimeBaseInitStructure.TIM_Period = 3599; //TIMx_ARR??????20k
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;            //??×????¨?÷??·???
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitStructure);  //±??????????±??·????ò×?tDTS = tCK_INT

/* TIM1_CH1/2/3 and TIM8_CH3 Configuration in WM1 mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //TIM_OutputState_Enable
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //CHxN????????????????????????·??òCHx???á???¨??????
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);

/* TIM1, TIM8 enable counter */
TIM_Cmd(TIM1, ENABLE);  
TIM_Cmd(TIM8, ENABLE); //TIMx_CR1_0/CEN=1,
/* TIM1, TIM8 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);  //TIMx_BDTR_15/MOE=1: ?÷???????? 


/***************************************TIM2-TIM5??????????****************************************************/
// /*Configure TIM2 TIM3 TIM4 TIM5 as Encoder Interfaces input*/
// /*TIM base Configuration*/
//    TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
//    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
//    TIM_TimeBaseInitStructure.TIM_Period = 65535;
//    TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
//
// /*?¤·???=1???ò??????????·?????×?????×°??=65535???í???ú?ú?????ü???????¨UG??*/
// TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
//    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
//
// /*±à???÷????3???¨??1??2??????????????TI1??2????        IC1??????????????????      IC2????????????????*/
// TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); 
// TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
//
//    /* TIM2 3 4 enable counter */
//    TIM_Cmd(TIM2, ENABLE);
//    TIM_Cmd(TIM3, ENABLE);
// TIM_Cmd(TIM4, ENABLE); 
// TIM_Cmd(TIM5, ENABLE); 
/***********??????????TIM5????????????????????USART2??ADC12??????????????????????TIM5 CH1??2??·¨??????PA0??1***************/


//////////////////////TIM2-TIM5????????????TIM_ICInit(TIMx, &TIM_ICInitStructure);////////////////////
TIM_DeInit(TIM2); TIM_DeInit(TIM3); TIM_DeInit(TIM4); TIM_DeInit(TIM5);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);

TIM_TimeBaseInitStructure.TIM_Prescaler = 0x0; // No prescaling 
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 0xffff;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);

TIM2->CR2 &= 0xFF7F; TIM3->CR2 &= 0xFF7F; TIM4->CR2 &= 0xFF7F; TIM5->CR2 &= 0xFF7F; //TI1S=0,TIM5_CH1??????????TI1??????

TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); 
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); 
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);     
TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
 
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0x06;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInit(TIM5, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInit(TIM5, &TIM_ICInitStructure);

//////////////????·??????????????????????????è??????????????///////////////
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM2->CNT = 0x0000;TIM3->CNT = 0x0000;TIM4->CNT = 0x0000;TIM5->CNT = 0x0000; 
//////////////////????????????????????????????×???????????/////////////////

TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM4, ENABLE);TIM_Cmd(TIM5, ENABLE);
//////////////////////????????TIM5????PA0??PA1??CH1??CH2????????????????·???????////////////////////////////////


/*config TIM6 as sample timer interrupt*/
TIM_TimeBaseInitStructure.TIM_Prescaler = 0x47;  //71
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 999; //1000 us  ??????????????????1000us??
//??????????????????????????????????????????·???SampleTime?????ú????????????
/*?¤·?????72MHZ/(71+1)=1MHZ,×?????×°????????500us???*/
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStructure); //?¨?±?÷TIM6??TIM2345???ù???ú????????,?¨?????íPID,?ü??PWM
//  TIM_Cmd(TIM6, ENABLE);//TIM6????·¨?¨?±?÷??·????÷???ò??????

TIM_ARRPreloadConfig(TIM6, ENABLE);  //TIM6_CR1_7/ARPE=1,TIMx_ARR?????÷???????? 
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);  //TIM6_SR_0/UIF=0???í???????ü??????±ê????
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //enable tim6 count overflow interrupt  TIM6_DIER_0/UIE=1???????ü??????
}


GPIO的

/*configure TIM2_CH1/CH2,remap to A15/PB3*/  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;    //PA15??TIM2_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // B3 ??TIM2_CH2
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);//AFIO_MAPR_TIM2_REMAP[1:0]/[9:8]=01;TIM2????????????·?????1 CH1/PA15??CH2/PB3
 
/*Confiture TIM3_CH1 & CH2 as Encoder Interfaces, Remaped on C6 & C7*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);  //TIM3???ê??????????CH1=PC6??CH2=PC7

/*Configure TIM4_CH1 (PD12) & TIM4_CH2 (PD13) as Encoder Interfaces*/  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_Init(GPIOD,&GPIO_InitStructure);  //TIM4_REMAP=0??????????????CH1/CH2????????????????PB6/PB7????
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);

/*Configure TIM5_CH1 (PA0) & TIM5_CH2 (PA1) as Encoder Interfaces*/  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStructure);  //TIM5_CH1/CH2,??????????????????????PA0/PA1


定时采集函数的语句
void TIM6_IRQHandler() //?¨?±1000us
{
if(TIM_GetITStatus(TIM6, TIM_IT_Update) == SET)  //TIM6?????òif????
{
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);  //TIM6_SR_0/UIF=0,????????±ê????

speed_fb[0] =  TIM_GetCounter(TIM2);  //???ù???????ú????·??????????¨????????????????
TIM_SetCounter(TIM2, 0);   //???????ú?????÷TIMx_CNT????????????????        
speed_fb[1] =  TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3, 0);
speed_fb[2] =  TIM_GetCounter(TIM4);
TIM_SetCounter(TIM4, 0);
speed_fb[3] =  TIM_GetCounter(TIM5);
TIM_SetCounter(TIM5, 0);

一周热门 更多>