Skip to main content
andreaspiezia9
Associate III
January 17, 2014
Question

Issue with PWM TIM3 on STM32F3Discovery board

  • January 17, 2014
  • 15 replies
  • 2007 views
Posted on January 17, 2014 at 18:31

Hi all,

I'd like to see a PWM with my oscilloscope, but I see nothing and I can't understand why. Can you help me? Thanks in advantage <I>

int main(void)
{
TIM_Config();
GPIO_Configuration();
/* Infinite loop */
while (1)
{
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOB clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2); // TIM3 Channel1 PA6 AF2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // Or UP for open-collector sources
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/********************************/
static void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
__IO uint16_t CCR1_Val = 40961;
uint16_t PrescalerValue = 0;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* ----------------------------------------------------------------------- */ 
TIM_DeInit(TIM3);
/* Compute the prescaler value we want TIM3 clk @8Mhz => Presc = 8*/
PrescalerValue = (uint16_t) ((SystemCoreClock) / 8000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 100;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Prescaler configuration */
TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
/* Output Compare Timing Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
}

</I>
    This topic has been closed for replies.

    15 replies

    andreaspiezia9
    Associate III
    January 17, 2014
    Posted on January 17, 2014 at 18:32

    BTW do you know how hlm format the code as code?

    chen
    Associate II
    January 17, 2014
    Posted on January 17, 2014 at 18:43

    Hi

    I think you want :

      /* Output Compare Timing Mode configuration: Channel1 */

      TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    or the PWM2

    Tesla DeLorean
    Guru
    January 17, 2014
    Posted on January 17, 2014 at 19:09

    You use the Paintbrush [<>] icon (top left)

    Your problem here is that with a period of 100 you're never going to get the counter to reach 40961

    Set

    TIM_TimeBaseStructure.TIM_Period = 100-1; // Divide by 100

    ..

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_Pulse = 50; // 100/2 is 50/50 duty

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    andreaspiezia9
    Associate III
    January 18, 2014
    Posted on January 18, 2014 at 14:22

    Thank you but it doesn't work both with new period/pulse (100-1/50) and 

    <> TIM_OCMode = TIM_OCMode_PWM1 </>

    Any other idea?

    Tesla DeLorean
    Guru
    January 18, 2014
    Posted on January 18, 2014 at 15:23

    Is PA6 clashing with the L3GD20 (SAO/SDO)?

    Can't you use PA4 (TIM3_CH2)?
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    andreaspiezia9
    Associate III
    January 18, 2014
    Posted on January 18, 2014 at 15:31

    I'm just trying with TIM3_CH2 and let you know soon

    andreaspiezia9
    Associate III
    January 18, 2014
    Posted on January 18, 2014 at 15:42

    Used TIM3_CH2 but it doesn't work, I reported the last code revision since I've lightly modified:

    any ideas?

    #include ''main.h''
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    uint16_t PrescalerValue = 0;
    static void TIM_Config(void);
    void GPIO_Configuration(void);
    int main(void)
    {
    /* TIM Configuration */
    TIM_Config();
    GPIO_Configuration();
    /* Compute the prescaler value */
    PrescalerValue = (uint16_t) ((SystemCoreClock) / 1000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 100-1;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    /* Prescaler configuration */
    TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
    /* Output Compare Timing Mode configuration: Channel2 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 50;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
    /* TIM3 enable counter */
    TIM_Cmd(TIM3, ENABLE);
    /* Infinite loop */
    while (1)
    {
    }
    }
    static void TIM_Config(void)
    {
    /* TIM3 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    }
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    /* GPIOB clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_2); // TIM3 Channel2 PA4 AF2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // Or UP for open-collector sources
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    }

    Tesla DeLorean
    Guru
    January 18, 2014
    Posted on January 18, 2014 at 16:05

    0690X00000605WzQAI.png
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    andreaspiezia9
    Associate III
    January 18, 2014
    Posted on January 18, 2014 at 19:41

    Have you an example code for TIM stm32f3?

    Tesla DeLorean
    Guru
    January 18, 2014
    Posted on January 18, 2014 at 19:51

    Have you an example code for TIM stm32f3?

    I write and post a lot of things, when I need them I Google for them. I could write some additional ones, but don't have a scope with me.

    https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F3%20Timer3%20and%20Timer4%20as%20PWM&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=153

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