初中生
最后登录1970-1-1
在线时间 小时
注册时间2020-3-30
|
void Tim_Encode_Test(void)
{
s32 TIM3_Old_Count;
TIM3_Init_Encode();
while(1)
{
TIM3_Count=TIM3_Get_Encode();
if(TIM3_Old_Count != TIM3_Count)
{
TIM3_Old_Count = TIM3_Count;
printf("脉冲=%ld 圈数=%8ld 位置=%d 脉冲圈=%d\r\n"\
,TIM3_Count,TIM3_Circle_Count,TIM3->CNT,TIM3_Encoder_Line_Number*TIM3_Encoder_Line_Multiple);
}
}
}
s32 TIM3_Count,TIM3_Circle_Count,TIM3_Encoder_Line_Multiple;//×ܼÆÊý¡¢È¦Êý¼ÆÊý¡¢Âö³å±¶ÆµÏµÊý(һȦÂö³åÊý=TIM3_Encoder_Line_Number*TIM3_Encoder_Line_Multiple)
void TIM3_Init_Encode(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_ICStructInit(&TIM_ICInitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/*- Õý½»±àÂëÆ÷ÊäÈëÒý½Å PA->6 PA->7 -*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_DeInit(TIM3);
if (TIM3_ENCODER_MODE<TIM_EncoderMode_TI12) TIM3_Encoder_Line_Multiple=2;
else TIM3_Encoder_Line_Multiple=4;
TIM_TimeBaseStructure.TIM_Period = TIM3_Encoder_Line_Number*TIM3_Encoder_Line_Multiple-1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM3_ENCODER_MODE, TIM_ICPolarity_Rising ,TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM3->CNT = 0;
TIM_Cmd(TIM3, ENABLE);
}
s32 TIM3_Get_Encode(void)
{
return TIM3_Circle_Count*TIM3_Encoder_Line_Number*TIM3_Encoder_Line_Multiple+(TIM3->CNT);
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
if((((TIM3->CR1)>>4) & 0x01)==0)TIM3_Circle_Count++;//DIR==0
else TIM3_Circle_Count--;//DIR==1
}
}
|
|