AnsweredAssumed Answered

STM32F303 PWM

Question asked by k.piotr on Jan 24, 2015
Latest reply on Jan 24, 2015 by k.piotr
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--;
}

Outcomes