1 | void Init_PWM ( void )
|
2 | {
|
3 | GPIO_InitTypeDef GPIO_InitStructure_A, GPIO_InitStructure_B;
|
4 | GPIO_StructInit(&GPIO_InitStructure_A);
|
5 | GPIO_StructInit(&GPIO_InitStructure_B);
|
6 |
|
7 | // IOs als Ausgang initialisieren (PortA und PortB)
|
8 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
9 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
10 |
|
11 | GPIO_InitStructure_A.GPIO_Mode = GPIO_Mode_AF;
|
12 | GPIO_InitStructure_A.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ;
|
13 | GPIO_InitStructure_A.GPIO_OType = GPIO_OType_PP;
|
14 | GPIO_InitStructure_A.GPIO_PuPd = GPIO_PuPd_DOWN;
|
15 | GPIO_InitStructure_A.GPIO_Speed = GPIO_Speed_100MHz;
|
16 | GPIO_Init(GPIOA, &GPIO_InitStructure_A);
|
17 |
|
18 | /////////////////////////////////////////////////////////
|
19 | GPIO_InitStructure_B.GPIO_Mode = GPIO_Mode_AF;
|
20 | GPIO_InitStructure_B.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
|
21 | GPIO_InitStructure_B.GPIO_OType = GPIO_OType_PP;
|
22 | GPIO_InitStructure_B.GPIO_PuPd = GPIO_PuPd_DOWN;
|
23 | GPIO_InitStructure_B.GPIO_Speed = GPIO_Speed_100MHz;
|
24 | GPIO_Init(GPIOB, &GPIO_InitStructure_B);
|
25 |
|
26 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM1);
|
27 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
|
28 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);
|
29 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
|
30 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM1);
|
31 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM1);
|
32 |
|
33 | /////////////////////////////////////////////////////////
|
34 | /////////////////////////////////////////////////////////
|
35 | // Timer
|
36 | TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
|
37 | TIM_TimeBaseStructInit(&TIM_TimeBase_InitStructure);
|
38 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
39 |
|
40 | TIM_TimeBase_InitStructure.TIM_Prescaler = PRESCALER_8KHZ;
|
41 | TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
42 | TIM_TimeBase_InitStructure.TIM_Period = PWM_PERIOD;
|
43 | TIM_TimeBase_InitStructure.TIM_ClockDivision = 0;
|
44 | TIM_TimeBase_InitStructure.TIM_RepetitionCounter = 0;
|
45 |
|
46 | TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);
|
47 | TIM_UpdateRequestConfig(TIM1, TIM_UpdateSource_Global);
|
48 | TIM_UpdateDisableConfig(TIM1,DISABLE);
|
49 |
|
50 |
|
51 | /////////////////////////////////////////////////////////
|
52 | /////////////////////////////////////////////////////////
|
53 |
|
54 | // PWM
|
55 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
56 | TIM_OCStructInit(&TIM_OCInitStructure);
|
57 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
58 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
59 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
|
60 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
61 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
|
62 | TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
|
63 | TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
|
64 |
|
65 | TIM_OCInitStructure.TIM_Pulse = 0;
|
66 | TIM_OC1Init(TIM1, &TIM_OCInitStructure);
|
67 | TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
|
68 |
|
69 | TIM_OCInitStructure.TIM_Pulse = 0;
|
70 | TIM_OC2Init(TIM1, &TIM_OCInitStructure);
|
71 | TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
|
72 |
|
73 | TIM_OCInitStructure.TIM_Pulse = 0;
|
74 | TIM_OC3Init(TIM1, &TIM_OCInitStructure);
|
75 | TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
|
76 |
|
77 | TIM_ARRPreloadConfig(TIM1, ENABLE);
|
78 | TIM_CtrlPWMOutputs(TIM1, ENABLE);
|
79 |
|
80 |
|
81 | /////////////////////////////////////////////////////////
|
82 | /////////////////////////////////////////////////////////
|
83 | // Dead-Time
|
84 | TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
|
85 | TIM_BDTRStructInit(&TIM_BDTRInitStructure);
|
86 | /* Automatic Output enable, Break, dead time and lock configuration*/
|
87 | TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
|
88 | TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
|
89 | TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
|
90 | TIM_BDTRInitStructure.TIM_DeadTime = 223;
|
91 | TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
|
92 | TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
|
93 | TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
|
94 | TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
|
95 |
|
96 | /* Enable Timer */
|
97 | TIM_Cmd( TIM1, ENABLE );
|
98 |
|
99 | DISABLE_PWM();
|
100 | // Set Duty
|
101 | TIM_SetCompare1(TIM1,PWM_PERIOD/2);
|
102 | TIM_SetCompare2(TIM1,PWM_PERIOD/2);
|
103 | TIM_SetCompare3(TIM1,PWM_PERIOD/2);
|
104 | }
|