PWM on STM32F051 Discovery board

This is a quick note about how to setup the PWM functionality on the on-boards leds for this handy board. As soon as I become an expert in this microcontroller I’ll explain the source code. Meanwhile here it is what has been worked for me:

pwm.h

#ifndef __PWM_H__
#define __PWM_H__
#include "stm32f0xx.h"
#include "stm32f0xx_conf.h"

#ifdef __cplusplus
extern "C"
{
#endif

void TIM_Config(void);
void PWM_Init(uint32_t Frecuency);
void PWM_ConfigChannel(uint32_t Channel, uint32_t DutyCycle);
void PWM_Cmd(FunctionalState NewState);

#ifdef __cplusplus
}
#endif

#endif // __PWM_H__

pwm.c

#include "pwm.h"

static uint32_t TimerPeriod;

void TIM_Config(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;

/* GPIOC Clocks enable */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_0);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_0);
}
void PWM_Init(uint32_t Frequency)
{
 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_Config();
TimerPeriod = (SystemCoreClock / Frequency ) - 1;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
 TIM_TimeBaseStructure.TIM_Prescaler = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
}

void PWM_ConfigChannel(uint32_t Channel, uint32_t DutyCycle)
{
 TIM_OCInitTypeDef TIM_OCInitStructure;
 uint32_t ChannelPulse = 0;

 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

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

if(Channel == 0){
 TIM_OC3Init(TIM3, &TIM_OCInitStructure);
 }
 else if(Channel == 1){
 TIM_OC4Init(TIM3, &TIM_OCInitStructure);
 }
}

void PWM_Cmd(FunctionalState NewState)
{
 TIM_Cmd(TIM3, NewState);
}

main.c

void delaySW(uint32_t delay);

int main(void)
{
 uint32_t i=0;

 PWM_Init(1000);
 PWM_ConfigChannel(0, 99);
 PWM_ConfigChannel(1, 5);
 PWM_Cmd(ENABLE);

while(1)
 {
 for(i=0;i<100;i+=5){
 PWM_ConfigChannel(0, i);
 PWM_ConfigChannel(1, 100-i);
 delaySW(5000);
 }

for(;i>0;i-=5){
 PWM_ConfigChannel(0, i);
 PWM_ConfigChannel(1, 100-i);
 delaySW(5000);
 }
};

return 0;
}

void delaySW(uint32_t delay)
{
 uint32_t i=10000;
for(;delay > 0; delay--){
 for(;i>0;i--)
 ;
 }
}
Anuncios

Responder

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

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. 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 )

Google+ photo

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

Conectando a %s