-
Notifications
You must be signed in to change notification settings - Fork 0
/
initial_tuning.c
313 lines (234 loc) · 7.73 KB
/
initial_tuning.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// file contanes subriutines for initial tuning pheropherals
#include "main.h"
// 1. Clock configuration
void CLK_Config(void)
{
/* Initialization of the clock */
/* Clock divider to HSI/1 */
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
CLK_LSICmd(ENABLE);
}
// 2. Ports configuration
void GPIO_Config (void)
{
// port A
GPIO_Init(GPIOA, E1A, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOA, E1B, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOA, E2A, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOA, E2B, GPIO_MODE_IN_PU_NO_IT);
// port B
GPIO_Init(GPIOB, BT1, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOB, BT2, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOB, BT3, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOB, BT4, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOB, BT5, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOB, BT6, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOB, BT7, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_PU_NO_IT);
// port C
GPIO_Init(GPIOC, OP1, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOC, OP1);
GPIO_Init(GPIOC, OP2, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOC, OP2);
GPIO_Init(GPIOC, OP3, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOC, OP3);
GPIO_Init(GPIOC, OP4, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOC, OP4);
GPIO_Init(GPIOC, LED_CLK, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOC, LED_DATA, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOC, LED_COM_C, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_WriteLow(GPIOC, LED_COM_C);
// port D
GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOD, ON1, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOD, ON1);
GPIO_Init(GPIOD, ON2, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOD, ON2);
GPIO_Init(GPIOD, BE, GPIO_MODE_OUT_PP_LOW_SLOW);
GPIO_Init(GPIOD, ON3, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOD, ON3);
GPIO_Init(GPIOD, ON4, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteLow(GPIOD, ON4);
GPIO_Init(GPIOD, GPIO_PIN_7, GPIO_MODE_IN_PU_NO_IT);
// port E
GPIO_Init(GPIOE, E5A, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOE, GPIO_PIN_1, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOE, GPIO_PIN_2, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOE, E5B, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOE, E4A, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOE, E4B, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOE, GPIO_PIN_7, GPIO_MODE_IN_PU_NO_IT);
// port G
GPIO_Init(GPIOG, E3B, GPIO_MODE_IN_PU_NO_IT);
GPIO_Init(GPIOG, E3A, GPIO_MODE_IN_PU_NO_IT);
// for (i=0;i<99;i++) OutPWM[i] |= LED_COM_C;
}
// 5. Timers configuration
void Timers_Config (void)
{
/* TIM4 configuration:
- TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 64 so the TIM1 counter
clock used is 16 MHz / 64 = 250 000 Hz
- With 250 000 Hz we can generate time base:
max time base is 1.024 ms if TIM4_PERIOD = 255 --> (255 + 1) / 250000 = 1.024 ms
min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 250000 = 0.008 ms
- we need to generate a time base equal to 1 ms
so TIM4_PERIOD = (0.001 * 250000 - 1) = 250 */
TIM4_TimeBaseInit(TIM4_PRESCALER_64, 250); // Time base configuration
TIM4_ClearFlag(TIM4_FLAG_UPDATE);
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
TIM4_Cmd(ENABLE);
/* TIM2 configuration:
TIM2 èñïîëüçóåòñÿ äëÿ ôîðìèðîâàíèÿ ñòàðòà èìïóëñîâ â êàíàëàõ ñ ÷àñòîòîé
îò 100 äî 1000 Ãö (èçìåíåíèå îò 100 äî 1000 Ãö ñ øàãîì 100 us çà 0,5 ñåê, è îáðàòíî çà 0,5 ñåê
è òàê 4 ðàçà ïîäðÿä (ïîñûëêà 4 ñåêóíäû)), òîåñòü äëÿ ïåðèîäè÷åñêîãî çàïóñêà TIM1
(äåëåå ñàìó îñöèëëîãðàììó 4-õ êàíàëîâ îäíîãî èìïóëüñà ôîðìèðóåò TIM1)
- TIM2CLK îò 16 MHz ñ äåëèòåëåì 16 ïîëó÷èì 16 MHz / 16 = 1 000 000 Hz,
èëè 1 us, ò.å. 100 us ýòî 100 òàéìîâ, à 10 ms - 10000 òàéìîâ.
*/
TIM2_TimeBaseInit(TIM2_PRESCALER_16, 10000); // Time base configuration ïî óìîë÷àíèþ
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
// TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 120, 0);
}
// 4. Beep configuration
void Beep_Config (void)
{
BEEP_LSICalibrationConfig (LSIMeasurment());
BEEP_Init(BEEP_FREQUENCY_4KHZ);
// BEEP-> CSR = 0x0e;
// BEEP-> CSR |= 0x80;
// BEEP-> CSR |= 0x20;
// BEEP_Cmd(ENABLE);
}
void SPI_Config (void)
{
/* Initialize SPI */
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_MASTER,
SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX,
SPI_NSS_SOFT, 0x01);
SPI_Cmd(ENABLE);
}
void Variables_Config(void)
{
RealTimeSecond = 0;
RealTimeMinute = 0;
RealTimeCounter = 0;
RealTimeSecond = 0;
RealTimeMinute = 0;
Chenal4Number = 0;
Chenal3Number = 0;
Chenal2Number = 0;
Chenal1Number = 0;
Chenal1Enable = Clear;
Chenal2Enable = Clear;
Chenal3Enable = Clear;
Chenal4Enable = Clear;
Chenal4NumberPWM = 0;
Chenal3NumberPWM = 0;
Chenal2NumberPWM = 0;
Chenal1NumberPWM = 0;
Chenal1NumberShow = NumberToNumberShow(Chenal1Number, Clear, Clear);
Chenal2NumberShow = NumberToNumberShow(Chenal2Number, Clear, Clear);
Chenal3NumberShow = NumberToNumberShow(Chenal3Number, Clear, Clear);
Chenal4NumberShow = NumberToNumberShow(Chenal4Number, Clear, Clear);
RealTimeSecondNumberShow = NumberToNumberShow(RealTimeSecond, Clear, Set);
RealTimeMinuteNumberShow = NumberToNumberShow(RealTimeMinute, LowPoint, Set);
Device.State = Stop;
Device2.Chenal1Show = Set;
Device2.Chenal2Show = Set;
Device2.Chenal3Show = Set;
Device2.Chenal4Show = Set;
StateButtons = GPIO_ReadInputData ( GPIOB );
LedData = 0;
// Mode define
LedData = LedA;
Device.Mode = ModeA;
/*
if ((StateButtons & 0x20) == 0)
{
LedData = LedA;
Device.Mode = ModeA;
}
else
{
if ((StateButtons & 0x10) == 0)
{
LedData = LedB;
Device.Mode = ModeB;
}
else
{
if ((StateButtons & 0x08) == 0)
{
LedData |= LedC;
Device.Mode = ModeC;
}
else
{
LedData |= LedA;
Device.Mode = ModeA;
}
}
}
*/
// Polarity define
LedData |= LedP;
Device.Polarity = PolarityP;
/*
if ((StateButtons & 0x04) == 0)
{
LedData |= LedP;
Device.Polarity = PolarityP;
}
else
{
if ((StateButtons & 0x02) == 0)
{
LedData |= LedN;
Device.Polarity = PolarityN;
}
else
{
if ((StateButtons & 0x01) == 0)
{
LedData |= LedPN;
Device.Polarity = PolarityPN;
}
else
{
LedData |= LedP;
Device.Polarity = PolarityP;
}
}
}
*/
}
uint32_t LSIMeasurment(void)
{
uint32_t lsi_freq_hz = 0x0;
uint32_t fmaster = 0x0;
uint16_t ICValue1 = 0x0;
uint16_t ICValue2 = 0x0;
fmaster = CLK_GetClockFreq(); /* Get master frequency */
AWU->CSR |= AWU_CSR_MSR; /* Enable the LSI measurement: LSI clock connected to timer Input Capture 1 */
/* Measure the LSI frequency with TIMER Input Capture 1 */
/* Capture only every 8 events!!! */
/* Enable capture of TI1 */
TIM3_ICInit(TIM3_CHANNEL_1, TIM3_ICPOLARITY_RISING, TIM3_ICSELECTION_DIRECTTI, TIM3_ICPSC_DIV8, 0);
TIM3_Cmd(ENABLE);
while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1); /* wait a capture on cc1 */
ICValue1 = TIM3_GetCapture1(); /* Get CCR1 value*/
TIM3_ClearFlag(TIM3_FLAG_CC1);
while ((TIM3->SR1 & TIM3_FLAG_CC1) != TIM3_FLAG_CC1); /* wait a capture on cc1 */
ICValue2 = TIM3_GetCapture1(); /* Get CCR1 value*/
TIM3_ClearFlag(TIM3_FLAG_CC1);
TIM3->CCER1 &= (uint8_t)(~TIM3_CCER1_CC1E); /* Disable IC1 input capture */
TIM3_Cmd(DISABLE);
lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1); /* Compute LSI clock frequency */
AWU->CSR &= (uint8_t)(~AWU_CSR_MSR); /* Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 */
return (lsi_freq_hz);
}