/* 初始化上层控制电机两个IO口 B0 B1 */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); //使能时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* 上层电机 B0 B1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 下层电机 C6 C7 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* 初始化TIM模块 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3时钟
TIM_TimeBaseStructure.TIM_Prescaler=0;//预分频为0
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_Period=10285;
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分频系数为0
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //TIM3初始化
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//模式1
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//正向通道输出使能
TIM_OCInitStructure.TIM_Pulse = 2400;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //正向通道输出极性
TIM_OC1Init(TIM3, &TIM_OCInitStructure);//使能通道1
TIM_OC2Init(TIM3, &TIM_OCInitStructure);//使能通道2
TIM_OC3Init(TIM3, &TIM_OCInitStructure);//使能通道3
TIM_OC4Init(TIM3, &TIM_OCInitStructure);//使能通道4
TIM_Cmd(TIM3, ENABLE); |