我用输出比较模式产生PWM信号,相应端口有输出PWM信号,我是查询到CC寄存器匹配标志再来给CC寄存器赋值,但一直无法查询到匹配标志,所以无法动态改变PWM频率,但可以查询到中断标志,可以在中断里重新给CC赋值。
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//
TIM_OCInitTypeDef TIM_OCInitStructure; //
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //TIM2\TIM4时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
//PE 推挽输出端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOE
TIM_TimeBaseStructure.TIM_Period = 65535; //自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //先不装入分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // TIM2
TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate); //预分频值即时装入TIM2
//定时器2的CC1配置输出比较模式
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Toggle; //输出比较触发模式
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //比较寄存器值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);//禁止TIM_CCR1寄存器预装
//配置TIM2中断
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级1级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_Cmd(TIM2, ENABLE); //开启定时器2
|