最详细的SPWM产生方法

2019-07-20 22:38发布

        淘来的绝对详细的SPWM产生方法,分享给大家。哪位大哥发出了波形上传看看,小弟对于这个文档的一些细节配置不太清楚。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
16条回答
正点原子
1楼-- · 2019-07-21 00:33
谢谢分享,不过建议发分享贴。
tao96
2楼-- · 2019-07-21 05:44
Mark。。
幻影觉123
3楼-- · 2019-07-21 08:45
原子哥,我按照文档里的内容在高级定时器内设置通道1,通道2。利用通道2每次向下计数溢出时将正弦表数据赋给通道1比较寄存器,可是波形出不来啊,感觉向下溢出这方面配置有点错误。
#include "pwm.h"
#include "stm32f10x_tim.h"
extern uint16_t NUM;
extern uint16_t SPWM[];
extern uint16_t i;
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy

void TIM1_PWM_Init(u16 arr,u16 psc)
{  
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
        //GPIOê±Öóê1Äü
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);  //ê1ÄüGPIOíaéèê±Öóê1Äü
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
       
        //éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM1 CH1,CH2μ&#196WMÂö3å2¨DÎ
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //TIM1-CH1/CH2
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸′óÃíÆíìêä3ö
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        //éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM1 CH1,CH2μ&#196WMÂö3å2¨DÎ
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //TIM1-CH1/CH2μÄêä3ö»¥21í¨μà¡£
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸′óÃíÆíìêä3ö
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);   
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
        NVIC_InitStructure.NVIC_IRQChannel =TIM1_CC_IRQn;  //TIM1ÖD¶Ï
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //ÏèÕ¼óÅÏ輶0¼¶
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //′óóÅÏ輶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
        NVIC_Init(&NVIC_InitStructure);  //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
       
        TIM_DeInit(TIM1);
        //TIMx¶¨ê±Æ÷3õê¼»ˉ
        TIM_TimeBaseStructure.TIM_Period = arr; //èçéèÖÃarr=999£¬μ±¶¨ê±Æ÷′ó0¼Æêyμ½999£¬ò»121000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú¡£
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃÔ¤·ÖÆμÖμ£¬Ö¸μÄêÇ′óAPB2×üÏßÆμÂêμ½Ïàó|μĶ¨ê±Æ÷TIMxμÄ·ÖÆμÏμêy¡£
        TIM_TimeBaseStructure.TIM_ClockDivision =0; //éèÖÃê±Öó·ÖÆμÏμêy¡£Ö¸μÄêÇTIMx¶¨ê±Æ÷ê±Öóμ½Ïàó|μÄTIMx_CNT¶¨ê±Æ÷¼ÆêyÆ÷μÄ·ÖÆμÏμêy¡£
        TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//¸üDÂÕ¼¿Õ±è
        TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_CenterAligned1;  //TIMÖDÑë¶ÔÆëÄ£ê½1
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
        TIM_ClearFlag(TIM1,TIM_FLAG_CC2);
  TIM_ITConfig(TIM1,TIM_IT_CC2,ENABLE);

       
         
        /*--------------------------------------------------------------------------------------------------------------------------------*/
  //TIM1--CH1êä3öí¨μàPWMÄ£ê½ÅäÖã¨í¨óÃêä3öí¨μàPWMÄ£ê½ÅäÖã©
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
       
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è½Ïêä3öê1Äü
        TIM_OCInitStructure.TIM_Pulse =0; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖ죬μçƽ·¢éúìø±ä
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷ÖμD¡óúéÏÃæËùéèÖÃμÄTIM_PulseÖμê±£&#172WMêä3ö¸ßμçƽ(衾öóúTIMμļÆêyÄ£ê½)
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
        TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
        TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset;
        //ê1ÄüTIMxêä3öí¨μà1 PWMģ꽣¨±ØDëê1ÄüÏàó|μÄÖØ×°ÔؼÄ′æÆ÷TIMx_CCRx£©

        TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1ԤװÔØê1Äü         
        //TIM1->DIER=0X01<<1;
       
  //TIM1--CH2ê&#228;3&#246;í¨μàPWM&#196;£ê&#189;&#197;&#228;&#214;&#195;£¨í¨ó&#195;ê&#228;3&#246;í¨μàPWM&#196;£ê&#189;&#197;&#228;&#214;&#195;£&#169;
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //&#209;&#161;&#212;&#241;&#182;¨ê±&#198;÷&#196;£ê&#189;:TIM&#194;&#246;3&#229;&#191;í&#182;èμ÷&#214;&#198;&#196;£ê&#189;2
       
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è&#189;&#207;ê&#228;3&#246;ê1&#196;ü
        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //±è&#189;&#207;ê&#228;3&#246;ê1&#196;ü
        TIM_OCInitStructure.TIM_Pulse =0; //éè&#214;&#195;ì&#248;±&#228;&#214;μ£&#172;μ±&#188;&#198;êy&#198;÷&#188;&#198;êyμ&#189;&#213;a&#184;&#246;&#214;μ£&#172;μ&#231;&#198;&#189;·¢éúì&#248;±&#228;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //μ±&#182;¨ê±&#198;÷&#214;μD&#161;óúé&#207;&#195;&#230;&#203;ùéè&#214;&#195;μ&#196;TIM_Pulse&#214;μê±£&#172WMê&#228;3&#246;&#184;&#223;μ&#231;&#198;&#189;(è&#161;&#190;&#246;óúTIMμ&#196;&#188;&#198;êy&#196;£ê&#189;)
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
        TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
        TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset;
        //ê1&#196;üTIMxê&#228;3&#246;í¨μà1 PWM&#196;£ê&#189;£¨±&#216;D&#235;ê1&#196;ü&#207;àó|μ&#196;&#214;&#216;×°&#212;&#216;&#188;&#196;′&#230;&#198;÷TIMx_CCRx£&#169;

        TIM_OC2Init(TIM1, &TIM_OCInitStructure);  //&#184;ù&#190;YTIM_OCInitStruct&#214;D&#214;&#184;&#182;¨μ&#196;2&#206;êy3&#245;ê&#188;&#187;ˉíaéèTIMx
  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH2&#212;¤×°&#212;&#216;ê1&#196;ü       
        //TIM1->DIER=0X01<<2;
       
        /*---------------------------------------------------------------------------------------------------------------------------------*/
       
        TIM_CtrlPWMOutputs(TIM1, ENABLE);   
        TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1&#196;üTIMx&#212;úARRé&#207;μ&#196;&#212;¤×°&#212;&#216;&#188;&#196;′&#230;&#198;÷
        TIM_Cmd(TIM1, ENABLE);  //ê1&#196;üTIM1  
}


void TIM1_CC_IRQHandler(void)   //TIM3&#214;D&#182;&#207;
{
       
        if (TIM_GetITStatus(TIM1,TIM_IT_CC2) != RESET) //&#188;ì2é&#214;&#184;&#182;¨μ&#196;TIM&#214;D&#182;&#207;·¢éúó&#235;·&#241;:TIM &#214;D&#182;&#207;&#212;′
                {
                          
                                TIM1->CCR1=SPWM[i++];
                                  if(i>=NUM)
                        {
                                 i=0;
                         }
   
                            
                }
                TIM_ClearITPendingBit(TIM1,TIM_IT_CC2); //&#199;&#229;3yTIMxμ&#196;&#214;D&#182;&#207;′y′|àí&#206;&#187;:TIM &#214;D&#182;&#207;&#212;′
               
}
幻影觉123
4楼-- · 2019-07-21 10:04
 精彩回答 2  元偷偷看……
张册
5楼-- · 2019-07-21 15:00
我最近也在写spwm有机会可以交流学习我微信ZC138285
陈柒
6楼-- · 2019-07-21 16:47
谢谢楼主分享

一周热门 更多>