Facebook
From Oskar, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 317
  1. #include "stm32f4xx.h"
  2. #include "stm32f4_discovery.h"
  3.  
  4. int x;
  5. int main(void)
  6. {
  7.         /*Structures used in the configuration*/
  8.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  9.   TIM_OCInitTypeDef  TIM_OCInitStructure;
  10.   GPIO_InitTypeDef GPIO_InitStructure;
  11.  
  12.   /* Enable TIM4 Clock */
  13.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  14.  
  15.   /* Enable GPIOD Pins that are used for on board LED's */
  16.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  17.  
  18.   //Enabled GPIOB we are going to use PB6 which is linked to TIM4_CH1 according to the
  19.   //documentation
  20.  
  21.  
  22.   /* Initialise  pins 13, 14 and 15 D - relating to on board LED's*/
  23.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  24.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  25.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  26.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  27.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  28.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  29.  
  30.  
  31.  
  32.   GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
  33.   /* Setup timer defaults */
  34.   TIM_TimeBaseStructure.TIM_Period = 840;//665;
  35.   TIM_TimeBaseStructure.TIM_Prescaler = 100;//PrescalerValue;
  36.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  37.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  38.  
  39.   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  40.  
  41.  
  42.   /* Configure timer for PWM - channel 2 */
  43.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  44.   TIM_OCInitStructure.TIM_Pulse = 0;
  45.   TIM_OC2Init(TIM4, &TIM_OCInitStructure);
  46.   TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
  47.  
  48.   /* Configure timer for PWM - channel 3*/
  49.  
  50.  
  51.   /* Configure timer for PWM - channel 4 */
  52.  
  53.  
  54.   TIM_ARRPreloadConfig(TIM4, ENABLE);
  55.  
  56.   /* Start timer*/
  57.   TIM_Cmd(TIM4, ENABLE);
  58.  
  59.   int power = 0, j=0;
  60.   while(1)  //Loop forever
  61.   {
  62.  
  63.           TIM4->CCR2 = 100;//
  64.  
  65.            x=GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_13);
  66.           for(j=0; j<80000; j++);
  67.           power++;
  68.           if(power > 699){
  69.                   power = 0;
  70.                   for(j=0; j<80000; j++);
  71.           }
  72.     }
  73. }
  74.