Facebook
From Mature Mockingbird, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 246
  1. #include "stm32f4xx.h"
  2. #include "stm32f4_discovery.h"
  3. int counter;
  4.  
  5. int main(void)
  6. {
  7.  
  8.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  9.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  10.  
  11.         GPIO_InitTypeDef  GPIO_InitStructure;
  12.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
  13.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
  14.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
  15.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
  16.  
  17.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15 ;
  18.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  19.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  20.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  21.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  22.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  23.  
  24.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  25.         TIM_TimeBaseStructure.TIM_Period = 99;
  26.         TIM_TimeBaseStructure.TIM_Prescaler = 16799;
  27.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  28.         TIM_TimeBaseStructure.TIM_CounterMode =  TIM_CounterMode_Up;
  29.         TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  30.  
  31.         TIM_OCInitTypeDef TIM_OCInitStructure;
  32.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  33.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  34.         TIM_OCInitStructure.TIM_Pulse = 0;
  35.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  36.  
  37.         TIM_OC1Init(TIM4, &TIM_OCInitStructure);
  38.         TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
  39.  
  40.         TIM_OC2Init(TIM4, &TIM_OCInitStructure);
  41.         TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
  42.  
  43.         TIM_OC3Init(TIM4, &TIM_OCInitStructure);
  44.         TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
  45.  
  46.         TIM_OC4Init(TIM4, &TIM_OCInitStructure);
  47.         TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
  48.  
  49.         TIM_Cmd(TIM4, ENABLE);
  50.  
  51.  
  52.         int a=0;
  53.         int b=0;
  54.         int c=0;
  55.         int d=0;
  56.         for(;;)
  57.         {
  58.  
  59.                 if(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update)) {
  60.  
  61.  
  62.                         TIM4 -> CCR1 = a;
  63.                         a+=1;
  64.                         if(a > 99) a = 0;
  65.                         if(a>10 || b !=0 )
  66.                         {
  67.                         TIM4 -> CCR2 = b;
  68.                         b+=1;
  69.                         if(b > 99) b = 0;
  70.                         }
  71.                         if(a>20 || c !=0 )
  72.                         {
  73.                         TIM4 -> CCR3 = c;
  74.                         c+=1;
  75.                         if(c > 99) c = 0;
  76.                         }
  77.  
  78.                         if(a>30 || d !=0 )
  79.                         {
  80.                         TIM4 -> CCR4 = d;
  81.                         d+=1;
  82.                         if(d > 99) d = 0;
  83.                         }
  84.  
  85.  
  86.                         TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  87.                 }
  88.  
  89.  
  90.         }
  91. }
  92.