PWM on the STM32L152 Discovery board

In this post I will show you a little driver for getting a PWM output on such a board. As I pointed out in a previous post, this board is showing a bad engineneering practices, like the lack of at least a single serial port. And while working on the PWM driver I noted that there are not timer outputs easily available, so I just found a single one, TIM2_CH1 on PA5. For now that is what I need, however I’ll get into troubles whenever I need more PWMs (without sacrificing the LCD and the Touch pads).

Well, here is the driver, and please note that there are several changes regard to a previous driver for the STM32F051 Discovery board:

void PWM0_Init(void);
void PWM0_Config(uint32_t Frecuency);
void PWM0_ConfigChannel(uint32_t Channel, uint32_t DutyCycle);
void PWM0_Cmd(FunctionalState NewState);

void PWM0_Init(void)
 GPIO_InitTypeDef GPIO_InitStructure;


 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);

 GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);

void PWM0_Config(uint32_t Frequency)
 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 TIM_OCInitTypeDef TIM_OCInitStructure;
 uint16_t TimerPeriod;

 TimerPeriod = (SystemCoreClock / Frequency ) - 1;

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);

 TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
 TIM_TimeBaseStructure.TIM_Prescaler = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

 TIM_OC1Init(TIM2, &TIM_OCInitStructure);
 TIM_OC2Init(TIM2, &TIM_OCInitStructure);
 TIM_OC3Init(TIM2, &TIM_OCInitStructure);
 TIM_OC4Init(TIM2, &TIM_OCInitStructure);

void PWM0_ConfigChannel(uint32_t Channel, uint32_t DutyCycle)
 TIM_OCInitTypeDef TIM_OCInitStructure;
 uint16_t ChannelPulse = 0;

 if (Channel == 0) {

   ChannelPulse = (uint16_t) ((DutyCycle * 10 *(TimerPeriod - 1)) / 1000);

   TIM_OCInitStructure.TIM_Pulse = ChannelPulse;
   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
   TIM_OC1Init(TIM2, &TIM_OCInitStructure);
#if 0 
 else if (Channel == 1) {
  TIM_OC4Init(TIM2, &TIM_OCInitStructure);

void PWM0_Cmd(FunctionalState NewState)
 TIM_Cmd(TIM2, NewState);
int main(void)


 PWM0_ConfigChannel(0, 25);

 while (1)
 PWM0_ConfigChannel(0, 25);

 PWM0_ConfigChannel(0, 75);

As always, your comments are appreciated.


4 comentarios sobre “PWM on the STM32L152 Discovery board

    1. Hola, últimamente he estado utilizando otro micro para cosas de mi trabajo, pero en cuanto me haga de un poco de tiempo libre volveré al STM32 para corregir y aumentar los ejemplos que he puesto.


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s