00001 /* This file has been prepared for Doxygen automatic documentation generation.*/ 00027 #ifndef _PMSM_H_ 00028 #define _PMSM_H_ 00029 00030 00031 #include "stdint.h" 00032 00033 00038 typedef struct PMSMflags 00039 { 00040 uint8_t motorStopped : 1; 00041 uint8_t motorSynchronized: 1; 00042 uint8_t actualDirection : 2; 00043 uint8_t desiredDirection : 1; 00044 uint8_t driveWaveform : 2; 00045 } PMSMflags_t; 00046 00047 00049 #define FALSE 0 00050 00052 #define TRUE (!FALSE) 00053 00054 00056 #define CPU_FREQUENCY 16000000UL 00057 00059 #define DEAD_TIME 10 00060 00061 00062 #ifndef PWM1X 00063 00068 #define PWM1X 7 00069 #endif 00070 00071 00073 #define UL PB0 00074 00076 #define UH PB1 00077 00079 #define VL PB2 00080 00082 #define VH PB3 00083 00085 #define WL PB4 00086 00088 #define WH PB5 00089 00091 #define PWM_PIN_MASK_PORTB ((1 << UL) | (1 << UH) | (1 << VL) | (1 << VH) | (1 << WL) | (1 << WH)) 00092 00093 00095 #define H1_PIN PA0 00096 00098 #define H2_PIN PA1 00099 00101 #define H3_PIN PA2 00102 00104 #define HALL_PULL_UP_ENABLE FALSE 00105 //#define HALL_PULL_UP_ENABLE TRUE 00106 00107 00109 #define DIRECTION_PIN PA3 00110 00111 00112 //Output compare override enable bits 00113 #define UL_OCOE OC1OE0 00114 #define UH_OCOE OC1OE1 00115 #define VL_OCOE OC1OE2 00116 #define VH_OCOE OC1OE3 00117 #define WL_OCOE OC1OE4 00118 #define WH_OCOE OC1OE5 00119 00120 00122 #define COMPARE_REGISTER_PHASE_U OCR1A 00123 00125 #define COMPARE_REGISTER_PHASE_V OCR1B 00126 00128 #define COMPARE_REGISTER_PHASE_W OCR1D 00129 00130 00136 #define PWM_INVERT_OUTPUT 0 00137 //#define PWM_INVERT_OUTPUT 1 00138 00139 00141 #define DIRECTION_FORWARD 0 00142 00144 #define DIRECTION_REVERSE 1 00145 00147 #define DIRECTION_UNKNOWN 3 00148 00149 00156 #define SINE_TABLE_LENGTH 192U 00157 00159 #define SINE_TABLE_SIZE_LARGE 0 00160 00162 #define SINE_TABLE_SIZE_SMALL 1 00163 00168 #define SINE_TABLE_SIZE SINE_TABLE_SIZE_LARGE 00169 //#define SINE_TABLE_SIZE SINE_TABLE_SIZE_SMALL 00170 00171 00173 #define TABLE_ELEMENTS_PER_COMMUTATION_SECTOR (SINE_TABLE_LENGTH / 6) 00174 00175 00177 #define PWM_TOP_VALUE 0x03ff 00178 00179 00185 #define COMMUTATION_TICKS_STOPPED 6000 00186 00187 00193 #define SYNCHRONIZATION_COUNT 2 00194 00195 00197 #define WAVEFORM_BLOCK_COMMUTATION 0 00198 00200 #define WAVEFORM_SINUSOIDAL 1 00201 00203 #define WAVEFORM_BRAKING 2 00204 00206 #define WAVEFORM_UNDEFINED 3 00207 00208 00210 #define BLOCK_COMMUTATION_DUTY_MULTIPLIER 3 00211 00212 00214 #define TURN_MODE_COAST 0 00215 00217 #define TURN_MODE_BRAKE 1 00218 00223 //#define TURN_MODE TURN_MODE_COAST 00224 #define TURN_MODE TURN_MODE_BRAKE 00225 00226 00228 #define SPEED_CONTROL_OPEN_LOOP 0 00229 00231 #define SPEED_CONTROL_CLOSED_LOOP 1 00232 00237 #define SPEED_CONTROL_METHOD SPEED_CONTROL_OPEN_LOOP 00238 //#define SPEED_CONTROL_METHOD SPEED_CONTROL_CLOSED_LOOP 00239 00243 #define SPEED_CONTROLLER_TIME_BASE 150 00244 00248 #define PID_K_P 256 00249 00253 #define PID_K_I 10 00254 00258 #define PID_K_D 0 00259 00265 #define SPEED_CONTROLLER_MAX_INCREMENT 620 00266 00272 #define SPEED_CONTROLLER_MAX_INPUT 1024 00273 00274 00275 static void SpeedController(uint16_t speedReference); 00276 static void PortsInit(void); 00277 static void PLLInit(void); 00278 static void TimersInit(void); 00279 static void PWMInit(void); 00280 static void ADCInit(void); 00281 static void PinChangeInit(void); 00282 static void ExternalInterruptInit(void); 00283 static void TimerSetModeBlockCommutation(void); 00284 static void TimerSetModeSinusoidal(void); 00285 static void TimerSetModeBrake(void); 00286 static void BlockCommutationSetDuty(const uint16_t compareValue); 00287 static uint8_t GetDesiredDirection(void); 00288 static uint8_t GetActualDirection(void); 00289 static void BlockCommutate(const uint8_t direction, const uint8_t hall); 00290 static uint8_t GetHall(void); 00291 static void DesiredDirectionUpdate(void); 00292 static void ActualDirectionUpdate(uint8_t lastHall, const uint8_t newHall); 00293 static uint16_t SineTableIncrementCalculate(const uint16_t ticks); 00294 static void AdjustSineTableIndex(const uint16_t increment); 00295 static void SetAdvanceCommutation(const uint8_t advanceCommutation); 00296 static void EnablePWMOutputs(void); 00297 static void DisablePWMOutputs(void); 00298 static void CommutationTicksUpdate(void); 00299 static void MotorSynchronizedUpdate(void); 00300 static uint8_t IsMotorSynchronized(void); 00301 static uint8_t SineTableSmallGetValue(uint8_t index); 00302 static void SineOutputUpdate(void); 00303 static unsigned int MultiplyUS15x8(const uint16_t m15, const uint8_t m8); 00304 00305 00310 #define TC1_PWM6_SET_DUTY_CYCLE(dutyCycle) TC1_WRITE_10_BIT_REGISTER(OCR1A, (dutyCycle)) 00311 00312 00313 #endif 00314
1.5.2