#include "encoder.h"
#include "led.h"
void Encoder_TIM2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//¿aÆôê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;//3õê¼»ˉGPIO--PA0¡¢PA1
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0 |GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);//3õê¼»ˉ¶¨ê±Æ÷
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//2»·ÖÆμ
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//ÏòéϼÆêy
TIM_TimeBaseInitStruct.TIM_Period=65535;//ÖØ×°3õÖμ
TIM_TimeBaseInitStruct.TIM_Prescaler=0;//·ÖÆμÏμêy
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct); //ÅäÖö¨ê±Æ÷
TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//ÅäÖñàÂëÆ÷ģ꽣¨¶¨ê±Æ÷2£¬Ä£ê½3£¬éÏéyÑØ£¬éÏéyÑØ£©
TIM_ICStructInit(&TIM_ICInitStruct);//3õê¼»ˉêäèë2¶»ñ
TIM_ICInitStruct.TIM_ICFilter=10;//ÂË2¨Æ÷
TIM_ICInit(TIM2,&TIM_ICInitStruct);//êäèë2¶»ñÅäÖÃ
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//ÅäÖÃòç3ö¸üDÂÖD¶Ï±ê־λ
TIM_SetCounter(TIM2,0);//Çåá㶨ê±Æ÷¼ÆêyÖμ
TIM_Cmd(TIM2,ENABLE);//¿aÆô¶¨ê±Æ÷
}
void Encoder_TIM4_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6 |GPIO_Pin_7;
GPIO_Init(GPIOB,&GPIO_InitStruct);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=65535;
TIM_TimeBaseInitStruct.TIM_Prescaler=0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStruct);
TIM_ICInitStruct.TIM_ICFilter=10;
TIM_ICInit(TIM4,&TIM_ICInitStruct);
TIM_ClearFlag(TIM4,TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_SetCounter(TIM4,0);
TIM_Cmd(TIM4,ENABLE);
}
/**********************
±àÂëÆ÷Ëù¶è¶áè¡oˉêy
èë¿ú2Îêy£o¶¨ê±Æ÷
**********************/
int Read_Speed(int TIMx)
{
int value_1;
switch(TIMx)
{
case 2:value_1=(short)TIM_GetCounter(TIM2);TIM_SetCounter(TIM2,0);break;//IFêǶ¨ê±Æ÷2£¬1.2é¼ˉ±àÂëÆ÷μļÆêyÖμ2¢±£′æ¡£2.½«¶¨ê±Æ÷μļÆêyÖμÇåáã¡£
case 4:value_1=(short)TIM_GetCounter(TIM4);TIM_SetCounter(TIM4,0);break;
default:value_1=0;
}
return value_1;
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=0)//èç1ûÖD¶Ï±ê־λÎa1£¬Ôò½«′ËλÇåáã
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
// LED0=0;
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=0)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
LED0=0;
}
}
|