-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.c
434 lines (346 loc) · 12.8 KB
/
main.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_conf.h"
#include "stm32f10x_tim.h"
/*
Connections map:
Interface Buttons:
PB6 - "Phase -" button
PB7 - "Phase +" button
PB8 - "Amplitude +" button
PB9 - "Amplitude -" button
*/
#define PWM_TIMERS_PRESCALER 0
#define PWM_TIMERS_PERIOD 3550
#define PWM_TIMERS_INIT_PULSE 500
#define NUMBER_OF_POINTS_IN_ONE_CYCLE 60
// Senoid Pattern generated in MatLab Script on subfolder
int senoid_pwm[NUMBER_OF_POINTS_IN_ONE_CYCLE] = {1775 ,1961 ,2144 ,2324 ,2497 ,2663 ,2818 ,2963 ,3094 ,3211 ,3312 ,3397 ,3463 ,3511 ,3540 ,3550 ,3540 ,3511 ,3463 ,3397 ,3312 ,3211 ,3094 ,2963 ,2818 ,2663 ,2497 ,2324 ,2144 ,1961 ,1775 ,1589 ,1406 ,1226 ,1053 ,888 ,732 ,587 ,456 ,339 ,238 ,153 ,87 ,39 ,10 ,0 ,10 ,39 ,87 ,153 ,238 ,339 ,456 ,587 ,732 ,887 ,1053 ,1226 ,1406 ,1589};
// State Control Variables
int t_senoid_R_init_value = 0;
int t_senoid_S_init_value = NUMBER_OF_POINTS_IN_ONE_CYCLE/3;
int t_senoid_T_init_value = NUMBER_OF_POINTS_IN_ONE_CYCLE*2/3;
int t_senoid_R = 0;
int t_senoid_S = NUMBER_OF_POINTS_IN_ONE_CYCLE/3;
int t_senoid_T = NUMBER_OF_POINTS_IN_ONE_CYCLE*2/3;
float amplitudeFactor = 0.8;
int isGridTie = 0;
// Functions Prototypes
void debounceDelay(unsigned int nCount);
void InitializeTimer2(void);
void InitializeTimer3(void);
void InitializeTimer4IT(void);
void InitializeGPIOInterruptionOnPB6(void);
void InitializePWMChannels(void);
void InitializePWMGPIO(void);
void InitializeLEDGPIO(void);
void InitializeButtonsGPIO(void);
void toggle_led_PC13(void);
void changeRpositiveDutyCycle(int dutyCycle);
void changeRnegativeDutyCycle(int dutyCycle);
void changeSpositiveDutyCycle(int dutyCycle);
void changeSnegativeDutyCycle(int dutyCycle);
void changeTpositiveDutyCycle(int dutyCycle);
void changeTnegativeDutyCycle(int dutyCycle);
void updatePhaseR(void);
void updatePhaseS(void);
void updatePhaseT(void);
void incrementSenoidPosition(void);
void decrementSenoidPosition(void);
void buttonPressDetection(void);
void setup(){
InitializeLEDGPIO();
InitializeTimer2();
InitializeTimer3();
InitializePWMChannels();
InitializePWMGPIO();
InitializeGPIOInterruptionOnPB6();
InitializeTimer4IT();
}
int main(){
setup();
while(1){
buttonPressDetection();
}
return 0;
}
void incrementSenoidPosition(){
if ((t_senoid_R < 60) & (t_senoid_S < 60) & (t_senoid_T < 60)){
t_senoid_R ++;
t_senoid_S ++;
t_senoid_T ++;
}
}
void decrementSenoidPosition(){
if ((t_senoid_R > 0) & (t_senoid_S > 0) & (t_senoid_T > 0)){
t_senoid_R --;
t_senoid_S --;
t_senoid_T --;
}
}
void buttonPressDetection(){
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6) | GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) | GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8) | GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9)){
debounceDelay(100);
//detect 2 buttons long press
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6) & GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8)){
toggle_led_PC13();
if (isGridTie == 1){
isGridTie = 0;
} else {
isGridTie = 1;
}
} else if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6)){
/*Phase -*/
decrementSenoidPosition();
} else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)){
/*Phase +*/
incrementSenoidPosition();
} else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8)){
/*Amplitude -*/
//avoid negative amplitudes
if (!(amplitudeFactor <= 0)){
amplitudeFactor = amplitudeFactor - 0.05;
}
} else if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9)){
/*Amplitude +*/
// avoid sinewave distortions
if (!(amplitudeFactor >= 1)){
amplitudeFactor = amplitudeFactor + 0.05;
}
}
// avoid double press
debounceDelay(200);
}
}
void InitializeLEDGPIO(){
// Enable clock for GPIOC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
//GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //Analog voltages to be measured are connected to pins 1 and 2 of port A,
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Output push-pull mode
GPIO_Init(GPIOC, &GPIO_InitStructure); // calls the function to actually configure the port A.
GPIO_SetBits(GPIOC, GPIO_Pin_13);
}
void InitializeButtonsGPIO(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6
GPIO_Init(GPIOB , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PB7
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PB8
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
void InitializePWMGPIO(){
// Enable clock for GPIOA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PA3
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PA6
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PA7
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
void InitializeTimer2(){
// Enable clock for timer 2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
// Create and initialize TimeBase structure
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
// Configure TimeBase structure
TIM_TimeBaseStructure.TIM_Prescaler = PWM_TIMERS_PRESCALER;//SystemCoreClock/8000 - 1; // considering 8MHz, 1ms
TIM_TimeBaseStructure.TIM_Period = PWM_TIMERS_PERIOD; // 0..999
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
}
void InitializeTimer3(){
// Enable clock for timer 2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
// Create and initialize TimeBase structure
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
// Configure TimeBase structure
TIM_TimeBaseStructure.TIM_Prescaler = PWM_TIMERS_PRESCALER;//SystemCoreClock/8 - 1; // considering 8MHz, 1ms
TIM_TimeBaseStructure.TIM_Period = PWM_TIMERS_PERIOD; // 0..999
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE);
}
void InitializePWMChannels(){
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
// Configuration for the positive parte (non-denied PWM channles)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PWM_TIMERS_INIT_PULSE;
//TIM_OCx < X is the PWM channel number
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //PA0 - R positive
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //PA2 - S positive
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //PA6 - T positive
// Configuration for the negative parte (denied PWM channles)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //PA1 - R negative
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //PA3 - S negative
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //PA7 - T negative
}
void debounceDelay(unsigned int nCount){
unsigned int i, j;
for (i = 0; i < nCount; i++)
for (j = 0; j < 0x2AFF; j++);
}
void changeRpositiveDutyCycle(int dutyCycle){
TIM2->CCR1 = dutyCycle*amplitudeFactor;
}
void changeRnegativeDutyCycle(int dutyCycle){
TIM2->CCR2 = dutyCycle*amplitudeFactor;
}
void changeSpositiveDutyCycle(int dutyCycle){
TIM2->CCR3 = dutyCycle*amplitudeFactor;
}
void changeSnegativeDutyCycle(int dutyCycle){
TIM2->CCR4 = dutyCycle*amplitudeFactor;
}
void changeTpositiveDutyCycle(int dutyCycle){
TIM3->CCR1 = dutyCycle*amplitudeFactor;
}
void changeTnegativeDutyCycle(int dutyCycle){
TIM3->CCR2 = dutyCycle*amplitudeFactor;
}
void InitializeTimer4IT(){
// Configure NVIC
NVIC_InitTypeDef NVIC_InitStructure;
// No StructInit call in API
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Enable clock for timer 4
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
// Create and initialize TimeBase structure
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
// Configure TimeBase structure
TIM_TimeBaseStructure.TIM_Prescaler = 0; // considering 72MHz, 1ms
TIM_TimeBaseStructure.TIM_Period = 19999; // each 277,777777778 us
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
// Enable interrupt and starting timer 4
TIM_ITConfig(TIM4 , TIM_IT_Update , ENABLE);
TIM_Cmd(TIM4 , ENABLE);
}
void InitializeGPIOInterruptionOnPB6(){
// acording to https://scienceprog.com/interrupt-based-button-read-on-stm32f103zet6-board/
// Interruption pin = PB1
// Ext. Interruption Line = 0
/* Enable clock for GPIOB */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
/* Set pin 6 as input Pull Down*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// Connect PB6 to External Interrupt Event Controller line 0
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
//EXTI_StructInit(&EXTI_InitStruct);
// PD1 is connected to EXTI_Line1 */
EXTI_InitStruct.EXTI_Line = EXTI_Line1;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, DISABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_Init(&NVIC_InitStructure);
//select NVIC channel to configure
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
//set priority to lowest
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
//set subpriority to lowest
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
//enable IRQ channel
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//update NVIC registers
NVIC_Init(&NVIC_InitStructure);
//NVIC_EnableIRQ(EXTI0_IRQn);
}
//EXTIn_IRQHandler - where n is the line which is also the pin
void EXTI1_IRQHandler(void){
//Check if EXTI_Line0 is asserted
if(EXTI_GetITStatus(EXTI_Line1) != RESET){
toggle_led_PC13();
}
//we need to clear line pending bit manually
EXTI_ClearITPendingBit(EXTI_Line1);
}
void TIM4_IRQHandler(void){
// Update phases SPWMs
updatePhaseR();
updatePhaseS();
updatePhaseT();
// Update Interruption bit state
TIM_ClearITPendingBit(TIM4 ,TIM_IT_Update);
}
void updatePhaseR(){
if(t_senoid_R<60){
changeRpositiveDutyCycle(senoid_pwm[t_senoid_R]);
changeRnegativeDutyCycle(senoid_pwm[t_senoid_R]);
t_senoid_R++;
} else {
t_senoid_R = 0;
changeRpositiveDutyCycle(senoid_pwm[t_senoid_R]);
changeRnegativeDutyCycle(senoid_pwm[t_senoid_R]);
}
}
void updatePhaseS(){
if(t_senoid_S<60){
changeSpositiveDutyCycle(senoid_pwm[t_senoid_S]);
changeSnegativeDutyCycle(senoid_pwm[t_senoid_S]);
t_senoid_S++;
} else {
t_senoid_S = 0;
changeSpositiveDutyCycle(senoid_pwm[t_senoid_S]);
changeSnegativeDutyCycle(senoid_pwm[t_senoid_S]);
}
}
void updatePhaseT(){
if(t_senoid_T<60){
changeTpositiveDutyCycle(senoid_pwm[t_senoid_T]);
changeTnegativeDutyCycle(senoid_pwm[t_senoid_T]);
t_senoid_T++;
} else {
t_senoid_T = 0;
changeTpositiveDutyCycle(senoid_pwm[t_senoid_T]);
changeTnegativeDutyCycle(senoid_pwm[t_senoid_T]);
}
}
void toggle_led_PC13(){
//toggle LED on pin PC13
if(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)){
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
} else {
GPIO_SetBits(GPIOC, GPIO_Pin_13);
}
}