1 | void svpwm(volatile svpwm_options_t *svpwm_options)
|
2 | {
|
3 | int tempsr0, tempsr2, tempsr4;
|
4 | int delta_i, delta_2i, delta_z;
|
5 | int Vb_M_Va, Vb_P_Va, Va_P_Vb, Va_M_Vb;
|
6 |
|
7 | //******************************************************************
|
8 | // Vbeta-sqrt(3)*Valpha
|
9 | Vb_M_Va = svpwm_options->Vbeta - (svpwm_options->Valpha <<1) + (svpwm_options->Valpha >>2) + (svpwm_options->Valpha >>6);
|
10 | // Vbeta+sqrt(3)*Valpha
|
11 | Vb_P_Va = (svpwm_options->Vbeta <<1) - Vb_M_Va;
|
12 | if (svpwm_options->Vbeta >0)
|
13 | {
|
14 | if (svpwm_options->Valpha >0)
|
15 | {
|
16 | if (Vb_M_Va <0) // SECTOR =i =1
|
17 | { // delta_2i= 2*sqrt(2)* Vbeta
|
18 | svpwm_options->current_to_be_measured=BC;
|
19 | delta_2i = (svpwm_options->Vbeta<<1) + (svpwm_options->Vbeta>>1) + (svpwm_options->Vbeta>>2) + (svpwm_options->Vbeta>>4)+ (svpwm_options->Vbeta>>6);
|
20 |
|
21 | // delta_i = 2*sqrt(1.5)*Valpha - 0.5*delta_2i
|
22 | delta_i = (svpwm_options->Valpha<<1) + (svpwm_options->Valpha>>1) - (svpwm_options->Valpha>>4) - (delta_2i>>1);
|
23 |
|
24 | // delta_z = 0.5*( 1 - delta_2i - delta_i )
|
25 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
26 |
|
27 | tempsr0 = delta_z + delta_2i + delta_i;
|
28 | tempsr2 = delta_2i + delta_z;
|
29 | tempsr4 = delta_z;
|
30 | }
|
31 |
|
32 | else // SECTOR = i = 2
|
33 | {
|
34 | svpwm_options->current_to_be_measured=AC;
|
35 | //Valpha-sqrt(3)*Vbeta
|
36 | Va_P_Vb = svpwm_options->Valpha + (svpwm_options->Vbeta<<1) - (svpwm_options->Vbeta>>2) - (svpwm_options->Vbeta>>6);
|
37 | // delta_2i = sqrt(2)* Vb_M_Va
|
38 | delta_2i = (Vb_M_Va<<1) - (Vb_M_Va>>1) -(Vb_M_Va>>4) -(Vb_M_Va>>5);
|
39 | // delta_i = sqrt(1.5)*(Valpha+sqrt(3)*Vbeta) - 0.5*delta_2i
|
40 | delta_i = Va_P_Vb + (Va_P_Vb>>2) - (Va_P_Vb>>5) - (delta_2i>>1);
|
41 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
42 | tempsr0 = delta_i + delta_z;
|
43 | tempsr2 = delta_i + delta_2i + delta_z;
|
44 | tempsr4 = delta_z;
|
45 | }
|
46 | }
|
47 | else
|
48 | if (Vb_P_Va <0) // SECTOR = i = 3
|
49 | {
|
50 | svpwm_options->current_to_be_measured=CA;
|
51 | // delta_2i = -sqrt(2)*Vb_P_Va
|
52 | delta_2i = -(Vb_P_Va<<1) + (Vb_P_Va>>1) + (Vb_P_Va>>4) + (Vb_P_Va>>5);
|
53 | // Valpha-sqrt(3)*Vbeta
|
54 | Va_M_Vb = svpwm_options->Valpha - (svpwm_options->Vbeta<<1) + (svpwm_options->Vbeta>>2) + (svpwm_options->Vbeta>>6);
|
55 | // delta_i = -sqrt(1.5)*(Va_M_Vb)- 0.5*delta_2i
|
56 | delta_i = - Va_M_Vb - (Va_M_Vb>>2) + (Va_M_Vb>>5) - (delta_2i>>1);
|
57 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
58 | tempsr0 = delta_z;
|
59 | tempsr2 = delta_i + delta_2i+ delta_z;
|
60 | tempsr4 = delta_2i + delta_z;
|
61 | }
|
62 | else // SECTOR = i = 2
|
63 | {
|
64 |
|
65 | svpwm_options->current_to_be_measured=AC;
|
66 | //Valpha+sqrt(3)*Vbeta
|
67 | Va_P_Vb = svpwm_options->Valpha + (svpwm_options->Vbeta<<1) - (svpwm_options->Vbeta>>2) - (svpwm_options->Vbeta>>6);
|
68 | // delta_2i = sqrt(2)* Vb_M_Va
|
69 | delta_2i = (Vb_M_Va<<1) - (Vb_M_Va>>1) -(Vb_M_Va>>4) -(Vb_M_Va>>5);
|
70 | // delta_i = sqrt(1.5)*(Valpha+sqrt(3)*Vbeta) - 0.5*delta_2i
|
71 | delta_i = Va_P_Vb + (Va_P_Vb>>2) - (Va_P_Vb>>5) - (delta_2i>>1);
|
72 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
73 | tempsr0 = delta_i + delta_z;
|
74 | tempsr2 = delta_i + delta_2i + delta_z;
|
75 | tempsr4 = delta_z;
|
76 | }
|
77 | }
|
78 | else
|
79 | {
|
80 | if (svpwm_options->Valpha >0)
|
81 | {
|
82 | if (Vb_P_Va <0)// SECTOR = i = 5
|
83 | {
|
84 | svpwm_options->current_to_be_measured=AB;
|
85 | // delta_2i = - sqrt(2)*( Vbeta - sqrt(3)*Valpha)
|
86 | delta_2i = -(Vb_M_Va<<1) + (Vb_M_Va>>1) + (Vb_M_Va>>4) + (Vb_M_Va>>5);
|
87 | //Valpha+sqrt(3)*Vbeta
|
88 | Va_P_Vb = svpwm_options->Valpha + (svpwm_options->Vbeta<<1) - (svpwm_options->Vbeta>>2) - (svpwm_options->Vbeta>>6);
|
89 | // delta_i = - sqrt(1.5)*(Valpha+sqrt(3)*Vbeta) - 0.5* delta_2i
|
90 | delta_i = - Va_P_Vb - (Va_P_Vb>>2) + (Va_P_Vb>>5) - (delta_2i>>1);
|
91 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
92 | tempsr0 = delta_2i + delta_z;
|
93 | tempsr2 = delta_z;
|
94 | tempsr4 = delta_z + delta_2i + delta_i;
|
95 | }
|
96 | else // SECTOR = i = 6
|
97 | {
|
98 | svpwm_options->current_to_be_measured=CB;
|
99 | // delta_2i = sqrt(2)*( Vbeta + sqrt(3)*Valpha)
|
100 | delta_2i = (Vb_P_Va<<1) - (Vb_P_Va>>1) - (Vb_P_Va>>4) - (Vb_P_Va>>5);
|
101 | // Valpha-sqrt(3)*Vbeta
|
102 | Va_M_Vb = svpwm_options->Valpha - (svpwm_options->Vbeta<<1) + (svpwm_options->Vbeta>>2) + (svpwm_options->Vbeta>>6);
|
103 | // delta_i = sqrt(1.5)*(Valpha-sqrt(3)*Vbeta) - 0.5* delta_2i
|
104 | delta_i = Va_M_Vb + (Va_M_Vb>>2) - (Va_M_Vb>>5) - (delta_2i>>1);
|
105 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
106 | tempsr0 = delta_2i + delta_z + delta_i;
|
107 | tempsr2 = delta_z;
|
108 | tempsr4 = delta_z + delta_i;
|
109 | }
|
110 | }
|
111 | else
|
112 | if (Vb_M_Va <0)// SECTOR = i = 5
|
113 | {
|
114 | svpwm_options->current_to_be_measured=AB;
|
115 | // delta_2i = - sqrt(2)*( Vbeta - sqrt(3)*Valpha)
|
116 | delta_2i = -(Vb_M_Va<<1) + (Vb_M_Va>>1) + (Vb_M_Va>>4) + (Vb_M_Va>>5);
|
117 | //Valpha+sqrt(3)*Vbeta
|
118 | Va_P_Vb = svpwm_options->Valpha + (svpwm_options->Vbeta<<1) - (svpwm_options->Vbeta>>2) - (svpwm_options->Vbeta>>6);
|
119 | // delta_i = - sqrt(1.5)*(Valpha+sqrt(3)*Vbeta) - 0.5* delta_2i
|
120 | delta_i = - Va_P_Vb - (Va_P_Vb>>2) + (Va_P_Vb>>5) - (delta_2i>>1);
|
121 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
122 | tempsr0 = delta_2i + delta_z;
|
123 | tempsr2 = delta_z;
|
124 | tempsr4 = delta_z + delta_2i + delta_i;
|
125 | }
|
126 | else // SECTOR = i = 4
|
127 | {
|
128 | svpwm_options->current_to_be_measured=BA;
|
129 | // delta_2i= -2*sqrt(2)* Vbeta
|
130 | delta_2i = -(svpwm_options->Vbeta<<1)- (svpwm_options->Vbeta>>1) - (svpwm_options->Vbeta>>2) - (svpwm_options->Vbeta>>4)- (svpwm_options->Vbeta>>6);
|
131 | // delta_i = -2*sqrt(1.5)*Valpha - 0.5*delta_2i
|
132 | delta_i = -(svpwm_options->Valpha<<1) - (svpwm_options->Valpha>>1) + (svpwm_options->Valpha>>4) - (delta_2i>>1);
|
133 | delta_z = (Un_1 - delta_2i - delta_i)>>1;
|
134 | tempsr0 = delta_z;
|
135 | tempsr2 = delta_z + delta_i;
|
136 | tempsr4 = delta_z + delta_2i + delta_i;
|
137 | }
|
138 | }
|
139 |
|
140 | svpwm_options->duty0 = (tempsr0>>20) - (tempsr0>>22) - (tempsr0>>23) - (tempsr0>>25) - (tempsr0>>26)-10;
|
141 | svpwm_options->duty1 = svpwm_options->duty0+20;
|
142 | svpwm_options->duty2 = (tempsr2>>20) - (tempsr2>>22) - (tempsr2>>23) - (tempsr2>>25) - (tempsr2>>26)-10;
|
143 | svpwm_options->duty3 = svpwm_options->duty2+20;
|
144 | svpwm_options->duty4 = (tempsr4>>20) - (tempsr4>>22) - (tempsr4>>23) - (tempsr4>>25) - (tempsr4>>26)-10;
|
145 | svpwm_options->duty5 = svpwm_options->duty4+20;
|
146 | }
|