#include "encode.h"
#include "sys.h"
#include"usart.h"
#include "led.h"
int Encoder_Timer_Overflow=0; //±àÂëÆ÷Òç³ö´ÎÊý£¨Ã¿389*4Òç³öÒ»´Î£©
u16 Previous_Count; //ÉÏ´ÎTIM3->CNTµÄÖµ
void Encoder_Init_TIM2(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;//¶¨ÒåÒ»¸öGPIOÊý¾Ý½á¹¹£¬´æ·ÅÉèÖõIJÎÊý
NVIC_InitTypeDef NVIC_InitStructure;//NVIC³õʼ»¯½á¹¹¶¨Òå
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //¶¨ÒåÒ»¸ö¶¨Ê±Æ÷³õʼ»¯Êý¾Ý½á¹¹Ì壬´æ·ÅÉèÖõIJÎÊý,²úÉúPWM²¨Ê±ÓÃ
TIM_ICInitTypeDef TIM_ICInitStructure; // TIMÊäÈ벶»ñ³õʼ»¯½á¹¹¶¨Òå
//ʱÖÓʹÄÜ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//ʹÄܶ¨Ê±Æ÷2µÄʱÖÓ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ʹÄ&UumlA¶Ë¿ÚʱÖÓ
//³õʼ»¯IO¿Ú£¬Ä£Ê½¸´ÓÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´ÓÃģʽ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //¸¡¿Õ,ÎÞÉÏÀ ºÍÏÂÀ µç×è
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOA³õʼ»&macrA6º&IacuteA7
//ÉèÖÃÒý½Å¸´ÓÃÓ³Éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);
//³õʼ»¯¶¨Ê±Æ÷ARR,PSC
TIM_TimeBaseStructure.TIM_Period = arr; //(±àÂëÆ÷ÏßÊý-1)*4 Ëı¶ÆµÔ Àí
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨Ê±Æ÷·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //ʱÖÓ·ÖƵÒò×Ó£¬²»·ÖƵ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷timxʱ»ùµ¥ÔªÍâÉèTIM_TimeBaseInitÖеÄÖ¸¶¨²ÎÊý¡£
//³õʼ»¯ÊäÈ벶»ñͨµÀ
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1; //Ñ¡ÔñÊäÈë¶ËIC1Ó³Éäµ½TI1ÉÏ
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising; //ÉÏÉýÑز¶»ñ
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI; //Ó³Éäµ½TI1ÉÏ
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1; //ÅäÖÃÊäÈë·ÖƵ,²»·ÖƵ
TIM_ICInitStructure.TIM_ICFilter =6; //ÅäÖÃÊäÈëÂ˲¨Æ÷
TIM_ICInit(TIM2, &TIM_ICInitStructure);//¸ù¾ÝTIM_ICInitStructÖеÄÖ¸¶¨²ÎÊý³õʼ»¯TIMÍâΧÉ豸
TIM_ICInitStructure.TIM_Channel=TIM_Channel_2; //Ñ¡ÔñÊäÈë¶ËIC1Ó³Éäµ½TI1ÉÏ
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising; //ÉÏÉýÑز¶»ñÊäÈ벶»ñ¼«ÐÔ
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI; //ͨµÀÖ±½ÓÓ³Éäµ½TI1ÉÏ
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1; //ÅäÖÃÊäÈë·ÖƵ,²»·ÖƵ
TIM_ICInitStructure.TIM_ICFilter =6; //ÅäÖÃÊäÈëÂ˲¨Æ÷£¬
TIM_ICInit(TIM2, &TIM_ICInitStructure);//¸ù¾ÝTIM_ICInitStructÖеIJÎÊý³õʼ»¯ÍâÉèTIMxÊäÈ벶»ñͨµÀº¯Êý
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//ʹÓñàÂëÆ÷ģʽ3//ÅäÖö¨Ê±Æ÷±àÂëÆ÷½Ó¿//±àÂëÆ÷ÅäÖ㨶¨Ê±Æ÷¡¢±àÂëģʽ¡¢ÉÏÉýÑØ¡¢ÉÏÉýÑØ£©Ú
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; //¶¨Ê±Æ÷2ÖжϷÖ×éÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //ʹÄÜ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //ÇÀÕ¼ÓÅÏȼ¶1
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0x02; //ÏìÓ¦ÓÅÏȼ¶2
NVIC_Init(&NVIC_InitStructure); //ÅäÖö¨Ê±Æ÷2
//¿ªÆô²¶»ñÖжÏ,¸üÐÂÖжÏ
//TIM_ITConfig(TIM2, TIM_IT_Update|TIM_IT_CC1, ENABLE);//ʹÄܶ¨Ê±Æ÷ÖжÏ
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);//ʹÄܶ¨Ê±Æ÷ÖжÏ
//ʹÄܶ¨Ê±Æ÷
//TIM_SetCounter(TIM2,0);//ÉèÖÃTIMx ¼ÆÊýÆ÷¼Ä´æÆ÷Öµ
TIM_Cmd(TIM2, ENABLE); //ʹÄܶ¨Ê±Æ÷
}
//Öжϴ¦Àíº¯ÊýΪ¿Õ£¬Çå³ýÖжϱê־λºó½áÊøÖжÏ
//±àдÖжϷþÎñº¯Êý
void TIM2_IRQHandler(void)
{
if(TIM_GetFlagStatus(TIM2,TIM_FLAG_Update)==SET)//Òç³öÖжÏEXTI_GetITStatus(EXTI_Line3)!-=RESETÅжÏÊÇ·ñÖÃλ
{
LED1=!LED1;
LED0=!LED0;
Encoder_Timer_Overflow++; //±àÂëÆ÷Òç³ö
}
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//Çå³ýÖжϱê־λ
}
u32 Read_Encoder(void)
{
u32 Count; //Ò»¶Îʱ¼äÄÚת¹ýµÄÂö³åÊý
u16 Current_Count; //µ±Ç°TIM3->CNTµÄÖµ
u16 Enc_Timer_Overflow_one;
Enc_Timer_Overflow_one=Encoder_Timer_Overflow;
Current_Count = TIM_GetCounter(TIM2); //»ñµÃµ±Ç°TIM3->CNTµÄÖµ
printf("¶¨Ê±Æ÷¼ÆÊýÆ÷µÄֵΪ£º%d
",Current_Count);
// Encoder_Timer_Overflow=0; //ÇåÁ㣬·½±ãÏ´μÆËã
Count = (u32)(Current_Count - Previous_Count + (Enc_Timer_Overflow_one) * (4*ENCODER_PPR)); //¼ÆËã³öÒ»¸öʱ¼äת¹ýµÄÂö³åÊý
Previous_Count = Current_Count;
Encoder_Timer_Overflow=0;
return(Count);
} |