#include "pwm_output.h"



u16 CCR1_Val = 32768;

u16 CCR2_Val = 16384;

u16 CCR3_Val = 8192;

u16 CCR4_Val = 4096;


u16 CCR1_dc = 50;

u16 CCR2_dc = 50;

u16 CCR3_dc = 50;

u16 CCR4_dc = 50;



u32 capture = 0;

u8 flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0;

u16 setcap = 0;


u8 i = 0;


void TIM2_PWM_Init(float pwm1, u16 dutyradio1,float pwm2,u16 dutyradio2,float pwm3,u16 dutyradio3,float pwm4,u16 dutyradio4)

{

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure= {0};

    NVIC_InitTypeDef NVIC_InitStructure= {0};

    TIM_OCInitTypeDef TIM_OCInitStructure= {0};

    GPIO_InitTypeDef GPIO_InitStructure = {0};

    

    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init( GPIOA, &GPIO_InitStructure );


    TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF;//65535;

    TIM_TimeBaseInitStructure.TIM_Prescaler = 48-1;//2-1;

    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStructure );


    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;


    CCR1_Val = 0x8000;

    CCR2_Val = 0x4000;

    CCR3_Val = 0x2000;

    CCR4_Val = 0x1000;


    CCR1_dc = dutyradio1+10;

    CCR2_dc = dutyradio2+20;

    CCR3_dc = dutyradio3-10;

    CCR4_dc = dutyradio4-20;


    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;

    TIM_OC1Init( TIM2, &TIM_OCInitStructure );//TIM2_OC1   PA0


    TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

    TIM_OC2Init( TIM2, &TIM_OCInitStructure );//TIM2_OC2   PA1


    TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

    TIM_OC3Init( TIM2, &TIM_OCInitStructure );//TIM2_OC1   PA2


    TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

    TIM_OC4Init( TIM2, &TIM_OCInitStructure );//TIM2_OC1   PA3


    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init( &NVIC_InitStructure );

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);


    TIM_CtrlPWMOutputs(TIM2, ENABLE );


    TIM_OC1PreloadConfig( TIM2, TIM_OCPreload_Disable );

    TIM_OC2PreloadConfig( TIM2, TIM_OCPreload_Disable );

    TIM_OC3PreloadConfig( TIM2, TIM_OCPreload_Disable );

    TIM_OC4PreloadConfig( TIM2, TIM_OCPreload_Disable );


    TIM_ARRPreloadConfig( TIM2, ENABLE );

    TIM_ITConfig( TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE );

    TIM_Cmd( TIM2, ENABLE );


}


void TIM2_IRQHandler( void )

{

    printf("irq\r\n");


    if( TIM_GetITStatus( TIM2, TIM_IT_CC1 ) != RESET )

    {

        TIM_ClearITPendingBit( TIM2, TIM_IT_CC1 );

        capture = TIM_GetCapture1( TIM2 );


        if( flag1 == 0 )

        {

            flag1 = 1;

            setcap = capture + ( u32 )CCR1_Val * CCR1_dc / 100;

        }

        else

        {

            flag1 = 0;

            setcap = capture + ( u32 )CCR1_Val  * ( 100 - CCR1_dc ) / 100;

        }

        TIM_SetCompare1( TIM2, setcap  );

    }


    if( TIM_GetITStatus( TIM2, TIM_IT_CC2 ) != RESET )

    {

        TIM_ClearITPendingBit( TIM2, TIM_IT_CC2 );

        capture = TIM_GetCapture2( TIM2 );

        if( flag2 == 0 )

        {

            flag2 = 1;

            setcap = capture + CCR2_Val * CCR2_dc / 100;

        }

        else

        {

            flag2 = 0;

            setcap = capture + CCR2_Val  * ( 100 - CCR2_dc ) / 100;

        }

        TIM_SetCompare2( TIM2, setcap );

    }



    if( TIM_GetITStatus( TIM2, TIM_IT_CC3 ) != RESET )

    {

        TIM_ClearITPendingBit( TIM2, TIM_IT_CC3 );

        capture = TIM_GetCapture3( TIM2 );


        if( flag3 == 0 )

        {

            flag3 = 1;

            setcap = capture + CCR3_Val * CCR3_dc / 100;

        }

        else

        {

            flag3 = 0;

            setcap = capture + CCR3_Val  * ( 100 - CCR3_dc ) / 100;

        }

        TIM_SetCompare3( TIM2, setcap );

    }


    if( TIM_GetITStatus( TIM2, TIM_IT_CC4 ) != RESET )

    {

        TIM_ClearITPendingBit( TIM2, TIM_IT_CC4 );

        capture = TIM_GetCapture4( TIM2 );


        if( flag4 == 0 )

        {

            flag4 = 1;

            setcap = capture + CCR4_Val * CCR4_dc / 100;

        }

        else

        {

            flag4 = 0;

            setcap = capture + CCR4_Val  * ( 100 - CCR4_dc ) / 100;

        }

        TIM_SetCompare4( TIM2, setcap );

    }


}