/定時器1的配置 輸出兩路PWM信號 PA8 PA11,計數(shù)時鐘為400KHZ 計數(shù)溢出大小為40000 /
void Tim1Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
u16 LeftMotor_CCR;
u16 RightMotor_CCR;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 ; // 定時器1 通道1和4
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
LeftMotor_CCR = RightMotor_CCR = 2000;
//在APB2設(shè)置為72MHZ的基礎(chǔ)上,對定時器1進行配置
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; // 重復(fù)計數(shù)器無效 */
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; // 向上計數(shù) */
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 時鐘分頻 */
TIM_TimeBaseInitStruct.TIM_Prescaler = 180-1; // 預(yù)分頻系數(shù)為180-1,計數(shù)器時鐘為72MHz/180 = 400KHz */
TIM_TimeBaseInitStruct.TIM_Period = 40000-1;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_Pulse = LeftMotor_CCR;
TIM_OC1Init(TIM1, &TIM_OCInitStruct);
TIM_OCInitStruct.TIM_Pulse = RightMotor_CCR;
TIM_OC4Init(TIM1, &TIM_OCInitStruct);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}