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_AF_TIM1;
|
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_2MHz;
|
16 | GPIO_Init(GPIOA, &GPIO_InitStructure_A);
|
17 |
|
18 | /////////////////////////////////////////////////////////
|
19 | GPIO_InitStructure_B.GPIO_Mode = GPIO_AF_TIM1;
|
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_2MHz;
|
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 = 1;
|
41 | TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
42 | TIM_TimeBase_InitStructure.TIM_Period = 32000;
|
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 | // PWM
|
53 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
54 | TIM_OCStructInit(&TIM_OCInitStructure);
|
55 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
|
56 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
57 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
|
58 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
59 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
|
60 | TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
|
61 | TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
|
62 |
|
63 | TIM_OCInitStructure.TIM_Pulse = 16000;
|
64 | TIM_OC1Init(TIM1, &TIM_OCInitStructure);
|
65 | TIM_OCInitStructure.TIM_Pulse = 16000;
|
66 | TIM_OC2Init(TIM1, &TIM_OCInitStructure);
|
67 | TIM_OCInitStructure.TIM_Pulse = 16000;
|
68 | TIM_OC3Init(TIM1, &TIM_OCInitStructure);
|
69 |
|
70 | /////////////////////////////////////////////////////////
|
71 | /////////////////////////////////////////////////////////
|
72 | // Dead-Time
|
73 | TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
|
74 | TIM_BDTRStructInit(&TIM_BDTRInitStructure);
|
75 | /* Automatic Output enable, Break, dead time and lock configuration*/
|
76 | TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
|
77 | TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
|
78 | TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
|
79 | TIM_BDTRInitStructure.TIM_DeadTime = 100;
|
80 | TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
|
81 | TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
|
82 | TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
|
83 | TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
|
84 |
|
85 | /* Enable Timer */
|
86 | TIM_Cmd( TIM1, ENABLE );
|
87 | TIM_CtrlPWMOutputs(TIM1, ENABLE);
|
88 |
|
89 | // Set Duty
|
90 | TIM_SetCompare1(TIM1,16000);
|
91 | TIM_SetCompare2(TIM1,16000);
|
92 | TIM_SetCompare3(TIM1,16000);
|
93 |
|
94 |
|
95 | }
|