Skip to main content
Visitor II
March 9, 2008
Question

Setting PWM Output

  • March 9, 2008
  • 3 replies
  • 1201 views
Posted on March 09, 2008 at 23:44

Setting PWM Output

    This topic has been closed for replies.

    3 replies

    daryl2Author
    Visitor II
    March 7, 2008
    Posted on March 07, 2008 at 06:54

    Hi,

    I am trying to generate PWM output using the STR750 MCU. Below are my settings. Somehow I am not able to get any PWM signals out. Anyone has any idea why this is so? Please enlighten me. Thank you very much

    // PWM, TIM0, TIM1 and TIM2 clock has been enabled

    PWM_InitTypeDef PWM_InitStructure;

    /* Configure P1.06 as alternate function (PWM_OC2) */

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

    GPIO_Init(GPIO1, &GPIO_InitStructure);

    // PWM configuration

    PWM_InitStructure.PWM_Mode = PWM_Mode_PWM;

    PWM_InitStructure.PWM_Prescaler = 0x00; // TIM_CLK: 60 MHz

    PWM_InitStructure.PWM_CounterMode = PWM_CounterMode_Up;

    PWM_InitStructure.PWM_Period = 0x7530; // PWM frequency : ~2KHz

    PWM_InitStructure.PWM_Channel = PWM_Channel_2;

    PWM_InitStructure.PWM_Pulse2 = 0x3A98; // Duty cycle: 50%

    PWM_InitStructure.PWM_Polarity2 = PWM_Polarity2_Low;

    PWM_InitStructure.PWM_Polarity2N = PWM_Polarity2_Low;

    PWM_InitStructure.PWM_Complementary = PWM_Complementary_Disable;

    PWM_InitStructure.PWM_OCState = PWM_OCState_Disable;

    PWM_InitStructure.PWM_OCNState = PWM_OCNState_Enable;

    PWM_InitStructure.PWM_DeadTime = 0;

    PWM_Init(&PWM_InitStructure);

    PWM_Cmd(ENABLE);

    Visitor II
    March 7, 2008
    Posted on March 07, 2008 at 13:03

    Do you have the PWM peripheral clock enabled?

    MRCC_PeripheralClockConfig(MRCC_Peripheral_PWM, ENABLE);

    This is how I use the PWM channels:

    Code:

    /* PWM is configured to generate a 3Khz pulse with CK_TIM = 60MHz:

    * update frequency = CK_TIM/(Prescaler+1)(Period+1) = 300 Hz

    * The blower motor(s) require a PWM signal with a frequency of 150hz to 400hz.

    * A 100% duty cycle is on full, while a 0% duty cycle is completely off.

    */

    PWM_DeInit(); /* reset PWM */

    /* Configure blowers as PWM* outputs */

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_8;

    GPIO_Init(GPIO1, &GPIO_InitStructure); /* blower motors */

    MRCC_PeripheralClockConfig(MRCC_Peripheral_PWM, ENABLE); /* PWM clock source enable */

    /* PWM Channel 1 configuration: main ball blower */

    PWM_InitStructure.PWM_Mode = PWM_Mode_PWM;

    PWM_InitStructure.PWM_Prescaler = 19999; // prescale by 60Mhz/20000 = 3Khz

    PWM_InitStructure.PWM_CounterMode = PWM_CounterMode_Up;

    PWM_InitStructure.PWM_Period = BLO_OFF - 1; // divide 3Khz /10 = 300Hz

    PWM_InitStructure.PWM_Complementary = PWM_Complementary_Enable; // enable inverted output pins

    PWM_InitStructure.PWM_Channel = PWM_Channel_1;

    PWM_InitStructure.PWM_Pulse1 = BLO_OFF; // startup motor off

    PWM_InitStructure.PWM_DTRAccess = PWM_DTRAccess_Disable;

    PWM_InitStructure.PWM_Polarity1N = PWM_Polarity1N_Low; // PWM1N active lo (invert to FET)

    PWM_InitStructure.PWM_RepetitionCounter = 0x05;

    PWM_Init(&PWM_InitStructure);

    /* PWM Channel 2 configuration: eject ball blower */

    PWM_InitStructure.PWM_Channel = PWM_Channel_2;

    PWM_InitStructure.PWM_Pulse2 = BLO_OFF; // startup motor off

    PWM_InitStructure.PWM_Polarity2N = PWM_Polarity2N_Low; // PWM2N active lo

    PWM_Init(&PWM_InitStructure);

    /* PWM Channel 3 configuration: unused */

    PWM_InitStructure.PWM_Channel = PWM_Channel_3;

    PWM_InitStructure.PWM_Pulse3 = BLO_OFF; // startup motor off

    PWM_InitStructure.PWM_Polarity3N = PWM_Polarity3N_Low; // PWM3N active lo

    // no dead time, no emergency stop

    PWM_InitStructure.PWM_DTRAccess = PWM_DTRAccess_Enable;

    PWM_InitStructure.PWM_Emergency = PWM_Emergency_Disable;

    PWM_InitStructure.PWM_DeadTime = 0x00;

    PWM_InitStructure.PWM_LOCKLevel = PWM_LOCKLevel_3;

    PWM_InitStructure.PWM_OSSIState = PWM_OSSIState_Enable;

    PWM_Init(&PWM_InitStructure);

    /* Enable PWM Main Outputs */

    PWM_CtrlPWMOutputs(ENABLE);

    /* Enable PWM counter */

    PWM_Cmd(ENABLE);

    To set the duty cycle takes one line:

    PWM_SetPulse(PWM_Channel_1, value0to99);

    daryl2Author
    Visitor II
    March 9, 2008
    Posted on March 09, 2008 at 23:44

    thanks jakbird. Managed to solve my problem. My code did not run PWM_CtrlPWMOutputs(ENABLE).