cancel
Showing results for 
Search instead for 
Did you mean: 

Generating PWM Waveform with STM32L011?

Yasmin Bragg
Associate
Posted on May 10, 2018 at 12:24

Hi all,

I'm new to using timers and I'm currently trying to generate a PWM Waveform so that I can use this to power some motors. At the moment I am testing the PWM Waveform via LED's rather than going straight to the motors. I'm using the STM32L011K4T6 development board to try and do this.

By looking at AN4013 (STM32 Cross-Series Timer Overview) Section 2.5 'Timer PWM Mode' I have come up with what I thought should get the PWM waveform generated, I've attached files with the code in as well as the document I was using. From this I thought I would be able to see a noticeable difference between two LED's by setting the CCRx values to different amounts. 

I'm trying to output the waveform to PA8 (AF5 = TIM2_CH1) and PA10 (AF5 = TIM2_CH3). When I run the code the LED's light up ever so slightly, however, modifying the CCRx value does nothing to change this brightness, and even when I set it to 0 the LED's are still lit up slightly. Obviously, I've gone wrong somewhere but I just can't see what I need to modify to get this to work.  Do I need to do anything within the main body of code on top of the initialization to get it to work or is it something within the initialization routine I've got wrong?

I'd really appreciate any help with this.

Thanks. 

#tim-pwm #general-purpose-timers #stm32l011k4 #stm32l011 #stm32l0
3 REPLIES 3
Posted on May 10, 2018 at 12:55

    //Select output mode by writing CCxS bits in CCMRx register

    //CCMR1 for Capture/Compare 1 and 2 selection

    //CCMR2 for Capture/Compare 3 and 4 selection

    TIM2->CCMR1 |= TIM_CCMR1_CC1S;

    TIM2->CCMR2 |= TIM_CCMR2_CC3S;

No - this sets both CCxS bits, which is one of the Capture (i.e. input) modes.

You want them to be 0b00 for Compare.

So, depending on how the LEDs are connected, you are probably seing the current flowing through the pullups.

JW

Posted on May 10, 2018 at 14:14

Hi JW.

It doesn't like using binary in the files so I tried:

'TIM2->CCMR1 &= ~3UL;

 TIM2->CCMR2 &= ~3UL;'

which I assumed would do the same thing and clear the lower two bits of CCMRx to set them to 0. However, when I do this I get the same result as before, there is no noticeable difference in the LED's (apart from getting slightly dimmer when other LED's are turned on). 

Is there anything else I might have missed? From the pins on the board it goes into the LED's then through resistors before going to ground. 

Thanks

Posted on May 10, 2018 at 14:58

I don't see any obvious problem.

You can run the program for the initialization, then play with the registers in the debugger without further programming.

Try to set the pins in GPIO to (MODER bits = 0b01) and then toggle in ODR to see if the LEDs go high and down.

Then set back MODER to AF (0b10), check if AFIO is set correctly,  and then look at the timer registers. You should see CNT to change (depending on your debugger you may need to reload their value) between 0 and ARR. Check the values of CCR registers, and also those in CCMR and CCER registers. You should be also able to change the output by selecting one of the 'frozen' modes in CCMR, and also by toggling the polarity bit in CCER.

JW