void Pwm_Init(unsigned long Frequency, float DC1, float DC2, float DC3, float DC4) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; uint16_t TimerPeriod = 0; uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0; /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */ TimerPeriod = (SystemCoreClock / (Frequency << 1)) - 1; /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */ Channel1Pulse = (uint16_t) (((uint32_t) DC1 * (TimerPeriod - 1)) / 100); /* Compute CCR2 value to generate a duty cycle at 37.5% for channel 2 and 2N */ Channel2Pulse = (uint16_t) (((uint32_t) DC2 * (TimerPeriod - 1)) / 100); /* Compute CCR3 value to generate a duty cycle at 25% for channel 3 and 3N */ Channel3Pulse = (uint16_t) (((uint32_t) DC3 * (TimerPeriod - 1)) / 100); /* Compute CCR4 value to generate a duty cycle at 12.5% for channel 4 */ Channel4Pulse = (uint16_t) (((uint32_t) DC4 * (TimerPeriod - 1)) / 100); /* TIM1 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = TimerPeriod; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* Channel 1, 2,3 and 4 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; TIM_OC2Init(TIM4, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel3Pulse; TIM_OC3Init(TIM4, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel4Pulse; TIM_OC4Init(TIM4, &TIM_OCInitStructure); /* TIM1 counter enable */ TIM_Cmd(TIM4, ENABLE); /* TIM1 Main Output Enable */ TIM_CtrlPWMOutputs(TIM4, ENABLE); } void Pwm_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA, GPIOB and GPIOE Clocks enable */ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE); /* GPIOA Configuration: Channel 1, 3 and 4 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4); GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4); GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4); GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4); }
作者:MetalSeed 发表于2013-9-7 1:25:59 原文链接
阅读:143 评论:0 查看评论