本帖最后由 1769731564 于 2019-4-2 20:20 编辑
[img]本来是想通过TIM1生成两路互补的SPWM波的
结果有点问题
请各位指导指导程序如下:
#include"time1.h"
int f=0;
u16 CCR1=200;
u16 CCR2=200;
static u16 SPWM_Date[100]=
{203,215,228,240,252,264,276,287,299,309,
320,329,339,347,356,363,370,376,382,387,
391,394,396,398,399,399,399,397,395,392,
389,384,379,373,367,359,352,343,334,325,
314,304,293,282,270,258,246,234,221,209,
196,184,171,159,147,135,123,112,100,90,
79,70,60,52,43,36,29,23,17,12,8,5,3,1,0,
0,0,2,4,7,10,15,20,26,32,40,47,56,65,74,
85,95,106,117,129,141,153,165,178,190};
/*配置TIM1输出的PWM信号的模式,如周期、极性、占空比 */
void TIM1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //使能GPIO和服用功能时钟
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //引脚重映射要开这个时钟
//初始化GPI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //PWM输出在PA8,9,10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //PWM输出在PA7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化GPIO
//初始化定时器 TIM1
TIM_TimeBaseStructure.TIM_Period = 400-1; //7.2M/400=18k //设置在自动重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler =0; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数模式
// TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复寄存器,用于自动更新pwm占空比
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//pwm输出配置,TIM_OCInitStructure共七项配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //设置为pwm2输出模式
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
//TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //设置输出极性//输出极性和互补极性的有效电平为低
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出//比较输出使能
//下面几个参数(除了pulse)是高级定时器才会用到,通用定时器不用配置
// TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High; //设置互补端输出极性//输出极性和互补极性的有效电平为低
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable; //使能互补端输出
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //死区后输出状态
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset; //死区后互补端输出状态
//设置通道1捕获比较寄存器的脉冲值-占空比为50%
// TIM_OCInitStructure.TIM_Pulse=CCR1;
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //设置通道1
//设置通道2捕获比较寄存器的脉冲值-占空比为25%
// TIM_OCInitStructure.TIM_Pulse=CCR2;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
////设置通道3捕获比较寄存器的脉冲值-占空比为12.5%
// TIM_OCInitStructure.TIM_Pulse=CCR3_Val;
// TIM_OC3Init(TIM1,&TIM_OCInitStructure);
//死区和刹车功能配置,高级定时器才有的,通用定时器不用配置TIM_BDTRInitStructure,共七项配置
TIM_BDTRInitStructure.TIM_OSSRState=TIM_OSSRState_Disable; //运行模式下输出选择//运行模式下“关闭状态”使能
TIM_BDTRInitStructure.TIM_OSSIState=TIM_OSSIState_Disable; //空闲模式下输出选择//关闭模式下“关闭状态”使能
TIM_BDTRInitStructure.TIM_LOCKLevel=TIM_LOCKLevel_OFF; //锁定设置 //锁定关闭
TIM_BDTRInitStructure.TIM_DeadTime=0x01; //死区时间设置
TIM_BDTRInitStructure.TIM_Break=TIM_Break_Disable; //刹车功能使能
TIM_BDTRInitStructure.TIM_BreakPolarity=TIM_BreakPolarity_High; //刹车输入极性 //刹车输入高电平有效
TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable; //自动输出使能
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
//使能端的打开
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能TIMx在CCR1上的预装载寄存器?
TIM_ARRPreloadConfig(TIM1,ENABLE); //使能TIMx在ARR上的预装载寄存器?
TIM_Cmd(TIM1,ENABLE); //打开TIM2?
//下面这句是高级定时器才有的,输出pwm必须打开?
TIM_CtrlPWMOutputs(TIM1,ENABLE); //pwm输出使能,一定要记得打开
}
void TIM2_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
//定时器TIM3初始化
// TIM_TimeBaseStructure.TIM_Period =1000-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
// TIM_TimeBaseStructure.TIM_Prescaler =36-1; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_Period =9000; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =655; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
{
TIM1->CCR1=SPWM_Date[f];
TIM1->CCR2=SPWM_Date[f];
f++;
}
if(f>=100)
{
f=0;
}
}
}
|
|