STM32——高級(jí)定時(shí)器的應(yīng)用
/定時(shí)器1的配置 輸出兩路PWM信號(hào) PA8 PA11,計(jì)數(shù)時(shí)鐘為400KHZ 計(jì)數(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 ; // 定時(shí)器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ǔ)上,對(duì)定時(shí)器1進(jìn)行配置
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; // 重復(fù)計(jì)數(shù)器無(wú)效 */
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; // 向上計(jì)數(shù) */
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // 時(shí)鐘分頻 */
TIM_TimeBaseInitStruct.TIM_Prescaler = 180-1; // 預(yù)分頻系數(shù)為180-1,計(jì)數(shù)器時(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);
}