jeudi 26 février 2015

PWM example for STM32F4 Discovery

/*PWM example for STM32F4 Discovery@author     OUESLATI SABRI@email        sabrioueslati1@gmail.com@ide        Keil uVision 5 -- IAR Embedded workbech   i will set the PWM using the timer 4 that is located on PD12   and we will see its signal on the diode PD12*/


#include "stm32f4xx.h"

#include "stm32f4xx_rcc.h"

#include "stm32f4xx_gpio.h"

#include "stm32f4xx_tim.h"


void InitializeTimer(void){ 

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);  

  TIM_TimeBaseInitTypeDef timerInitStructure;  

  timerInitStructure.TIM_Prescaler = 40000;  

  timerInitStructure.TIM_CounterMode = TIM_CounterMode_Down; 

   timerInitStructure.TIM_Period = 1050;    

timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;  

  timerInitStructure.TIM_RepetitionCounter = 0; 

   TIM_TimeBaseInit(TIM4, &timerInitStructure);   

 TIM_Cmd(TIM4, ENABLE);

}

void InitializePWMChannel()

  TIM_OCInitTypeDef outputChannelInit = {0,};

    outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;

    outputChannelInit.TIM_Pulse = 524; 

   outputChannelInit.TIM_OutputState = TIM_OutputState_Enable; 

   outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;


    TIM_OC1Init(TIM4, &outputChannelInit);   

 TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);


    GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);

}

void InitializeLEDs() {   

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


    GPIO_InitTypeDef gpioStructure;  

  gpioStructure.GPIO_Pin = GPIO_Pin_12;  

  gpioStructure.GPIO_Mode = GPIO_Mode_AF; 

   gpioStructure.GPIO_Speed = GPIO_Speed_50MHz; 

   GPIO_Init(GPIOD, &gpioStructure);

}

int main()  {    

InitializeLEDs(); 

   InitializeTimer();   

 InitializePWMChannel();  

  for (;;)    {    }

}


1 commentaire: