打印
[STM32F1]

TIM1 PWM 中心对齐波形出不来?求助呀

[复制链接]
1334|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zjdwyb|  楼主 | 2015-12-3 22:51 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
源代码:
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;                           //基本结构体变量定义
    TIM_OCInitTypeDef  TIM_OCInitStructure;                     //输出结构体变量定义
    TIM_BDTRInitTypeDef  TIM_BDTRInitStructure;                                   //死区刹车结构体变量定义

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

    // PA8 9 10 PB 13 14 15 复用
    {
        GPIO_InitTypeDef GPIO_InitStructure;

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);

        __bic(BIT_16(8)|BIT_16(9)|BIT_16(10), GPIOA->ODR);  //缺省输出

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
        __bis(BIT_16(13)|BIT_16(14)|BIT_16(15), GPIOB->ODR);  //缺省输出
    }

    TIM_DeInit(TIM1);

    TIM_TimeBaseStructure.TIM_Prescaler = 0;                                           //TIM基本初始化
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;//中央对齐计数模式
    TIM_TimeBaseStructure.TIM_Period = 128*2;                                           //PWM 16K
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;                    //TIM输出通道初始化
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;                  
    TIM_OCInitStructure.TIM_Pulse =128;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;         
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;         

    TIM_OC1Init(TIM1,&TIM_OCInitStructure);

    TIM_OCInitStructure.TIM_Pulse =128/2;
    TIM_OC2Init(TIM1,&TIM_OCInitStructure);

    TIM_OCInitStructure.TIM_Pulse =128/2*3;
    TIM_OC3Init(TIM1,&TIM_OCInitStructure);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;                    //TIM输出通道4初始化,用来触发AD注入采样
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;                  
    TIM_OCInitStructure.TIM_Pulse =128;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;           

    TIM_OC4Init(TIM1,&TIM_OCInitStructure);

    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;        //死区刹车初始化
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
    TIM_BDTRInitStructure.TIM_DeadTime = 4;
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;         //如打开,开机无输出且状态紊乱????
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;

    TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);

    TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);  //使能捕获比较寄存器预装载(通道1)

    TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);         //使能捕获比较寄存器预装载(通道2)

    TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);         //使能捕获比较寄存器预装载(通道3)

    TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2);        //输入触发源选择TIM3   

    //TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Trigger);//从模式选择 触发          

    TIM_CtrlPWMOutputs(TIM1,ENABLE);

    TIM_Cmd(TIM1,ENABLE);
}
波形:
C:\Users\user\Documents\PWM_1.png
寄存器当前内容:
C:\Users\user\Documents\TIM1_REG.png
沙发
zjdwyb|  楼主 | 2015-12-3 22:54 | 只看该作者
补充波形和寄存器

TIM1_REG.png (58.17 KB )

TIM1寄存器状态

TIM1寄存器状态

PWM_1.png (17.77 KB )

输出波形PA8,PA9

输出波形PA8,PA9

使用特权

评论回复
板凳
zjdwyb|  楼主 | 2015-12-5 09:14 | 只看该作者
顶一下,高手来帮忙呀

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝