STM32F4库函数初始化系列:PWM输出
1 void _TIM4_Configuration(void) 2 { 3 /* TIM4 Configuration --------------------------------------------------- 4 Generates 7 PWM signals with 4 different duty cycles: 5 TIM4CLK = 72 MHz, Prescaler = 0, TIM4 counter clock = 72 MHz 6 TIM4 frequency = TIM4CLK/(TIM4_Period + 1) = 17.57 KHz 7 - TIM4 Channel1 & Channel1N duty cycle = TIM4->CCR1 / (TIM4_Period + 1) = 50% 8 - TIM4 Channel2 & Channel2N duty cycle = TIM4->CCR2 / (TIM4_Period + 1) = 37.5% 9 - TIM4 Channel3 & Channel3N duty cycle = TIM4->CCR3 / (TIM4_Period + 1) = 25% 10 - TIM4 Channel4 duty cycle = TIM4->CCR4 / (TIM4_Period + 1) = 12.5% 11 ----------------------------------------------------------------------- */ 12 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 13 TIM_OCInitTypeDef TIM_OCInitStructure; 14 15 /* Time Base configuration */ 16 TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/880000/2)-1; //2000000 17 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 18 TIM_TimeBaseStructure.TIM_Period = 1023; 19 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4; 20 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; 21 22 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 23 24 TIM_ARRPreloadConfig(TIM4, ENABLE); 25 26 TIM_OCStructInit(&TIM_OCInitStructure); 27 28 /* Channel 1, 2,3 and 4 Configuration in PWM mode */ 29 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 30 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 31 TIM_OCInitStructure.TIM_Pulse = 0; 32 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; 33 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; 34 35 TIM_OC1Init(TIM4, &TIM_OCInitStructure); 36 TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); 37 38 TIM_OCInitStructure.TIM_Pulse = 0; 39 TIM_OC2Init(TIM4, &TIM_OCInitStructure); 40 TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); 41 42 TIM_OCInitStructure.TIM_Pulse = 0; 43 TIM_OC3Init(TIM4, &TIM_OCInitStructure); 44 TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); 45 46 TIM_OCInitStructure.TIM_Pulse = 0; 47 TIM_OC4Init(TIM4, &TIM_OCInitStructure); 48 TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); 49 50 /* TIM4 counter enable */ 51 TIM_Cmd(TIM4, ENABLE); 52 53 /* TIM4 Main Output Enable */ 54 TIM_CtrlPWMOutputs(TIM4, ENABLE); 55 } 56 57 void _TIM8_Configuration(void) 58 { 59 /* TIM8 Configuration --------------------------------------------------- 60 Generates 7 PWM signals with 4 different duty cycles: 61 TIM8CLK = 72 MHz, Prescaler = 0, TIM8 counter clock = 72 MHz 62 TIM8 frequency = TIM8CLK/(TIM8_Period + 1) = 17.57 KHz 63 - TIM8 Channel1 & Channel1N duty cycle = TIM8->CCR1 / (TIM8_Period + 1) = 50% 64 - TIM8 Channel2 & Channel2N duty cycle = TIM8->CCR2 / (TIM8_Period + 1) = 37.5% 65 - TIM8 Channel3 & Channel3N duty cycle = TIM8->CCR3 / (TIM8_Period + 1) = 25% 66 - TIM8 Channel4 duty cycle = TIM8->CCR4 / (TIM8_Period + 1) = 12.5% 67 ----------------------------------------------------------------------- */ 68 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 69 TIM_OCInitTypeDef TIM_OCInitStructure; 70 71 /* Time Base configuration */ 72 TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/880000)-1; //2000000 73 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 74 TIM_TimeBaseStructure.TIM_Period = 1023; 75 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4; 76 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; 77 78 TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); 79 80 TIM_ARRPreloadConfig(TIM8, ENABLE); 81 82 TIM_OCStructInit(&TIM_OCInitStructure); 83 84 /* Channel 1, 2,3 and 4 Configuration in PWM mode */ 85 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 86 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 87 TIM_OCInitStructure.TIM_Pulse = 0; 88 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; 89 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; 90 91 TIM_OC1Init(TIM8, &TIM_OCInitStructure); 92 TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); 93 94 TIM_OCInitStructure.TIM_Pulse = 0; 95 TIM_OC2Init(TIM8, &TIM_OCInitStructure); 96 TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); 97 98 TIM_OCInitStructure.TIM_Pulse = 0; 99 TIM_OC3Init(TIM8, &TIM_OCInitStructure); 100 TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); 101 102 TIM_OCInitStructure.TIM_Pulse = 0; 103 TIM_OC4Init(TIM8, &TIM_OCInitStructure); 104 TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); 105 106 /* TIM8 counter enable */ 107 TIM_Cmd(TIM8, ENABLE); 108 109 /* TIM8 Main Output Enable */ 110 TIM_CtrlPWMOutputs(TIM8, ENABLE); 111 } 112 113 TIM_SetCompare1( TIM4,500 );