STM32F PWM的生成
扫描二维码
随时随地手机看文章
void TIME3_Config(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
//第一步:配置时钟
// GPIOC Configuration: Channel 6 as alternate function push-pull
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//第二步,配置goio口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM2时钟使能
TIM_TimeBaseStructure.TIM_Period =1000UL-1;// 自动重装载寄存器周期的值 ;
TIM_TimeBaseStructure.TIM_Prescaler = 30UL-1;// 设置了用来作为TIM2时钟频率除数的预分频值。20S
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 设置了时钟分割。
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;// TIM向上计数模式 ;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//第四步pwm输出配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//设置为pwm2输出模式
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//正向通道有效
TIM_OCInitStructure.TIM_Pulse=500;//设置占空比时间
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//设置输出极性
TIM_OC1Init(TIM3,&TIM_OCInitStructure);//按照指定参数初始化
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//第五步,使能端的打开
//TIM8计数器使能
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);//打开TIM8
}