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