|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
/************************************************************************
TIM3 PWM
************************************************************************/
void TIM3_PWMConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM3 IO configuration *************************************/
/* Enable GPIOC clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* Connect TIM3 pin (PC6) to AF2 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3);
/* Configure TIM3 CH1 pin (PC6) as alternate function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 |GPIO_Pin_8 | GPIO_Pin_9;
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(GPIOC, &GPIO_InitStructure);
/* TIM3 configuration *****************************************
TIM3 is configured to generate PWM signal on CH1 with a
frequency of 30 KHz and 50% duty cycle.
TIM3 input clock (TIM3CLK) is equal to:
- PCLK1 if PCLK1 prescaler is 1
- 2 x PCLK1, otherwise
This example assumes that HCLK = 120 MHz and PCLK1 = 30 MHz
=> TIM3CLK = 2 x PCLK1 = 60 MHz
TIM3 signal frequency = TIM6CLK /((Prescaler + 1) * Period)
= 30 KHz
Setting the Prescaler to 0, the Period = TIM6CLK / 30 KHz
= 2000
TIM6 signal duty cycle = (CCR1 / ARR) * 100 = 50%
= (Pulse / Period) * 100 = 50%
=> with a Period of 2000, the Pulse = 1000
**************************************************************/
/* Enable TIM3 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM3->CCR1 = 1000;//设置占空比
TIM3->CCR2 = 1000;
TIM3->CCR3 = 1000;
TIM3->CCR4 = 1000;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 2000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Configure CH1 in PWM1 Mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
/* Enable TIM3 counter */
TIM_Cmd(TIM3, ENABLE);
} |
|