cancel
Showing results for 
Search instead for 
Did you mean: 

PWM OP MODE for STM32F103VBT6

balu
Associate
Posted on November 27, 2014 at 13:51

Hi,

           i am trying to generate an Signal with 50 % duty cycle using PWM Mode 1 Timer 1 channel 1.Pls go through the code below.I can't generate any pulse at PE9

PE9 is used as timer 1 channel 1 PWM OP. Using HSE 8Mhz with PLL=HSE*9; SYSCLK=72Mhz.

void Configure_Controller(void)

{

    Reset_Clock_Control();

    GPIO_Configuration();

    Configure_Timer_1();

    

}

void Reset_Clock_Control(void)

{

    ErrorStatus HSEStartUpStatus; //To handle Error Status of HSE

    RCC_ClocksTypeDef RCC_Clocks_Attributes;

    RCC_DeInit();      // Reset the RCC clock configuration to the default reset state HSI ON,HSE & PLL OFF

    RCC_HSEConfig(RCC_HSE_ON); //Enable external clock 8Mhz

    HSEStartUpStatus = RCC_WaitForHSEStartUp();//Wait till HSE is ready and if Time out is reached exit

    if(HSEStartUpStatus==SUCCESS)          //if HSE is stable

    {

        Exception_Object.HSE_Clock_Exception=0;

        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//Enable Prefetch Buffer

        FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state

        RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);// (HSE Clock*9)/1;

        RCC_PLLCmd(ENABLE); //Enable PLL with 72MHz

        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);//Wait untill PLL is ready

        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select the PLL as system clock Source

        while(RCC_GetSYSCLKSource() != 0x08); //Wait untill PLL is used as system clock

        RCC_HCLKConfig(RCC_SYSCLK_Div1); //use HCLK/AHB =system clock/1; 72Mhz

        RCC_PCLK1Config(RCC_HCLK_Div2); //APB1 HCLk/2=36Mhz

        RCC_PCLK2Config(RCC_HCLK_Div1);    //APB2 HCLk/1=72Mhz

        RCC_LSEConfig(RCC_LSE_OFF); //Disable LSE

        RCC_LSICmd(DISABLE); //Disable LSI

        RCC_GetClocksFreq(&RCC_Clocks_Attributes);          //Get Present Clocks

        if((RCC_Clocks_Attributes.SYSCLK_Frequency==72000000)&&(RCC_Clocks_Attributes.HCLK_Frequency==72000000)&&(RCC_Clocks_Attributes.PCLK1_Frequency==36000000)&&(RCC_Clocks_Attributes.PCLK2_Frequency==72000000))

        Exception_Object.Clocks_Exception=0;

        else

        Exception_Object.Clocks_Exception=1;                

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_TIM1 | RCC_APB2Periph_AFIO, ENABLE); //Port E & Timer 1 & AFIO

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);

    }

    else

    {

        Exception_Object.HSE_Clock_Exception=1;

        RCC_HSICmd(ENABLE);  //Enable

    }

}

/*************************************************************************************************************************************************************

****** Routine name:GPIO_Configuration

****** Formal Argument:None

****** Retur Type:None

****** Used to Intializse the Port Pins

        * PE^9: 10HZ Timer 1 Channel 1 with 50 % duty cycle

**************************************************************************************************************************************************************/

void GPIO_Configuration(void)

{

    GPIO_InitTypeDef GPIO_Configuration_Object;

    GPIO_DeInit(GPIOE);    //Reset the GPIO Port E to default value

    GPIO_Configuration_Object.GPIO_Pin=GPIO_Pin_9;  //Used for Generating 10 HZ signal through PWM_TIM1_CH1

    GPIO_Configuration_Object.GPIO_Speed=GPIO_Speed_2MHz;

    GPIO_Configuration_Object.GPIO_Mode=GPIO_Mode_Out_PP;

    GPIO_Init(GPIOE, &GPIO_Configuration_Object); //Initialise the PORt with required setting

    GPIO_PinRemapConfig(GPIO_Remap_TIM1, ENABLE); // Configure peripheral I/O remapping

}

/*************************************************************************************************************************************************************

****** Routine name:Configure_Timer_1

****** Formal Argument:None

****** Retur Type:None

****** Used to enable timer 1 channel 1 with 10 hz 50 % duty cycle

**************************************************************************************************************************************************************/

void Configure_Timer_1(void)

{

    TIM_OCInitTypeDef TIM_OCInitStructure={0};

    TIM_TimeBaseInitTypeDef Timer_Struct_Object={0};

    TIM_DeInit(TIM1);      //Reset the Timer 1

    

    Timer_Struct_Object.TIM_Period=50000;

    Timer_Struct_Object.TIM_Prescaler=144;

       Timer_Struct_Object.TIM_ClockDivision=TIM_CKD_DIV1;

    Timer_Struct_Object.TIM_CounterMode=TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM4, &Timer_Struct_Object);

    TIM_Cmd(TIM1, ENABLE);

    TIM_SelectOCxM(TIM4, TIM_Channel_1, TIM_OCMode_PWM1);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = 250;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC1Init(TIM1, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

    

    TIM_CtrlPWMOutputs(TIM1, ENABLE);

    

    

    

}

1 REPLY 1
Posted on November 27, 2014 at 15:08

Bunch of TIM4 references still in there. TIM1 you'd also want to fully qualify all the OC structure fields, including the inverting output fields.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..