-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrv_pwm_out.c
226 lines (181 loc) · 9.02 KB
/
Drv_pwm_out.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/******************** (C) COPYRIGHT 2017 ANO Tech ********************************
* 作者 :匿名科创
* 官网 :www.anotc.com
* 淘宝 :anotc.taobao.com
* 技术Q群 :190169595
* 描述 :PWM输出
**********************************************************************************/
#include "Drv_pwm_out.h"
#include "include.h"
#include "Ano_Math.h"
//21分频到 84000000/21 = 4M 0.25us
/*初始化高电平时间1000us(4000份)*/
#define INIT_DUTY 4000 //u16(1000/0.25)
/*频率400hz*/
#define HZ 400
/*精度10000,每份0.25us*/
#define ACCURACY 10000 //u16(2500/0.25) //accuracy
/*设置飞控控制信号转换比例为4*/
#define PWM_RADIO 4//(8000 - 4000)/1000.0
u8 PWM_Out_Init () //400hz
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
uint16_t PrescalerValue = 0;
u32 hz_set = ACCURACY * HZ;
GPIO_StructInit(&GPIO_InitStructure);
TIM_TimeBaseStructInit ( &TIM_TimeBaseStructure );
TIM_OCStructInit ( &TIM_OCInitStructure );
hz_set = LIMIT ( hz_set, 1, 84000000 );
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM5, ENABLE );
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_TIM8, ENABLE );
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_TIM1, ENABLE );
RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIO_Pin_0 | GPIO_Pin_1 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init ( GPIOA, &GPIO_InitStructure );
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5);
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5);
GPIO_PinAFConfig ( GPIOA, GPIO_PinSource2, GPIO_AF_TIM5 );
GPIO_PinAFConfig ( GPIOA, GPIO_PinSource3, GPIO_AF_TIM5 );
/* Compute the prescaler value */
PrescalerValue = ( uint16_t ) ( ( SystemCoreClock / 2 ) / hz_set ) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = ACCURACY;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit ( TIM5, &TIM_TimeBaseStructure );
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// /* PWM1 Mode configuration: Channel1 */
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
// TIM_OC1Init(TIM5, &TIM_OCInitStructure);
// TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);
// /* PWM1 Mode configuration: Channel2 */
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
// TIM_OC2Init(TIM5, &TIM_OCInitStructure);
// TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC3Init ( TIM5, &TIM_OCInitStructure );
TIM_OC3PreloadConfig ( TIM5, TIM_OCPreload_Enable );
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC4Init ( TIM5, &TIM_OCInitStructure );
TIM_OC4PreloadConfig ( TIM5, TIM_OCPreload_Enable );
TIM_ARRPreloadConfig ( TIM5, ENABLE );
TIM_Cmd ( TIM5, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init ( GPIOE, &GPIO_InitStructure );
GPIO_PinAFConfig ( GPIOE, GPIO_PinSource9, GPIO_AF_TIM1 );
GPIO_PinAFConfig ( GPIOE, GPIO_PinSource11, GPIO_AF_TIM1 );
GPIO_PinAFConfig ( GPIOE, GPIO_PinSource13, GPIO_AF_TIM1 );
GPIO_PinAFConfig ( GPIOE, GPIO_PinSource14, GPIO_AF_TIM1 );
/* Compute the prescaler value */
PrescalerValue = ( uint16_t ) ( ( SystemCoreClock ) / hz_set ) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = ACCURACY;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit ( TIM1, &TIM_TimeBaseStructure );
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC1Init ( TIM1, &TIM_OCInitStructure );
//TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC2Init ( TIM1, &TIM_OCInitStructure );
//TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC3Init ( TIM1, &TIM_OCInitStructure );
//TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC4Init ( TIM1, &TIM_OCInitStructure );
//TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs ( TIM1, ENABLE );
TIM_ARRPreloadConfig ( TIM1, ENABLE );
TIM_Cmd ( TIM1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init ( GPIOC, &GPIO_InitStructure );
GPIO_PinAFConfig ( GPIOC, GPIO_PinSource8, GPIO_AF_TIM8 );
GPIO_PinAFConfig ( GPIOC, GPIO_PinSource9, GPIO_AF_TIM8 );
/* Compute the prescaler value */
PrescalerValue = ( uint16_t ) ( ( SystemCoreClock ) / hz_set ) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = ACCURACY;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit ( TIM8, &TIM_TimeBaseStructure );
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
/* PWM1 Mode configuration: Channel3 */
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC3Init ( TIM8, &TIM_OCInitStructure );
//TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
//TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = INIT_DUTY;
TIM_OC4Init ( TIM8, &TIM_OCInitStructure );
//TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs ( TIM8, ENABLE );
TIM_ARRPreloadConfig ( TIM8, ENABLE );
TIM_Cmd ( TIM8, ENABLE );
if ( hz_set > 84000000 )
{
return 0;
}
else
{
return 1;
}
}
void SetPwm ( int16_t pwm[MOTORSNUM] )
{
TIM1->CCR4 = PWM_RADIO * ( pwm[0] ) + INIT_DUTY; //1
TIM1->CCR3 = PWM_RADIO * ( pwm[1] ) + INIT_DUTY; //2
TIM1->CCR2 = PWM_RADIO * ( pwm[2] ) + INIT_DUTY; //3
TIM1->CCR1 = PWM_RADIO * ( pwm[3] ) + INIT_DUTY; //4
// TIM5->CCR4 = PWM_RADIO *( pwm_tem[CH_out_Mapping[4]] ) + INIT_DUTY; //5
// TIM5->CCR3 = PWM_RADIO *( pwm_tem[CH_out_Mapping[5]] ) + INIT_DUTY; //6
// TIM8->CCR4 = PWM_RADIO *( pwm_tem[CH_out_Mapping[6]] ) + INIT_DUTY; //7
// TIM8->CCR3 = PWM_RADIO *( pwm_tem[CH_out_Mapping[7]] ) + INIT_DUTY; //8
}
/******************* (C) COPYRIGHT 2014 ANO TECH *****END OF FILE************/