STM32 如何实现1HZ频率 1us脉宽 PWM

2019-08-17 09:49发布

STM32 如何实现1HZ频率 1us脉宽可调   PWM
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
39条回答
wszdxmh
2019-08-19 10:36
[mw_shl_code=c,true]void TIM1_Init(uint16_t period, uint16_t prescaler)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //ê±Öóê1Äü
        //¶¨ê±Æ÷TIM43õê¼»ˉ
        TIM_TimeBaseStructure.TIM_Period = period; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ       
        TIM_TimeBaseStructure.TIM_Prescaler = prescaler; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
        TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM4ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï
        //ÖD¶ÏóÅÏ輶NVICéèÖÃ
        NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;  //TIM1ÖD¶Ï
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;  //ÏèÕ¼óÅÏ輶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //′óóÅÏ輶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
        NVIC_Init(&NVIC_InitStructure);  //3õê¼»ˉNVIC¼Ä′æÆ÷
        TIM_Cmd(TIM1, ENABLE);  //ê1ÄüTIMx                       
}

void TIM1_UP_IRQHandler(void)
{
    static long a;
        if(TIM_GetITStatus(TIM1, TIM_IT_Update))
        {
        a++;
        if(a == 1000000)
            a=0;
        if(a<b)
            led=1;
        else
            led=0;
                TIM_ClearITPendingBit(TIM1, TIM_IT_Update);  //&#199;&#229;3yTIMx&#184;üD&#194;&#214;D&#182;&#207;±ê&#214;&#190;
        }
}

void main(void)
{
    TIM1_Init(49999,1439);
    led_init();
    while(1);
}[/mw_shl_code]
用定时器模拟PWM,1us中断一次;改b值就可以控制占空比。

一周热门 更多>