STM32 output comparison mode outputs pulses with fixed frequency

Publisher:YaqiLatest update time:2016-10-13 Source: elecfansKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//The following is the TIM configuration program

 

void TIM1_PWM_INIT(void) //TIM1 configures PWM output parameters
{
    u32 temp_fre,temp_duty;
    TempC = 60; //Set the initial frequency value
    TempD = 4; //Set the initial duty cycle 1 / 2
    temp_fre = (1000000 / TempC) - 1;
    temp_duty = ((temp_fre + 1) / TempD);
   
    TIM_TimeBaseStructure.TIM_Prescaler = 72;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = temp_fre;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;  
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
   
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse
    = temp_duty; TIM_OCInitStructure.TIM_OCPolarity
    = TIM_OCPolarity_Low; InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High
    ; TIM_OCInitStructure.TIM_OCIdleState
    = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;   
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
   
    /* TIM1 counter enable */
    TIM_ARRPreloadConfig(TIM1, ENABLE);
    TIM_Cmd(TIM1, ENABLE);
   
    /* TIM1 Main Output Enable */
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
}


void TIM2_PWM_INIT(void)       
{
    TIM_TimeBaseStructure.TIM_Period = 65535;
    TIM_TimeBaseStructure.TIM_Prescaler = 71;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   
    TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
   
    /* Output Compare Toggle Mode configuration: Channel1 */
    TIM_OCInitStructure. TIM_OCMode = TIM_OCMode_Toggle;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = T1_H;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OC1Init(TIM2, &TIM_OCInitStructure);
   
    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
   
    /* Output Compare Toggle Mode configuration: Channel2 */
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = T2_H;
   
    TIM_OC2Init(TIM M2, &TIM_OCInitStructure);
   
    TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable) ;
   
    /* Output Compare Toggle Mode configuration: Channel3 */
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = T3_H;
   
    TIM_OC3Init(TIM2, &TIM_OCInitStructure);
   
    TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
   
    /* Output Compare Toggle Mode configuration: Channel4 */
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = T4_H;
   
    TIM_OC4Init(TIM2, &TIM_OCInitStructure);
   
    TIM_OC4PreloadConf ig(TIM2, TIM_OCPreload_Disable);
   
    /* TIM enable counter * /
    TIM_Cmd(TIM2, ENABLE);
   
    /* TIM IT enable */
    TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
}

void TIM3_PWM_IN_INIT(void)
{
    TIM_TimeBaseStructure.TIM_Period = 65535;
    TIM_TimeBaseStructure.TIM_Prescaler = 71;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3 , &TIM_TimeBaseStructure);
   
    TIM_ICInitTypeDef TIM_ICInitStructure;  
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0;
  
    TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
    /* Select the TIM2 Input Trigger: TI2FP2 */
    TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
   
    /* Select the slave Mode: Reset Mode */
    TIM_SelectSlave Mode(TIM3, TIM_SlaveMode_Reset) ;
   
    /* Enable the Master/Slave Mode */
    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
   
    /* TIM enable counter */
    TIM_Cmd(TIM3, ENABLE);
   
    /* Enable the CC2 Interrupt Request */
    TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}

 


 

// The following is the interrupt handler

void TIM2_IRQHandler(void)
{
    /* TIM2_CH1 toggling with frequency = 183.1 Hz */
    static u8 i1,i2,i3,i4;
    if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
    {
        i1++;
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );
        capture = TIM_GetCapture1(TIM2);
        if(i1 % 2 == 1)
        {
            TIM_SetCompare1(TIM2, capture + T1_L);           
        }
        else
        {
            TIM_SetCompare1(TIM2, capture + T1_H);           
        }
    }

    /* TIM2_CH2 toggling with frequency = 366.2 Hz */
    if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
    {
        i2++;
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
        capture = TIM_GetCapture2(TIM2);
        if(i2 % 2 == 1)
        {
            TIM_SetCompare2(TIM2, capture + T2_L);           
        }
        else
        {
            TIM_SetCompare2(TIM2, capture + T2_H);           
        }
    }

    /* TIM2_CH3 toggling with frequency = 732.4 Hz */
    if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
    {
        i3++;
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
        capture = TIM_GetCapture3(TIM2);
        if(i3 % 2 == 1)
        {
            TIM_SetCompare3(TIM2, capture + T3_L);
        }
        else
        {
            TIM_SetCompare3(TIM2, capture + T3_H);
        }
    }

    /* TIM2_CH4 toggling with frequency = 1464.8 Hz */
    if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
    {
        i4++;
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
        capture = TIM_GetCapture4(TIM2);
        if(i4 % 2 == 1)
        {
            TIM_SetCompare4(TIM2, capture + T4_L);
        }
        else
        {
            TIM_SetCompare4(TIM2, capture + T4_H);
        }
    }
}

/****************************************************** ******************************
* Function Name: TIM3_IRQHandler
* Description: This function handles TIM3 global interrupt request.
* Input: None
* Output: None
* Return: None
****************************************** *************************************/
void TIM3_IRQHandler(void)
{
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2) ;
    IC2Value = TIM_GetCapture2(TIM3);
   
    if (IC2Value != 0)
    {
        DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value; //Duty cycle
       
        TempB = 1000000 / IC2Value; //Input PWM frequency
        TempA = IC2Value / 1000 ; //Input PWM pulse width
    }
    else
    {
        DutyCycle = 0;
        TempB = 0;
        TempA = 0;
    }   
}

Keywords:STM32 Reference address:STM32 output comparison mode outputs pulses with fixed frequency

Previous article:STM32 output comparison error and solution
Next article:STM32 online upgrade QiFreeUSBS

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号