00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "mc_control.h"
00044 #include "mc_drv.h"
00045
00046
00047
00048
00049
00050
00051
00052 #ifdef __ICCAVR__
00053 extern code U8
00054 #elif __GNUC__
00055 extern U8
00056 #endif
00057
00058 ADMUXTableForward[6];
00059
00060
00061 #ifdef __ICCAVR__
00062 extern code U8
00063 #elif __GNUC__
00064 extern U8
00065 #endif
00066
00067 ADMUXTableReverse[6];
00068
00069
00070 #ifdef __ICCAVR__
00071 extern code U8
00072 #elif __GNUC__
00073 extern U8
00074 #endif
00075
00076 commTableForward[6];
00077
00078
00079
00080 #ifdef __ICCAVR__
00081 extern code U8
00082 #elif __GNUC__
00083 extern U8
00084 #endif
00085
00086 commTableReverse[6];
00087
00088
00089 #ifdef __ICCAVR__
00090 extern code U8
00091 #elif __GNUC__
00092 extern U8
00093 #endif
00094
00095 zcTableForward[6];
00096
00097
00098
00099 #ifdef __ICCAVR__
00100 extern code U8
00101 #elif __GNUC__
00102 extern U8
00103 #endif
00104
00105 zcTableReverse[6];
00106
00107 extern volatile U8 nextCommutationStep;
00108 extern volatile U16 speedReferenceADC;
00109 extern volatile U8 speedUpdated;
00110 extern volatile U8 zcPolarity;
00111
00112
00114 #define STARTUP_NUM_COMMUTATIONS 29
00115
00116 #ifdef __ICCAVR__
00117 code U32
00118 #elif __GNUC__
00119 U32
00120 #endif
00121 start_delay[STARTUP_NUM_COMMUTATIONS+1] =
00122 {2000,1900,1800,1600,1400,1200,1000,800,600,550,500,480,460,440,420,400,380,360,355,350,345,340,335,330,325,320,315,310,305,300};
00123
00126 void delay_us(volatile long delay)
00127 {
00128 for (volatile long i=0;i<delay;i++);
00129 }
00130
00133 void mc_set_duty_cycle(U16 duty)
00134 {
00135 TC1_WRITE_10_BIT_REGISTER(OCR1A, duty);
00136 }
00137
00143 void mc_start_motor()
00144 {
00145 U8 i = 0;
00146 U8 j = 0;
00147 U8 end_of_start = 0;
00148 mc_set_duty_cycle(STARTUP_PWM_COMPARE_VALUE) ;
00149
00150 nextCommutationStep = 0;
00151
00152 while(end_of_start!=1)
00153 {
00154 switch(nextCommutationStep)
00155 {
00156 case 0:
00157 #if (DIRECTION_OF_ROTATION == CCW)
00158 TCCR1E = commTableReverse[0];
00159 #else
00160 TCCR1E = commTableForward[0];
00161 #endif
00162 if (i < 2) i += 1;
00163 if ((i == 2)&&(j<STARTUP_NUM_COMMUTATIONS)) {
00164 j += 1;i=0;
00165 }
00166 if ((i==2)&&(j==STARTUP_NUM_COMMUTATIONS)){
00167 end_of_start = 1;
00168 }
00169 delay_us(start_delay[j]);
00170 nextCommutationStep = 1;
00171 break;
00172 case 1:
00173 #if (DIRECTION_OF_ROTATION == CCW)
00174 TCCR1E = commTableReverse[1];
00175 #else
00176 TCCR1E = commTableForward[1];
00177 #endif
00178 delay_us(start_delay[j]);
00179 nextCommutationStep = 2;
00180 break;
00181 case 2:
00182 #if (DIRECTION_OF_ROTATION == CCW)
00183 TCCR1E = commTableReverse[2];
00184 #else
00185 TCCR1E = commTableForward[2];
00186 #endif
00187 delay_us(start_delay[j]);
00188 nextCommutationStep = 3;
00189 break;
00190 case 3:
00191 #if (DIRECTION_OF_ROTATION == CCW)
00192 TCCR1E = commTableReverse[3];
00193 #else
00194 TCCR1E = commTableForward[3];
00195 #endif
00196 delay_us(start_delay[j]);
00197 nextCommutationStep = 4;
00198 break;
00199 case 4:
00200 #if (DIRECTION_OF_ROTATION == CCW)
00201 TCCR1E = commTableReverse[4];
00202 #else
00203 TCCR1E = commTableForward[4];
00204 #endif
00205 delay_us(start_delay[j]);
00206 nextCommutationStep = 5;
00207 break;
00208 case 5:
00209 #if (DIRECTION_OF_ROTATION == CCW)
00210 TCCR1E = commTableReverse[5];
00211 #else
00212 TCCR1E = commTableForward[5];
00213 #endif
00214 delay_us(start_delay[j]);
00215 nextCommutationStep = 0;
00216 break;
00217 default :
00218 nextCommutationStep = nextCommutationStep;
00219 break;
00220 }
00221 }
00222
00223
00224 nextCommutationStep = 0;
00225
00226 #if (DIRECTION_OF_ROTATION == CCW)
00227 zcPolarity = zcTableReverse[nextCommutationStep];
00228 ADMUX = ADMUXTableReverse[nextCommutationStep];
00229 #else
00230 zcPolarity = zcTableForward[nextCommutationStep];
00231 ADMUX = ADMUXTableForward[nextCommutationStep];
00232 #endif
00233
00234 TC0_WRITE_TCNT0(0);
00235
00236
00237 SET_TIMER1_INT_ZC_DETECTION;
00238
00239 }
00240
00243 void mc_regulation_loop()
00244 {
00245
00246 if (speedUpdated)
00247 {
00248 speedUpdated = FALSE;
00249
00250 mc_set_duty_cycle(MIN_PWM_COMPARE_VALUE+(speedReferenceADC>>3));
00251 }
00252 }
00253
00254