Definition in file mc_drv.c.
#include "config.h"
#include "mc_drv.h"
Go to the source code of this file.
Functions | |
void | ADCInit (void) |
__interrupt void | Commutate () |
Commutates and prepares for new zero-cross detection. | |
void | mc_init_HW (void) |
This function initializes the hardware/software resources required for motor driver. | |
__interrupt void | MotorPWMBottom () |
Timer/counter0 bottom overflow. Used for zero-cross detection. | |
void | PLLInit (void) |
void | PortsInit (void) |
void | PWMInit (void) |
void | Set_cpu_prescaler (U8 x) |
Set prescaler. | |
void | Timer0Init (void) |
Variables | |
ADMUXTableForward [6] | |
ADMUXTableReverse [6] | |
commTableForward [6] | |
commTableReverse [6] | |
volatile U8 | nextCommutationStep |
The commutation step that starts at next commutation. | |
volatile U8 | nextDrivePattern |
The power stage enable signals that will be output to the motor drivers at next commutation. | |
volatile U16 | speedReferenceADC |
ADC reading of external analog speed reference. | |
volatile U8 | speedUpdated = FALSE |
Flag that specifies whether a new external speed reference and a motor speed measurement is available. | |
volatile U8 | zcPolarity |
Polarity of the expected zero crossing. | |
zcTableForward [6] | |
zcTableReverse [6] |
void ADCInit | ( | void | ) |
Definition at line 210 of file mc_drv.c.
References ADC_PRESCALER_16, ADC_TRIGGER_SOURCE, ADMUX_REF_VOLTAGE, and speedReferenceADC.
Referenced by mc_init_HW().
00211 { 00212 ADMUX = ADMUX_REF_VOLTAGE ; 00213 00214 ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIF) | (ADC_PRESCALER_16); 00215 while (ADCSRA & (1 << ADSC)) 00216 { 00217 00218 } 00219 speedReferenceADC = ADC; 00220 00221 // Initialize the ADC for autotriggered operation on PWM timer overflow. 00222 ADCSRA = (1 << ADEN) | (0 << ADSC) | (1 << ADATE) | (1 << ADIF) | (0 << ADIE) | ADC_PRESCALER_16; 00223 ADCSRB = ADC_TRIGGER_SOURCE; 00224 }
__interrupt void Commutate | ( | ) |
Commutates and prepares for new zero-cross detection.
This interrupt service routine is triggered exactly when a commutation is scheduled. The commutation is performed instantly and Timer/counter0 is reset to measure the delay between commutation and zero-cross detection.
Commutation causes large transients on all phases for a short while that could cause false zero-cross detections. A zero cross detection hold-off period is therefore used to avoid any false readings. This is performed by using Timer/counter1 compare B. The compare is set to happen after the specified hold-off period. Timer/counter1 compare B interrupt handler then enables the zero-cross detection.
Definition at line 357 of file mc_drv.c.
References ADMUXTableForward, ADMUXTableReverse, CLEAR_ALL_TIMER0_INT_FLAGS, commTableForward, commTableReverse, DISABLE_ALL_TIMER0_INTS, nextCommutationStep, SET_TIMER1_INT_ZC_DETECTION, TC0_WRITE_TCNT0, zcPolarity, zcTableForward, and zcTableReverse.
00359 { 00360 CLEAR_ALL_TIMER0_INT_FLAGS; 00361 00362 #if (DIRECTION_OF_ROTATION == CCW) 00363 // Commutate and clear commutation timer. 00364 TCCR1E = commTableReverse[nextCommutationStep]; 00365 #else 00366 // Commutate and clear commutation timer. 00367 TCCR1E = commTableForward[nextCommutationStep]; 00368 #endif 00369 00370 00371 TC0_WRITE_TCNT0(0); 00372 00373 #if (DIRECTION_OF_ROTATION == CCW) 00374 zcPolarity = zcTableReverse[nextCommutationStep]; 00375 ADMUX = ADMUXTableReverse[nextCommutationStep]; 00376 #else 00377 zcPolarity = zcTableForward[nextCommutationStep]; 00378 ADMUX = ADMUXTableForward[nextCommutationStep]; 00379 #endif 00380 00381 nextCommutationStep ++; 00382 00383 if (nextCommutationStep > 5) 00384 { 00385 nextCommutationStep = 0; 00386 } 00387 00388 DISABLE_ALL_TIMER0_INTS; 00389 SET_TIMER1_INT_ZC_DETECTION; 00390 00391 }
void mc_init_HW | ( | void | ) |
This function initializes the hardware/software resources required for motor driver.
Definition at line 238 of file mc_drv.c.
References ADCInit(), Clear_prescaler, PLLInit(), PortsInit(), PWMInit(), and Timer0Init().
Referenced by main().
00239 { 00240 Clear_prescaler(); 00241 PLLInit(); 00242 PWMInit(); 00243 ADCInit(); 00244 00245 // Initialize timer0 for commutation timing 00246 Timer0Init(); 00247 00248 // Initialize alternate function 00249 PortsInit(); 00250 00251 }
__interrupt void MotorPWMBottom | ( | ) |
Timer/counter0 bottom overflow. Used for zero-cross detection.
This interrupt service routine is called every time the up/down counting PWM counter reaches bottom. An ADC reading on the active channel is automatically triggered at the same time as this interrupt is triggered. This is used to detect a zero crossing.
In the event of a zero crossing, the time since last commutation is stored and Timer/counter1 compare A is set up to trigger at the next commutation instant.
Definition at line 280 of file mc_drv.c.
References ADC_PRESCALER, ADC_PRESCALER_16, ADC_ZC_THRESHOLD, ADMUX_SPEED_REF, CLEAR_ALL_TIMER0_INT_FLAGS, CLEAR_ALL_TIMER1_INT_FLAGS, DISABLE_ALL_TIMER1_INTS, EDGE_FALLING, EDGE_RISING, SET_TIMER0_INT_COMMUTATION, speedReferenceADC, speedUpdated, TC0_READ_TCNT0, TC0_WRITE_TCNT0, TRUE, and zcPolarity.
00282 { 00283 00284 U16 temp; 00285 00286 CLEAR_ALL_TIMER1_INT_FLAGS; 00287 00288 00289 ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIF) | (ADC_PRESCALER_16); 00290 while (ADCSRA & (1 << ADSC)) 00291 { 00292 } 00293 temp= ADC; 00294 00295 if (((zcPolarity == EDGE_RISING) && (temp > ADC_ZC_THRESHOLD)) || ((zcPolarity == EDGE_FALLING) && (temp < ADC_ZC_THRESHOLD))) 00296 { 00297 U16 timeSinceCommutation; 00298 00299 // Find time since last commutation 00300 TC0_READ_TCNT0(timeSinceCommutation); 00301 00302 // Reset Timer before setting the next Commutation Period 00303 TC0_WRITE_TCNT0(0); 00304 00305 OCR0A = timeSinceCommutation; 00306 00307 speedUpdated = TRUE; 00308 00309 SET_TIMER0_INT_COMMUTATION; 00310 CLEAR_ALL_TIMER0_INT_FLAGS; 00311 00312 // Disable Timer/Counter1 overflow ISR. 00313 DISABLE_ALL_TIMER1_INTS; 00314 00315 // Read speed reference. 00316 00317 // Make sure that a sample is not in progress. 00318 while (ADCSRA & (1 << ADSC)) 00319 { 00320 00321 } 00322 // Change channel 00323 ADMUX = ADMUX_SPEED_REF; 00324 00325 // Start conversion manually. 00326 ADCSRA |= (1 << ADSC); 00327 00328 // Wait for conversion to complete. 00329 while((ADCSRA & (1 << ADSC))) 00330 { 00331 00332 } 00333 speedReferenceADC = ADC; 00334 00335 ADCSRA |= (1 << ADATE) | (0 << ADIE) | ADC_PRESCALER; 00336 } 00337 00338 }
void PLLInit | ( | void | ) |
Definition at line 185 of file mc_drv.c.
Referenced by mc_init_HW().
00186 { 00187 //Enable fast peripheral clock (64MHz for Timer1). 00188 PLLCSR = (1 << PCKE); 00189 }
void PortsInit | ( | void | ) |
Definition at line 227 of file mc_drv.c.
Referenced by mc_init_HW().
00228 { 00229 //Set PWM pins as output. (PWM output is still controlled through TCCR1E register.) 00230 DDRB = (1 << PB0) | (1 << PB1) | (1 << PB2) | (1 << PB3) | (1 << PB4) | (1 << PB5)| (1 << PB6) ; 00231 00232 // Disable digital input buffers on ADC channels. 00233 DIDR0 = (1 << ADC1D) | (1 << ADC2D) | (1 << ADC3D) | (1 << ADC4D) ; 00234 }
void PWMInit | ( | void | ) |
Definition at line 192 of file mc_drv.c.
References PWM_TOP_VALUE, and TC1_WRITE_10_BIT_REGISTER.
Referenced by mc_init_HW().
00193 { 00194 00195 //Clear on up-counting. 00196 TCCR1A = (1 << COM1A1) | (0 << COM1A0) | (1 << PWM1A); 00197 00198 //Set WGM to PWM6, dual slope mode. 00199 TCCR1D = (1 << WGM11) | (1 << WGM10); 00200 00201 //Set top value. 00202 TC1_WRITE_10_BIT_REGISTER(OCR1C, PWM_TOP_VALUE); 00203 00204 //Run timer at full speed. 00205 TCCR1B = (1 << CS10); 00206 00207 }
void Set_cpu_prescaler | ( | U8 | x | ) |
void Timer0Init | ( | void | ) |
Definition at line 179 of file mc_drv.c.
Referenced by mc_init_HW().
00180 { 00181 // Set up Timer/counter0 for commutation timing, prescaler = 8. 00182 TCCR0B = ((0<<CS02)|(1<<CS01) |(0<<CS00)); 00183 }
volatile U8 nextCommutationStep |
The commutation step that starts at next commutation.
The commutation step that starts at next commutation. This is used to keep track on where in the commutation cycle we are. Stored in register R11 for quick access
Definition at line 171 of file mc_drv.c.
Referenced by Commutate(), and mc_start_motor().
volatile U8 nextDrivePattern |
volatile U16 speedReferenceADC |
ADC reading of external analog speed reference.
Definition at line 174 of file mc_drv.c.
Referenced by ADCInit(), mc_regulation_loop(), and MotorPWMBottom().
volatile U8 speedUpdated = FALSE |
Flag that specifies whether a new external speed reference and a motor speed measurement is available.
Definition at line 177 of file mc_drv.c.
Referenced by mc_regulation_loop(), and MotorPWMBottom().
volatile U8 zcPolarity |
Polarity of the expected zero crossing.
The polarity of the expected zero crossing. Could be eiter EDGE_FALLING or EDGE_RISING.
Definition at line 163 of file mc_drv.c.
Referenced by Commutate(), mc_start_motor(), and MotorPWMBottom().
Initial value:
{ EDGE_RISING, EDGE_FALLING, EDGE_RISING, EDGE_FALLING, EDGE_RISING, EDGE_FALLING }
Initial value:
{ EDGE_FALLING, EDGE_RISING, EDGE_FALLING, EDGE_RISING, EDGE_FALLING, EDGE_RISING }