做了一个用STM32F103RBT6芯片控制可控硅加热的实验,用内部时钟CK_INT可以在TIM2_CH2/CH3/CH4上产生三路10ms脉宽PWM。因为外部电源频率有波动所以这几路PWM无法与电源过零信号同步控制可控硅。所以才用外部交流电源10ms周期的过零信号作为定时器的外部时钟接入PA0_ETR管脚,这样PWM周期就与外部交流电周期严格同步了。实际程序运行后TIM2_CH2/CH3/CH4上并没有产生10ms脉宽的PWM,不知道到底问题在那儿,请求哪位大侠帮忙看下。(外部中断是10ms的交流过零信号)
以下是源代码:
#include "sys.h"
#include "delay.h"
vu16 led0pwmval=0;
void GPIOInit() //一共10个PWM端口,初始化GPIOA与GPIOB端口
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA, ENABLE); //开启端口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//初始化定时器TIM1、TIM2、TIM3比较输出PWM管脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;// GPIOA端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0|GPIO_Pin_1;// GPIOB端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);
//初始化外部中断管脚
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_15; // GPIOB_Pin15中断输入端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PB15设置成上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);
//初始化外部时钟管脚
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0; // GPIOA_Pin0外部时钟输入端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //PA0设置成浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM2_CH1_Clock_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定时器2时钟
//GPIOA_Pin0的ETR外部时钟模式2配置
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x0f); //外部时钟配置:TIM2配置成 1)外部触发不预分频。2)外部触发极性上升沿有效。3)外部触发滤波器关闭
//初始化TIM2
TIM_TimeBaseStructure.TIM_Period = 1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 1-1; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复计数。重复溢出多少次来一个溢出中断
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
//初始化TIM2_CH2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC2
//初始化TIM2_CH3 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC3
//初始化TIM2_CH4 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC4
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR2上的预装载寄存器
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR3上的预装载寄存器
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR4上的预装载寄存器
TIM_ARRPreloadConfig(TIM2,ENABLE); //立即生效,使能预装载寄存器
}
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
//GPIOB.6 中断线以及中断初始化配置 上升沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);
EXTI_InitStructure.EXTI_Line=EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
//中断优先级配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能PB6所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
}
//外部GPIOB_Pin_6中断服务程序
void EXTI9_5_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line6); //清除LINE6上的中断标志位
TIM_Cmd(TIM2,ENABLE);
TIM_SetCompare2(TIM2,led0pwmval);
TIM_SetCompare3(TIM2,led0pwmval);
TIM_SetCompare4(TIM2,led0pwmval);
}
int main(void)
{
u8 dir=1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
GPIOInit();
TIM_DeInit(TIM2);
EXTIX_Init();
TIM2_CH1_Clock_Init();
while(1)
{
delay_us(100);
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval>35000)dir=0;
if(led0pwmval==0)dir=1;
}
}
|