cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303 PWM

deredas1
Associate
Posted on January 24, 2015 at 17:52

Hello, i have a problem with PWM. I am trying to turn on the led (PE9) on TIM1_CH1.

I want to make it brighter and brighter till counter equals 256 (2^8=256)

#include <
stm32f30x.h
>
#include <
stm32f30x_gpio.h
>
#include <
stm32f30x_usart.h
>
#include <
stm32f30x_rcc.h
>
#include <
stm32f30x_tim.h
>
#include <
stdlib.h
>
int timeout=0;
void delay_ms(int ms)
{
timeout = ms;
while(timeout > 0);
}
int main(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC|
RCC_AHBPeriph_GPIOD|RCC_AHBPeriph_GPIOE|RCC_AHBPeriph_GPIOF, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE,&GPIO_InitStruct);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_9);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_Period=1000;
TIM_TimeBaseInitStruct.TIM_Prescaler=72; //72mhz/72=1mhz
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStruct);
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCInitStruct.TIM_OCMode= TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCNPolarity_High;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCIdleState=TIM_OCIdleState_Set;
TIM_OCInitStruct.TIM_Pulse=250;
TIM_OC1Init(TIM1,&TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable );
TIM_ARRPreloadConfig(TIM1, DISABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
//1000ms - 72000 000
//1ms - x
//x = 72mln 1/1000 = 72mln /1000 = 72000
SysTick_Config(72000);
uint8_t moc = 0;
while(1)
{
TIM_SetCompare1(TIM1, moc++);
delay_ms(10);
}
}
void SysTick_Handler(void)
{
if(timeout > 0)
timeout--;
}

2 REPLIES 2
Posted on January 24, 2015 at 19:21

PE9 TIM1_CH1 is AF2 not AF9

TIM1's Output Compare structure needs to be filled in more completely.

Prescaler and Period values are N-1, so 72-1 and 1000-1

The range of the OC width is 0-1000, not 0-255
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
deredas1
Associate
Posted on January 24, 2015 at 20:04

Where did you find this info that TIM1_CH1 is AF2 ? Do you know where can i find this information in documentation ?  When i changed it to AF2 it stareted to  work....

I also changed this two lines:

    TIM_TimeBaseInitStruct.TIM_Period=255;

    TIM_TimeBaseInitStruct.TIM_Prescaler=72000;

Iam not sure if i can write 72000 becuase TIM_Prescaler is 16-bit int. The max value form TIM_Prescaler is 65536, isn't it ?

Now the timer works with ca 1 kHz frequency. Am i right ?

Can i say that the led is getting brighter and brighter during next 2,56 second  ?