输出PWM时300HZ,占空比从5%到95%变化,变化时间是500ms

2019-07-21 04:38发布

       我想些一段程序,输出PWM频率为300HZ,占空比从5%到95%连续变化,从5%到95%用时500ms,.应该怎么去实现呢。我这边些了一段代码不能输出300HZ的波形,请问那点有问题吗

static u16 led0pwmval=0;
static u16 dir=0;               

void TIM3_init_Init()
{  
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);        //ê1Äü¶¨ê±Æ÷3ê±Öó
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | RCC_APB2Periph_AFIO, ENABLE);  //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
       
        GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer32¿·ÖÖØó3éä  TIM3_CH2->B5   

   //éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM3 CH2μ&#196WMÂö3å2¨DΠ       GPIOB.5
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸′óÃíÆíìêä3ö
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIO

   //3õê¼»ˉTIM3
        TIM_TimeBaseStructure.TIM_Period = 76; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
        TIM_TimeBaseStructure.TIM_Prescaler =70; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
       
       

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3Ö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_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM3ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï

        TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //ê1ÄüTIM3ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷

        TIM_Cmd(TIM3, ENABLE);  //ê1ÄüTIM3
       
}
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
         dir++;
         printf(" dir %d ",dir);
         TIM_SetCompare2(TIM3,360+dir);
         led0pwmval=TIM_GetCapture2(TIM3);
         printf(" led0pwmval %d ",led0pwmval);
         if(dir >6480) dir=0;
}

}

//TIM3 PWM2¿·Ö3õê¼»ˉ
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM3_PWM_adj_Init()
{  
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
//        NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);        //ê1Äü¶¨ê±Æ÷3ê±Öó
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | RCC_APB2Periph_AFIO, ENABLE);  //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
       
        GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer32¿·ÖÖØó3éä  TIM3_CH2->B5   

   //éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM3 CH2μ&#196WMÂö3å2¨DΠ       GPIOB.5
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸′óÃíÆíìêä3ö
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIO

   //3õê¼»ˉTIM3
        TIM_TimeBaseStructure.TIM_Period = 7199; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
        TIM_TimeBaseStructure.TIM_Prescaler =32; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
       
        //3õê¼»ˉTIM3 Channel2 PWMģ꽠        
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
        TIM_OCInitStructure.TIM_Pulse= 4000 ;
        TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM3 OC2

        TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //ê1ÄüTIM3ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷

        TIM_Cmd(TIM3, ENABLE);  //ê1ÄüTIM3
       
}



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
peng1554
1楼-- · 2019-07-21 10:04
qianoocheng 发表于 2018-10-26 18:47
用的是战舰的开发板做的,我主要想实现占空比在500ms内变化,如果只是修改占空比例程就可以实现的

这样写 PWM频率300.03HZ ,时间就没测试了,大概500ms左右
2.jpg

qianoocheng
2楼-- · 2019-07-21 14:02
static u16 led0pwmval=0;
static u16 dir=0;               

void TIM3_init_Init()
{  
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);        //ê1Äü¶¨ê±Æ÷3ê±Öó
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | RCC_APB2Periph_AFIO, ENABLE);  //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
       
        GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer32¿·ÖÖØó3éä  TIM3_CH2->PB5   

  

   //3õê¼»ˉTIM3
        TIM_TimeBaseStructure.TIM_Period = 76; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
        TIM_TimeBaseStructure.TIM_Prescaler =70; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
       
       

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3Ö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_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM3ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï

        TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //ê1ÄüTIM3ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷

        TIM_Cmd(TIM3, ENABLE);  //ê1ÄüTIM3
       
}
xuyaqi
3楼-- · 2019-07-21 14:21
你是没有输出,还是频率不对?
peng1554
4楼-- · 2019-07-21 19:57
 精彩回答 2  元偷偷看……
qianoocheng
5楼-- · 2019-07-22 01:22
xuyaqi 发表于 2018-10-26 17:40
你是没有输出,还是频率不对?

没有波形输出,是不是因为定时器中断一次的时间和77us 更改一次pulse的值,CNT的值还没有计数到PLUSE的值,下一次pulse的值有改变了啊
qianoocheng
6楼-- · 2019-07-22 05:19
peng1554 发表于 2018-10-26 18:41
300HZ输出并且变化占空比还是修改下院子PWM例程就可以了 “从5%到95%用时500ms”要推敲下
对了,你用的32 ...

用的是战舰的开发板做的,我主要想实现占空比在500ms内变化,如果只是修改占空比例程就可以实现的

一周热门 更多>