forked from logzhan/RobotHardware-UESTC
230 lines
8.9 KiB
C
230 lines
8.9 KiB
C
|
/*
|
|||
|
********************************************************************************************************
|
|||
|
<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bsp_timer.c
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD>
|
|||
|
<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD>timer3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pwm,<EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>timer3<EFBFBD><EFBFBD>ch3<EFBFBD><EFBFBD>ch4<EFBFBD><EFBFBD><EFBFBD><EFBFBD> PB0-PB1<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ܶ<EFBFBD>PWMA<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> PWMB<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
timer1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ100ms<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
timer2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
timer4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
#include "bsp_timer.h"
|
|||
|
|
|||
|
//***************************<2A><>ʱ<EFBFBD><CAB1>3<EFBFBD><33>ʼ<EFBFBD><CABC> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṩPWM***************************//
|
|||
|
// TIM_Period / Auto Reload Register(ARR) = 1000 TIM_Prescaler--71
|
|||
|
//arr<72><72><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>psc<73><63>Ƶϵ<C6B5><CFB5>
|
|||
|
//PWM<57><4D>Ƶ<EFBFBD><C6B5> = 72MHz/ARR/PCS <20><><EFBFBD><EFBFBD> 20K = 72M/3600/1 = 20K
|
|||
|
|
|||
|
|
|||
|
//=====<3D><>ʼ<EFBFBD><CABC>PWM 20KHZ <20><>Ƶ<EFBFBD><C6B5><EFBFBD>Է<EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶʱ<C6B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// ARR= 3599 ʱƵ<CAB1><C6B5>Ϊ20Khz
|
|||
|
//PB0<42><30><EFBFBD><EFBFBD>PWMA--right moto<74><6F>PB1<42><31><EFBFBD><EFBFBD>PWMB--left moto<74><6F>STBYֱ<59><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//arr<72><72><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>psc<73><63>Ƶϵ<C6B5><CFB5>
|
|||
|
//PWM<57><4D>Ƶ<EFBFBD><C6B5> = 72MHz/ARR/PCS <20><><EFBFBD><EFBFBD> 20K = 72M/3600/1 = 20K
|
|||
|
void MOTO_PWM_Init(u32 arr, int psc)
|
|||
|
{
|
|||
|
TIM_OCInitTypeDef TIM_OCInitSructure;
|
|||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|||
|
|
|||
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
|||
|
TIM_TimeBaseStructure.TIM_Period = arr-1; //<2F>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ壨<D6B5><E5A3A8><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>)
|
|||
|
TIM_TimeBaseStructure.TIM_Prescaler = psc; //ʱ<>ӷ<EFBFBD>Ƶϵ<C6B5><CFB5>
|
|||
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //<2F><><EFBFBD>ⲿʱ<E2B2BF>ӽ<EFBFBD><D3BD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӷ<EFBFBD>Ƶ
|
|||
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //<2F><><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>
|
|||
|
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
|
|||
|
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
|
|||
|
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
|
|||
|
//<2F><><EFBFBD><EFBFBD>pwm<77><6D><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
|
|||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1;
|
|||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>3 pwm<77><6D><EFBFBD><EFBFBD>
|
|||
|
TIM_OCInitSructure.TIM_OCMode = TIM_OCMode_PWM1;
|
|||
|
TIM_OCInitSructure.TIM_OutputState= TIM_OutputState_Enable;
|
|||
|
TIM_OCInitSructure.TIM_Pulse = 0;//ռ<>ձ<EFBFBD>= 50/100
|
|||
|
TIM_OCInitSructure.TIM_OCPolarity = TIM_OCPolarity_High;//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵС<D6B5><D0A1>CCR1_ValʱΪ<CAB1>ߵ<EFBFBD>ƽ
|
|||
|
TIM_OC3Init(TIM3, &TIM_OCInitSructure);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
TIM_OC3PolarityConfig(TIM3, TIM_OCPreload_Enable);//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>pwm
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>4 pwm<77><6D><EFBFBD><EFBFBD>
|
|||
|
TIM_OCInitSructure.TIM_OCMode = TIM_OCMode_PWM1;
|
|||
|
TIM_OCInitSructure.TIM_OutputState= TIM_OutputState_Enable;
|
|||
|
TIM_OCInitSructure.TIM_Pulse = 0;//ռ<>ձ<EFBFBD>= 50/100
|
|||
|
TIM_OCInitSructure.TIM_OCPolarity = TIM_OCPolarity_High;//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵС<D6B5><D0A1>CCR1_ValʱΪ<CAB1>ߵ<EFBFBD>ƽ
|
|||
|
TIM_OC4Init(TIM3, &TIM_OCInitSructure);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
TIM_OC4PolarityConfig(TIM3, TIM_OCPreload_Enable);//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>pwm
|
|||
|
|
|||
|
TIM_ARRPreloadConfig(TIM3, ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ
|
|||
|
TIM_Cmd(TIM3, ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//ռ<>ձ<EFBFBD> = TIMx_CCRx / TIMx_ARR
|
|||
|
//moto_r<5F><72><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>moto_l<5F><6C><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>. <20><>ֵ 0-100
|
|||
|
void MOTO_PWM_Out(u16 moto_l, u16 moto_r)
|
|||
|
{
|
|||
|
|
|||
|
TIM_OCInitTypeDef TIM_OCInitSructure;
|
|||
|
|
|||
|
TIM_OCInitSructure.TIM_OCMode = TIM_OCMode_PWM1;
|
|||
|
TIM_OCInitSructure.TIM_OutputState= TIM_OutputState_Enable;
|
|||
|
//CH3 <20>ҵ<EFBFBD><D2B5><EFBFBD>
|
|||
|
TIM_OCInitSructure.TIM_Pulse = moto_l;//ռ<>ձ<EFBFBD>= ccr/100
|
|||
|
TIM_OC3Init(TIM3, &TIM_OCInitSructure);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
TIM_OC3PolarityConfig(TIM3, TIM_OCPreload_Enable);//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>pwm
|
|||
|
|
|||
|
//CH4 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
TIM_OCInitSructure.TIM_Pulse = moto_r;//ռ<>ձ<EFBFBD>= ccr /100
|
|||
|
TIM_OC4Init(TIM3, &TIM_OCInitSructure);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
TIM_OC4PolarityConfig(TIM3, TIM_OCPreload_Enable);//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>pwm
|
|||
|
|
|||
|
TIM_ARRPreloadConfig(TIM3, ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//***************************<2A><>ʱ<EFBFBD><CAB1>1<EFBFBD><31>ʼ<EFBFBD><CABC> ϵͳÿ10ms<6D><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>pwm<77><6D>***************************//
|
|||
|
void Timer6_Init(void)
|
|||
|
{
|
|||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|||
|
|
|||
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|||
|
|
|||
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
|
|||
|
TIM_TimeBaseStructure.TIM_Period = 9999;//<2F>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ壨<D6B5><E5A3A8><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>)
|
|||
|
TIM_TimeBaseStructure.TIM_Prescaler = 719;//ʱ<>ӷ<EFBFBD>Ƶϵ<C6B5><CFB5>
|
|||
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//<2F><><EFBFBD>ⲿʱ<E2B2BF>ӽ<EFBFBD><D3BD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӷ<EFBFBD>Ƶ
|
|||
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//<2F><><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>
|
|||
|
TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //<2F><EFBFBD><DFBC><EFBFBD>ʱ<EFBFBD><CAB1>1<EFBFBD><31><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ſ<EFBFBD><C5BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Ƶ<EFBFBD><C6B5>һ<EFBFBD><D2BB>ʼ<EFBFBD><CABC>72mhz ֵ<><D6B5>ע<EFBFBD><D7A2><EFBFBD>ĵط<C4B5>
|
|||
|
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseStructure);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
|
|||
|
TIM_ClearFlag(TIM6, TIM_FLAG_Update);
|
|||
|
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
|
|||
|
|
|||
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
|
|||
|
NVIC_InitStructure.NVIC_IRQChannel=TIM6_IRQn;
|
|||
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=5;
|
|||
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
|
|||
|
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
|
|||
|
NVIC_Init(&NVIC_InitStructure);
|
|||
|
|
|||
|
TIM_Cmd(TIM6, ENABLE);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
}
|
|||
|
|
|||
|
//***************************<2A><>ʱ<EFBFBD><CAB1>2<EFBFBD><32>ʼ<EFBFBD><CABC> <20><>ʹ<EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>***************************//
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//PA0----<2D><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><41> <20><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B2B<32><42>ʶ
|
|||
|
//PA1----<2D><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD><42> <20><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B2A<32><41>ʶ
|
|||
|
void Encoder_Init_TIM2(void)
|
|||
|
{
|
|||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|||
|
TIM_ICInitTypeDef TIM_ICInitStructure;
|
|||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|||
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1>4<EFBFBD><34>ʱ<EFBFBD><CAB1>
|
|||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ʹ<><CAB9>PB<50>˿<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
|||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //<2F>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GPIO_Init(GPIOA, &GPIO_InitStructure); //<2F><><EFBFBD><EFBFBD><EFBFBD>趨<EFBFBD><E8B6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>GPIOB
|
|||
|
|
|||
|
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
|
|||
|
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // Ԥ<><D4A4>Ƶ<EFBFBD><C6B5>
|
|||
|
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //<2F>趨<EFBFBD><E8B6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װֵ
|
|||
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//ѡ<><D1A1>ʱ<EFBFBD>ӷ<EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ
|
|||
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM<49><4D><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>
|
|||
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|||
|
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//ʹ<>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>ģʽ3
|
|||
|
TIM_ICStructInit(&TIM_ICInitStructure);
|
|||
|
TIM_ICInitStructure.TIM_ICFilter = 10;
|
|||
|
TIM_ICInit(TIM2, &TIM_ICInitStructure);
|
|||
|
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//<2F><><EFBFBD><EFBFBD>TIM<49>ĸ<EFBFBD><C4B8>±<EFBFBD>־λ
|
|||
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|||
|
//Reset counter
|
|||
|
TIM_SetCounter(TIM2,0);
|
|||
|
TIM_Cmd(TIM2, ENABLE);
|
|||
|
}
|
|||
|
|
|||
|
//***************************<2A><>ʱ<EFBFBD><CAB1>4<EFBFBD><34>ʼ<EFBFBD><CABC> <20><>ʹ<EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>***************************//
|
|||
|
//<2F>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//PB6----<2D><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD><42> <20><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B1A<31><41>ʶ
|
|||
|
//PB7----<2D><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><41> <20><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B1B<31><42>ʶ
|
|||
|
void Encoder_Init_TIM4(void)
|
|||
|
{
|
|||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|||
|
TIM_ICInitTypeDef TIM_ICInitStructure;
|
|||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|||
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1>4<EFBFBD><34>ʱ<EFBFBD><CAB1>
|
|||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//ʹ<><CAB9>PB<50>˿<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
|||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //<2F>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GPIO_Init(GPIOB, &GPIO_InitStructure); //<2F><><EFBFBD><EFBFBD><EFBFBD>趨<EFBFBD><E8B6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>GPIOB
|
|||
|
|
|||
|
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
|
|||
|
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // Ԥ<><D4A4>Ƶ<EFBFBD><C6B5>
|
|||
|
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //<2F>趨<EFBFBD><E8B6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װֵ
|
|||
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//ѡ<><D1A1>ʱ<EFBFBD>ӷ<EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ
|
|||
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM<49><4D><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD>
|
|||
|
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
|
|||
|
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//ʹ<>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>ģʽ3
|
|||
|
TIM_ICStructInit(&TIM_ICInitStructure);
|
|||
|
TIM_ICInitStructure.TIM_ICFilter = 10;
|
|||
|
TIM_ICInit(TIM4, &TIM_ICInitStructure);
|
|||
|
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//<2F><><EFBFBD><EFBFBD>TIM<49>ĸ<EFBFBD><C4B8>±<EFBFBD>־λ
|
|||
|
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
|
|||
|
//Reset counter
|
|||
|
TIM_SetCounter(TIM4,0);
|
|||
|
TIM_Cmd(TIM4, ENABLE);
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/**************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>λʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ֵ<EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>ֵ
|
|||
|
**************************************************************************/
|
|||
|
int Read_Encoder(u8 TIMX)
|
|||
|
{
|
|||
|
int Encoder_TIM;
|
|||
|
switch(TIMX)
|
|||
|
{
|
|||
|
case 2: Encoder_TIM= (short)TIM2 -> CNT; TIM2 -> CNT=30000;break;
|
|||
|
case 3: Encoder_TIM= (short)TIM3 -> CNT; TIM3 -> CNT=30000;break;
|
|||
|
case 4: Encoder_TIM= (short)TIM4 -> CNT; TIM4 -> CNT=30000;break;
|
|||
|
default: Encoder_TIM=0;
|
|||
|
}
|
|||
|
return Encoder_TIM;
|
|||
|
}
|
|||
|
|
|||
|
/**************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>TIM4<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
**************************************************************************/
|
|||
|
void TIM4_IRQHandler(void)
|
|||
|
{
|
|||
|
if(TIM4->SR&0X0001)//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
{
|
|||
|
}
|
|||
|
TIM4->SR&=~(1<<0);//<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|||
|
}
|
|||
|
/**************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>TIM2<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
**************************************************************************/
|
|||
|
void TIM2_IRQHandler(void)
|
|||
|
{
|
|||
|
if(TIM2->SR&0X0001)//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
{
|
|||
|
}
|
|||
|
TIM2->SR&=~(1<<0);//<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|||
|
}
|