-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotor_isr.c
424 lines (401 loc) · 13.6 KB
/
Motor_isr.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
#include "defs.h"
#include "hardware.h"
#include "Motor_isr.h"
#include "hall_state.h"
#include "TuningInterface.h"
#include "IIR_Filter.h"
#include "BEMF_filter.h"
#include "stdio.h"
// ----------------------Function prototypes ---------------------------
void CheckZeroCrossing(void);
unsigned int ThirtyDegreeTimeAverage(void);
// ----------------- Global Variable Declarations ----------------------
unsigned int pot; // stores the potentiometer value
unsigned int HalltoSector[8] = {-1,2,4,3,0,1,5,-1};
unsigned int SectortoState[6] = {STATE0,STATE1,STATE2,STATE3,STATE4,STATE5};
unsigned int HallState; // Hall effect sensor state
int Sector = 0; // Sector (0 - 5)
int OldSector; // Stores old sector
int vpha, vphb, vphc, vbus; // stores the ADC result for phase A, B, and C and the bus voltage measurement
unsigned int Timer1TimeoutCntr = 0;
unsigned int BlankingCount = 3;
unsigned int phase_advance = 0;
unsigned int SpeedPtr;
unsigned int RunMode = 0;
unsigned int SensorlessStartState = 0;
unsigned int MediumEventCounter = 0;
unsigned int SlowEventCounter = 0;
long accumulator_c;
int signal_average;
unsigned long Speed = 0;
int vpha_filtered_sample;
int vphb_filtered_sample;
int vphc_filtered_sample;
unsigned int NextSectorState;
unsigned int SixtyDegreeTime[6];
unsigned int OneEightyDegreeTime[16];
volatile struct ControlFlags ControlFlags;
unsigned int pos_ptr = 0;
unsigned int TMR4Save;
/*---------------------------------------------------------------------
Function Name: ADCInterrupt
Description: ADC Interrupt Handler
Inputs: None
Returns: None
-----------------------------------------------------------------------*/
/*---------------------------------------------------------------------
Function Name: ADCInterrupt
Description: ADC Interrupt Handler
Inputs: None
Returns: None
-----------------------------------------------------------------------*/
void __attribute__((__interrupt__,no_auto_psv)) _ADCInterrupt( void ) // occurs at a rate of 81.920 kHz
{
int i;
/* reset ADC interrupt flag */
IFS0bits.ADIF = 0;
// Read the potentiometer and phase voltages.
pot = (pot + POTBUF) >> 1;
if (BlankingCount) // if the blanking count hasn't expired, feed the Back EMF
{ // filters the last filtered Back EMF sample (rather than the unfiltered sample.)
BlankingCount--;
vpha = vpha_filtered_sample;
vphb = vphb_filtered_sample;
vphc = vphc_filtered_sample;
// vpha = (3*vpha_filtered_sample + VPHABUF)>>2; // This three lines can be used an a alternative to the
// vphb = (3*vphb_filtered_sample + VPHBBUF)>>2; // previous three lines
// vphc = (3*vphc_filtered_sample + VPHCBUF)>>2;
}
else
{
vpha = VPHABUF;
vphb = VPHBBUF;
vphc = VPHCBUF;
}
// Get the bus voltage and do a little averaging
vbus = (vbus + VBUSBUF) >> 1;
switch (RunMode)
{
case MOTOR_OFF:
OVDCON = 0;
break;
case HALL_SENSOR_MODE:
// Read the hall sensors. This is only done If running sensored.
// Read Hall sensors to get position.
do
{
HallState = (PORTD >> 8) & 0x0007;
Sector = HalltoSector[HallState];
i++;
} while((Sector == -1) && (i<4));
if(Sector != OldSector)
{
Commutate(Sector);
TMR2 = 0;
IFS0bits.T2IF = 0;
}
case SENSORLESS_START:
case SENSORLESS_RUNNING :
CheckZeroCrossing();
break;
default:
if (RunMode > (NO_OF_RUN_MODES - 1)) RunMode = 0;
break;
}
}
/*---------------------------------------------------------------------
Function Name: T1Interrupt
Description: T1 Interrupt Handler
Inputs: None
Returns: None
-----------------------------------------------------------------------*/
void __attribute__((__interrupt__,no_auto_psv)) _T1Interrupt( void )
{
IFS0bits.T1IF = 0;
if (Timer1TimeoutCntr++ >= NumOfTimer1TimeOuts) // When Timer 1 overflows the algorithm is lost
{
IEC0bits.T1IE = 0; // turn off Timer 1 and stop the motor
RunMode = MOTOR_OFF;
}
}
void __attribute__((__interrupt__,no_auto_psv)) _PWMInterrupt( void ) // Occurs every 50us or at a rate of 20kHz
{
IFS2bits.PWMIF = 0; // Clear the PWM interrupt flag
if (++SlowEventCounter >= 200) // Fire Slow event every 10ms
{
SlowEventCounter = 0;
ControlFlags.SlowEventFlag = 1;
}
if (++MediumEventCounter >= 20) // Fire Medium event every 1ms
{
MediumEventCounter = 0;
ControlFlags.MediumEventFlag = 1;
}
if (RunMode == SENSORLESS_START)
{
MediumEventCounter += 9; // Fire Medium event every 100us in SENSORLESS_START mode
}
}
/*---------------------------------------------------------------------
Function Name: T2Interrupt
Description: T2 Interrupt Handler
Inputs: None
Returns: None
-----------------------------------------------------------------------*/
void __attribute__((__interrupt__,no_auto_psv)) _T2Interrupt( void ) // TMR 2 is never turned on in Hall Mode
{
TMR2 = 0; // clear TMR2
IFS0bits.T2IF = 0;
if (RunMode == SENSORLESS_RUNNING)
{
Sector++; // Increment Sector (there are 6 total)
if(Sector > 5) Sector = 0;
Commutate(Sector); // Change the PWM output for next sector
}
}
void __attribute__((__interrupt__,no_auto_psv)) _T3Interrupt( void ) // TMR 3 is never turned on in Hall Mode
{
T3CONbits.TON = 0; // turn off TMR3
TMR3 = 0;
TMR2 = 0;
IFS0bits.T2IF = 0;
IFS0bits.T3IF = 0;
IEC0bits.T3IE = 0;
Timer1TimeoutCntr = 0; // A commutation has occured so the Timer 1 timeout counter can be reset
if (RunMode == SENSORLESS_RUNNING)
{
Sector = NextSectorState;
Commutate(Sector); // Force commutation
}
}
void CheckZeroCrossing(void)
{
static unsigned int ZeroCrossState = 0;
unsigned int ThirtyDegreeTime;
unsigned int ThreeSixtyDegreeTime;
unsigned int phase_delay;
static int vbus_offset = 0;
if (ZeroCrossState < 6) // If Low Speed Mode
{
BEMF_phaseA_Filter.pCoefs = &BEMF_filterCoefs_49152Hz;
BlockIIRTransposeFilter( &BEMF_phaseB_Filter, &vphb, &vphb_filtered_sample, 1 );
BlockIIRTransposeFilter( &BEMF_phaseC_Filter, &vphc, &vphc_filtered_sample, 1 );
}
else
BEMF_phaseA_Filter.pCoefs = &BEMF_filterCoefs_81940Hz;
BlockIIRTransposeFilter( &BEMF_phaseA_Filter, &vpha, &vpha_filtered_sample, 1 ); // Get a filtered sample
// finds the center voltage of the phase signal even under different loads
signal_average = vbus/2 + vbus_offset;
//printf("signal average = %f\r\n",vbus);
accumulator_c += vpha_filtered_sample - signal_average;
vbus_offset = accumulator_c >> 13;
if (ZeroCrossState < 6)
phase_delay = FILTER_PHASE_DELAY + PROCESSING_DELAY + phase_advance;
else
phase_delay = FILTER_PHASE_DELAY + PROCESSING_DELAY_HS + phase_advance;
switch(ZeroCrossState)
{
// States 0 - 5 implement the low speed mode of this algorithm. All three phase voltages are sampled. The sampling frequency
// is 49kHz when running in high speed mode.
case 0:
if (vpha_filtered_sample < signal_average) // signal is falling look for when it falls below center voltage
{
SixtyDegreeTime[ZeroCrossState] = TMR1;
TMR1 = 0;
ThirtyDegreeTime = ThirtyDegreeTimeAverage();
if (ThirtyDegreeTime < phase_delay)
ThirtyDegreeTime = phase_delay;
PR3 = ThirtyDegreeTime - phase_delay;
NextSectorState = 3;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
OneEightyDegreeTime[SpeedPtr&0x000F] = ThirtyDegreeTime*6;
SpeedPtr++;
ZeroCrossState++;
}
break;
case 1:
if (vphc_filtered_sample > signal_average)
{
SixtyDegreeTime[ZeroCrossState] = TMR1;
TMR1 = 0;
ThirtyDegreeTime = ThirtyDegreeTimeAverage();
if (ThirtyDegreeTime < phase_delay)
ThirtyDegreeTime = phase_delay;
PR3 = ThirtyDegreeTime - phase_delay;
NextSectorState = 4;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
OneEightyDegreeTime[SpeedPtr&0x000F] = ThirtyDegreeTime*6;
SpeedPtr++;
ZeroCrossState++;
}
break;
case 2:
if (vphb_filtered_sample < signal_average)
{
SixtyDegreeTime[ZeroCrossState] = TMR1;
TMR1 = 0;
ThirtyDegreeTime = ThirtyDegreeTimeAverage();
if (ThirtyDegreeTime < phase_delay)
ThirtyDegreeTime = phase_delay;
PR3 = ThirtyDegreeTime - phase_delay;
NextSectorState = 5;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
OneEightyDegreeTime[SpeedPtr&0x000F] = ThirtyDegreeTime*6;
SpeedPtr++;
ZeroCrossState++;
}
break;
case 3:
if (vpha_filtered_sample > signal_average)
{
SixtyDegreeTime[ZeroCrossState] = TMR1;
TMR1 = 0;
ThirtyDegreeTime = ThirtyDegreeTimeAverage();
if (ThirtyDegreeTime < phase_delay)
ThirtyDegreeTime = phase_delay;
PR3 = ThirtyDegreeTime - phase_delay;
NextSectorState = 0;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
OneEightyDegreeTime[SpeedPtr&0x000F] = ThirtyDegreeTime*6;
SpeedPtr++;
ZeroCrossState++;
// do the change over from LowSpeedMode to HighSpeed Mode
if (ControlFlags.HighSpeedMode)
{
ZeroCrossState = 6;
TMR2 = 0;
PR2 = ThirtyDegreeTime*2;
IFS0bits.T2IF = 0;
IEC0bits.T2IE = 1;
ControlFlags.TakeSnapshot = 1; // take shapshot on crossover
ADCON1bits.ADON = 0; // Turn ADC module off before modifying control bits;
ADCSSL = ADCSSL_HIGH_SPEED; // only read the pot, vbus and vpha
ADCON2 = ADCON2_HIGH_SPEED; // interrupt after three adc reads (adc interrupt frequency changes to 81.94 kHz)
ADCON1bits.ADON = 1; //Turn the ADC module back on
}
}
break;
case 4:
if (vphc_filtered_sample < signal_average)
{
SixtyDegreeTime[ZeroCrossState] = TMR1;
TMR1 = 0;
ThirtyDegreeTime = ThirtyDegreeTimeAverage();
if (ThirtyDegreeTime < phase_delay)
ThirtyDegreeTime = phase_delay;
PR3 = ThirtyDegreeTime - phase_delay;
NextSectorState = 1;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
OneEightyDegreeTime[SpeedPtr&0x000F] = ThirtyDegreeTime*6;
SpeedPtr++;
ZeroCrossState++;
}
break;
case 5:
if (vphb_filtered_sample > signal_average)
{
SixtyDegreeTime[ZeroCrossState] = TMR1;
TMR1 = 0;
ThirtyDegreeTime = ThirtyDegreeTimeAverage();
if (ThirtyDegreeTime < phase_delay)
ThirtyDegreeTime = phase_delay;
PR3 = ThirtyDegreeTime - phase_delay;
NextSectorState = 2;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
OneEightyDegreeTime[SpeedPtr&0x000F] = ThirtyDegreeTime*6;
SpeedPtr++;
ZeroCrossState = 0;
}
break;
// States 6 - 9 implement the high speed mode of this algorithm. Only one phase voltage is sampled. The sampling frequency
// is 81kHz when running in high speed mode.
case 6:
// Wait in this state until it's safe to check for the next zero cross event
// if (vpha_filtered_sample > (signal_average + 15))
// ZeroCrossState++;
if (Sector == 2)
ZeroCrossState++;
break;
case 7:
if (vpha_filtered_sample < signal_average) // signal is falling look for when it falls below center voltage
{
OneEightyDegreeTime[SpeedPtr&0x000F] = TMR1;
TMR1 = 0;
ThreeSixtyDegreeTime = OneEightyDegreeTime[SpeedPtr&0x000F] + OneEightyDegreeTime[(SpeedPtr-1)&0x000F];
PR2 = ThreeSixtyDegreeTime/6;
if ((ThreeSixtyDegreeTime>>2) < phase_delay)
PR3 = 0;
else
PR3 = (ThreeSixtyDegreeTime>>2) - phase_delay;
SpeedPtr++;
NextSectorState = 4;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
ZeroCrossState++;
}
break;
case 8:
// Wait in this state until it's safe to check for the next zero cross event
// if (vpha_filtered_sample < (signal_average - 15))
// ZeroCrossState++;
if (Sector == 5)
ZeroCrossState++;
break;
case 9:
if (vpha_filtered_sample > signal_average) // signal is falling look for when it falls below center voltage
{
OneEightyDegreeTime[SpeedPtr&0x000F] = TMR1;
TMR1 = 0;
ThreeSixtyDegreeTime = OneEightyDegreeTime[SpeedPtr&0x000F] + OneEightyDegreeTime[(SpeedPtr-1)&0x000F];
PR2 = ThreeSixtyDegreeTime/6;
if ((ThreeSixtyDegreeTime>>2) < phase_delay)
PR3 = 0;
else
PR3 = (ThreeSixtyDegreeTime>>2) - phase_delay;
SpeedPtr++;
NextSectorState = 1;
T3CONbits.TON = 1;
IEC0bits.T3IE = 1;
ZeroCrossState = 6;
if (!ControlFlags.HighSpeedMode)
{
ZeroCrossState = 4; // Go back to the appropriate state in low-speed mode
NextSectorState = 0; // The next timer 3 interrupt will initiate a commutation to Sector 0
PR3 = (PR2>>1) - phase_delay; // load PR2 with 30 degree time minus the phase delay
IEC0bits.T2IE = 0;
ADCON1bits.ADON = 0; // Turn ADC module off before modifying control bits;
ADCSSL = ADCSSL_LOW_SPEED; // read vphb and vphc in addition to the pot, vbus, and vpha
ADCON2 = ADCON2_LOW_SPEED; // interrupt every five samples (adc interrupts every 49kHz)
ADCON1bits.ADON = 1; // Turn ADC module back on
}
}
break;
default:
ZeroCrossState = 0;
break;
}
}
void Commutate(unsigned int sector)
{
OVDCON = SectortoState[sector]; // Change PWM phase
OldSector = sector; // Keep track of last sector (for error checking elsewhere)
//BlankingCount = 6;
}
unsigned int ThirtyDegreeTimeAverage()
{
unsigned int i;
unsigned long temp;
temp = 0;
for (i=0; i<6; i++)
{
temp += SixtyDegreeTime[i];
}
i = __builtin_divud(temp,12); // same as divide by 6 and then divide by 2 (returning the average 30 degree time
return i;
}