You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.6 KiB

#include "motor.h"
void Motor_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹ<EFBFBD><EFBFBD>PB<EFBFBD>˿<EFBFBD>ʱ<EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_14; //<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
Motor_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //ʹ<EFBFBD><EFBFBD>GPIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>TIM1 CH1 CH4<EFBFBD><EFBFBD>PWM<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //TIM_CH1 //TIM_CH4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //<EFBFBD>һ¼<EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>ؼĴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //<EFBFBD><EFBFBD><EFBFBD>ΪTIMxʱ<EFBFBD><EFBFBD>Ƶ<EFBFBD>ʳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD>Ƶֵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӷָ<EFBFBD>:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM<EFBFBD><EFBFBD><EFBFBD>ϼ<EFBFBD><EFBFBD><EFBFBD>ģʽ
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TIM_TimeBaseInitStruct<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>TIMx<EFBFBD><EFBFBD>ʱλ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ģʽ:TIM<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD>ģʽ1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //<EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
TIM_OCInitStructure.TIM_Pulse = 0; //<EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD>ȽϼĴ<EFBFBD><EFBFBD>ֵ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //:TIM<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚϼ<EFBFBD><EFBFBD>Ը<EFBFBD>
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TIM_OCInitStruct<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>TIMx
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TIM_OCInitStruct<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>TIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1Ԥװ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH4Ԥװ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
TIM_ARRPreloadConfig(TIM1, ENABLE); //ʹ<EFBFBD><EFBFBD>TIMx<EFBFBD><EFBFBD>ARR<EFBFBD>ϵ<EFBFBD>Ԥװ<EFBFBD>ؼĴ<EFBFBD><EFBFBD><EFBFBD>
TIM_Cmd(TIM1, ENABLE); //ʹ<EFBFBD><EFBFBD>TIM1
}