星火002 发表于 2017-8-1 15:44:53

STM32F103高级定时器TIM1产生六路PWM控制无刷舵机BLDC

首次发帖、不周之处请指正!
先描述下问题,最近在做120度霍尔BLDC的控制,用STM32F103 TIM1产生六路PWM时出现问题。
想要实现的控制状态是可以控制六个通道中任何一个通道的PWM输出与关断,不用互补、不需要死区
选用引脚:
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398247&size=300x300&key=387f180e7fd2872e&nocache=yes&type=fixnone
TIM1配置如下图
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398244&size=300x300&key=3bd424312d048628&nocache=yes&type=fixnone
主函数:
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398245&size=300x300&key=2fda6a2819b6a88c&nocache=yes&type=fixnone
软件仿真:
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398248&size=300x300&key=cb7a665a4611ea08&nocache=yes&type=fixnone
我想要6路PWM的,但是只产生四路   PB0与PB1只在刚开始有输出
而且,当我把CH1设置为Disable时,CH1N仍然是Enable
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398251&size=300x300&key=8676a6cb9e74abac&nocache=yes&type=fixnone
仿真结果如下,CH1与CH1N不输出、其他四路输出正常
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398252&size=300x300&key=847fcce701b10081&nocache=yes&type=fixnone
由此产生了几个问题
1、CH1的设置 会对PB0(CH2N)PB1(CH3N)产生影响? CH1 Enable,CH2N、CH3N不输出,CH1 Disable CH2N、CH3N输出
2、CH1由Enable变为Disable对CH1N的输出产生了影响,CH1 的失能与使能时CH1与CH1N的总开关?我之后使能CH1、失能CH1N,除CH1N以外的其余五路输出
怀疑是库函数的原因、尝试用寄存器写手册与配置仿真结果如下图
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398255&size=300x300&key=6dcdca50e106ec75&nocache=yes&type=fixnone
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398257&size=300x300&key=c931c4102b5c993a&nocache=yes&type=fixnone
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398258&size=300x300&key=55a625a8d2d491ed&nocache=yes&type=fixnone
想要的输出状态是上图红线标注的   CH通道关闭CHN通道输出,百度过这种输出状态,有人说可以实现但是没讲方法
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398253&size=300x300&key=7989e7478ae4b703&nocache=yes&type=fixnone
https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=398256&size=300x300&key=197e19d76a2d29d5&nocache=yes&type=fixnone
这样配置六路均有输出、但是PB0、PB1不正常而且也不是我想要的输出状态,可能是配置问题,很少用寄存器,不清楚这么对不对,请指正

前边都是截图,补一下TIM1配置部分、main部分代码

int main(void)
{
    delay_init();

星火002 发表于 2017-8-1 15:52:31

void PWM_OUT_Init()
{
    //PA8910 对应CBA相PA7PB0 PB1 对应C-B-A-相复用推挽输出
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef       TIM_OCInitStructure;
//    TIM_BDTRInitTypeDef   TIM_BDTRInitStructure;
   
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA , ENABLE); ///RCC_APB2Periph_AFIO
        GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);//需要部分重映像
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | 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);
   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
   
        TIM_DeInit(TIM1);
       
    /* Time Base configuration */
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = 4500;/// PWM 16K
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

    /* Channel 1, 2,3 and 4 Configuration in PWM mode */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;///TIM_OCMode_PWM2TIM_OCMode_Timing
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 2000;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;///TIM_OCNPolarity_High
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; ///TIM_OCNIdleState_Set
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

        TIM_OC2Init(TIM1, &TIM_OCInitStructure);
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
        TIM_OC3Init(TIM1, &TIM_OCInitStructure);

    /* TIM1 counter enable */
    TIM_Cmd(TIM1, ENABLE);

    /* Main Output Enable */
    TIM_CtrlPWMOutputs(TIM1, ENABLE);

}


int main(void)
{
    delay_init();                 //延时函数初始化          
    NVIC_Configuration();        //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
    GPIO_init();
    USART_init();
//        Hall_Init();
        PWM_OUT_Init();
    GPIO_SetBits(GPIOA,GPIO_Pin_1);//TX_OE

        TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1);
        TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable);
        TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable);

        /*Channel2 configuration */
        TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 );
        TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable);
        TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);

        /*Channel3 configuration */
        TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1);
        TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable);
        TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);       

        //N 通道受主通道影响主DISABLE 则N通道也关闭

//        TIM1->BDTR &=0xfc00;//解锁
//        TIM1->BDTR |= 0x8800;
//        TIM1->CR2 |=0x1f80;
//        TIM1->CCER = 0x00;
//        TIM1->CCER |= 0x555;
}
代码部分没贴上去,补一下

dsjsjf 发表于 2017-8-1 20:45:57

帮顶一个

wenlong0601 发表于 2017-8-1 20:54:41

帮顶啊。

星火002 发表于 2017-8-1 21:56:24

dsjsjf 发表于 2017-8-1 20:45
帮顶一个

谢谢

zero99 发表于 2017-9-29 15:59:10

图片也太小了吧,根本看不清

五哥1 发表于 2017-10-1 08:30:01

楼主解决了吗,坐等结果呀

贺工 发表于 2017-10-14 21:02:36


关于如何输出六路互补带死区PWM波形,请关注“单片机STM32”公众号,里面有一篇文章“STM32定时器TIM1输出六路带死区互补PWM波形(一)”描述。

贺工 发表于 2017-10-14 21:03:30

http://mp.weixin.qq.com/s?__biz=MzU3NTI2OTQ5NQ==&mid=100000051&idx=1&sn=265f1fe9fa26e58b85774851e14991f6&chksm=7d24fe2b4a53773d9b098a314f0152095cea5d79c0e7b39106df6b670a7598fb63d42780ba75#rd

星火002 发表于 2017-10-26 16:10:52

zero99 发表于 2017-9-29 15:59
图片也太小了吧,根本看不清

感兴趣可以下载源文件看哈
页: [1] 2
查看完整版本: STM32F103高级定时器TIM1产生六路PWM控制无刷舵机BLDC