main.c File Reference

#include "config.h"
#include "usi_twi_slave.h"
#include "bldc.h"
#include "TinyX61_macros.h"
#include "ushell_task.h"

Include dependency graph for main.c:

Go to the source code of this file.

Functions

void ADCInit (void)
__interrupt void ADCISR ()
void Commutate (void)
__interrupt void HallChangeISR ()
void HallSensorInit (void)
void main (void)
void MotorSetDutyCycle (U16 duty)
__interrupt void ovfl_timer0 (void)
 Timer0 Overflow for speed measurement.
void PLLInit (void)
void PortsInit (void)
void PWMInit (void)
void Set_cpu_prescaler (U8 x)
 Set_cpu_prescaler.
void SpeedMeasurement (void)
void Timer0Init (void)
void TWInit (void)

Variables

volatile U8 ControlMode
U16 current
unsigned char data_received
unsigned char data_to_send
U8 direction
volatile uint8_t hall
Bool is_data_received = 0
Bool is_data_to_send = 0
volatile Bool is_speed_to_read = FALSE
volatile motorControlFlags_t mcFastFlags
volatile U16 new_measured_speed
U16 speed
volatile U16 SpeedMeasure
volatile U16 timer_value


Function Documentation

void ADCInit ( void   ) 

Definition at line 229 of file main.c.

References ADC_PRESCALER, ADC_TS_FREERUNNING, and ADMUX_PA2.

Referenced by main().

00230 {
00231   ADMUX = (0 << REFS1) | (0 << REFS0) | //VCC as voltage reference.
00232           (0 << ADLAR) |                //Right adjusted result.
00233           ADMUX_PA2;                    //ADC channel. MC301 rev B
00234 
00235   ADCSRB = (1 << REFS2) |               //Last reference selection bit.
00236            ADC_TS_FREERUNNING;          //Free running mode.
00237 
00238   ADCSRA = (1 << ADEN) |                //Enable.
00239            (1 << ADSC) |                //Start first conversion.
00240            (1 << ADATE) |               //Auto triggering.
00241            (1 << ADIE) |                //Enable interrupt.
00242            ADC_PRESCALER;
00243 }

__interrupt void ADCISR (  ) 

Definition at line 305 of file main.c.

References ADC_MAX, ADC_TWI, ControlMode, motorControlFlags_t::direction, DIRECTION_FORWARD, DIRECTION_REVERSE, MOTOR_CONTROL_CENTER, MotorSetDutyCycle(), POTENTIOMETER, and TWInit().

00307 {
00308   U16 speedSetpoint;
00309 
00310   //Read ADC sample.
00311   speedSetpoint = ADC;
00312 
00313   if(speedSetpoint>=ADC_TWI)
00314   {
00315     ControlMode=MOTOR_CONTROL_CENTER;
00316 
00317     //  Clear ADC enable
00318     ADCSRA &= ~(1 << ADEN);
00319     //Disable interrupt.
00320     ADCSRA &= ~(1 << ADIE);              
00321 
00322     TWInit();
00323   }
00324   else
00325   {
00326     ControlMode=POTENTIOMETER;
00327     //Extract sign.
00328     if (speedSetpoint >= ((ADC_MAX + 1) / 2)) {
00329       mcFastFlags.direction = DIRECTION_FORWARD;
00330       speedSetpoint -= ((ADC_MAX + 1) / 2);
00331     }
00332     else {
00333       mcFastFlags.direction = DIRECTION_REVERSE;
00334       speedSetpoint = ((ADC_MAX) / 2) - speedSetpoint;
00335     }
00336 
00337     //Multiply speed setpoint by 2 to get full PWM range.
00338     speedSetpoint *= 2;
00339 
00340     MotorSetDutyCycle(speedSetpoint);
00341   }
00342 }

Here is the call graph for this function:

void Commutate ( void   ) 

Definition at line 160 of file main.c.

References commTableForward, commTableReverse, motorControlFlags_t::direction, DIRECTION_FORWARD, and hall.

Referenced by HallChangeISR(), and main().

00161 {
00162    uint8_t temp;
00163    //Read hall sensor inputs.
00164     temp = ( PINA & ((1 << PA5) | (1 << PA4) | (1 << PA1)) );
00165     hall = (((temp&0x30)>>3) | (temp&0x02)>>1);
00166 
00167     
00168   //Activate output pattern corresponding to current direction and position.
00169   if (mcFastFlags.direction == DIRECTION_FORWARD) {
00170     TCCR1E = commTableForward[hall];
00171   }
00172   else {
00173     TCCR1E = commTableReverse[hall];
00174   }
00175     
00176 }

__interrupt void HallChangeISR (  ) 

Definition at line 269 of file main.c.

References Commutate(), FALSE, is_speed_to_read, TC0_READ_TCNT0, TC0_WRITE_TCNT0, timer_value, and TRUE.

00271 {
00272    
00273   Commutate();
00274 
00275   //estimation speed on rising edge of Hall 1 (U')
00276   if (PINA&(1<<PORTA1))
00277   {
00278     if(is_speed_to_read==FALSE)
00279     {
00280       // Stop Timer
00281       TCCR0B &= ~((1<<CS02)|(1<<CS01) |(1<<CS00)); 
00282   
00283       // Read timer0 value
00284       TC0_READ_TCNT0(timer_value);
00285     }
00286     
00287     // Restart timer0
00288     TCCR0B = ((1<<CS02)|(0<<CS01) |(1<<CS00)); // Timer clock =
00289                                   // system clock /
00290                                   // 128 : 16µs
00291     TC0_WRITE_TCNT0(0x0);
00292         
00293     is_speed_to_read=FALSE;  // Wait for a period
00294   }
00295   else
00296   {
00297     is_speed_to_read=TRUE;
00298   }
00299 }

Here is the call graph for this function:

void HallSensorInit ( void   ) 

Definition at line 216 of file main.c.

Referenced by main().

00217 {
00218   //Enable pin change interrupts for PA1-PA4-PA5 (PCINT1-4-5).
00219   PCMSK0 = (1 << PCINT1) | (1 << PCINT4) | (1 << PCINT5);
00220 
00221   //Zero out PCMSK1, since it is initialized to a non-zero value.
00222   PCMSK1 = 1;
00223 
00224   //Enable pin change interrupt 1.
00225   GIMSK |= (1 << PCIE1);
00226 }

void main ( void   ) 

Definition at line 94 of file main.c.

References ADCInit(), Clear_prescaler, Commutate(), ControlMode, data_received, FALSE, HallSensorInit(), is_data_received, is_data_to_send, is_speed_to_read, MOTOR_CONTROL_CENTER, MotorSetDutyCycle(), PLLInit(), PortsInit(), PWMInit(), motorControlFlags_t::speed, SpeedMeasure, SpeedMeasurement(), motorControlFlags_t::state, STATE_RUN, Timer0Init(), TRUE, ushell_task(), ushell_task_init(), USI_TWI_Data_In_Receive_Buffer(), and USI_TWI_Receive_Byte().

00095 { 
00096   Clear_prescaler();
00097   PLLInit();
00098   PWMInit();
00099   HallSensorInit();
00100 
00101   ADCInit();
00102 
00103   // Init USI Handshake variables
00104   is_data_to_send=FALSE;
00105   is_data_received=FALSE;
00106 
00107   SpeedMeasure=0;
00108   is_speed_to_read=FALSE;
00109   
00110   // Initialize timer0 for Speed Measurement
00111   Timer0Init();
00112   
00113   PortsInit();
00114   
00115   // uShell init
00116   ushell_task_init();
00117   
00118   //Run Commutate() once to make sure that the right coils are activated.
00119   Commutate();
00120 
00121   //Enable interrupts. The rest will now be handled by the ADC and Pin change
00122   //interrupts.
00123   Enable_interrupt();
00124   
00125   for (;;) {
00126     if(ControlMode==MOTOR_CONTROL_CENTER)
00127     {
00128       if (mcFastFlags.state == STATE_RUN)
00129       {
00130         // Update Motor speed
00131         MotorSetDutyCycle(mcFastFlags.speed);
00132         // Start Speed measurement
00133         if(is_speed_to_read==TRUE)
00134           SpeedMeasurement();   
00135       }
00136       else
00137       {
00138         //Stop Motor
00139         MotorSetDutyCycle(0);
00140         mcFastFlags.speed=0;
00141       }
00142       
00143       // Decodes new Motor Control Center command
00144       ushell_task();
00145    
00146       // Manages new low level TWI data received
00147       if(is_data_received==FALSE)
00148       {
00149         if (USI_TWI_Data_In_Receive_Buffer())    //Something new of the USI ?
00150         {
00151           data_received=USI_TWI_Receive_Byte();
00152           is_data_received=TRUE;
00153         }
00154       }
00155     }
00156   }
00157 }

Here is the call graph for this function:

void MotorSetDutyCycle ( U16  duty  ) 

Definition at line 260 of file main.c.

References TC1_WRITE_10_BIT_REGISTER.

Referenced by ADCISR(), and main().

00261 {
00262 TC1_WRITE_10_BIT_REGISTER(OCR1A, duty);
00263 }

__interrupt void ovfl_timer0 ( void   ) 

Timer0 Overflow for speed measurement.

Precondition:
configuration of timer 0
Postcondition:
generate an overflow when the motor turns too slowly

Definition at line 366 of file main.c.

References TC0_WRITE_TCNT0.

00368 {
00369   TC0_WRITE_TCNT0(0x0);
00370   TIFR |= (1<<TOV0); //Clear TOV0 / clear
00371   //pending interrupts
00372   TIMSK |= (1<<TOIE0); //Enable Timer0
00373 }

void PLLInit ( void   ) 

Definition at line 192 of file main.c.

Referenced by main().

00193 {
00194   //Enable fast peripheral clock (64MHz for Timer1).
00195   PLLCSR = (1 << PCKE);
00196 }

void PortsInit ( void   ) 

Definition at line 246 of file main.c.

Referenced by main().

00247 {
00248   //Set PWM pins as output. (PWM output is still controlled through TCCR1E register.)
00249   DDRB = (1 << PB0) | (1 << PB1) | (1 << PB2) | (1 << PB3) | (1 << PB4) | (1 << PB5)| (1 << PB6) ;
00250 }

void PWMInit ( void   ) 

Definition at line 199 of file main.c.

References PWM_TOP_VALUE, and TC1_WRITE_10_BIT_REGISTER.

Referenced by main().

00200 {
00201  
00202   //Clear on up-counting.
00203   TCCR1A = (1 << COM1A1) | (0 << COM1A0) | (1 << PWM1A);
00204 
00205   //Set WGM to PWM6, dual slope mode.
00206   TCCR1D = (1 << WGM11) | (1 << WGM10);
00207 
00208   //Set top value.
00209   TC1_WRITE_10_BIT_REGISTER(OCR1C, PWM_TOP_VALUE);
00210 
00211   //Run timer at full speed.
00212   TCCR1B = (1 << CS10);
00213 }

void Set_cpu_prescaler ( U8  x  ) 

Set_cpu_prescaler.

Set_prescaler.

This function write the CPU prescaler register to a define value

Parameters:
U8 the precaler value to be written
Returns:
none.

Definition at line 84 of file main.c.

00085    {
00086       U8 save_int=SREG&0x80;
00087       Disable_interrupt();
00088       CLKPR=(1<<CLKPCE);
00089       CLKPR=x;
00090       if(save_int) { Enable_interrupt(); }
00091    }

void SpeedMeasurement ( void   ) 

Definition at line 179 of file main.c.

References FALSE, and is_speed_to_read.

Referenced by main().

00180 {
00181 //   if (timer_value == 0) 
00182   //  {timer_value += 1 ;} // warning DIV by 0
00183 /*
00184     new_measured_speed = K_SPEED / timer_value;
00185     if(new_measured_speed > 255) new_measured_speed = 255; // Variable saturation
00186     SpeedMeasure=(U16)new_measured_speed;
00187 */    
00188     is_speed_to_read=FALSE;
00189   
00190 }

void Timer0Init ( void   ) 

Definition at line 344 of file main.c.

References TC0_WRITE_TCNT0.

Referenced by main().

00345 {
00346   TCCR0B = ((1<<CS02)|(0<<CS01) |(1<<CS00)); // Timer clock =
00347                                 // system clock /
00348                                 // 128 : 16µs
00349   TIFR |= (1<<TOV0); //Clear TOV0 / clear
00350 
00351   TC0_WRITE_TCNT0(0x0);
00352   //pending interrupts
00353   TIMSK |= (1<<TOIE0); //Enable Timer0
00354   //Overflow Interrupt
00355 }

void TWInit ( void   ) 

Definition at line 252 of file main.c.

References TWI_SLAVE_ADDRESS, and USI_TWI_Slave_Initialise().

Referenced by ADCISR().

00253 {
00254   // Select the Port A as USI port
00255   USIPP|=(1<<USIPOS);
00256   // Own TWI slave address
00257   USI_TWI_Slave_Initialise( TWI_SLAVE_ADDRESS ); 
00258 }

Here is the call graph for this function:


Variable Documentation

volatile U8 ControlMode

Definition at line 56 of file main.c.

Referenced by ADCISR(), and main().

Definition at line 69 of file main.c.

Referenced by ushell_task().

unsigned char data_received

Definition at line 66 of file main.c.

Referenced by main(), and ushell_task().

unsigned char data_to_send

Definition at line 65 of file main.c.

Definition at line 70 of file main.c.

volatile uint8_t hall

Definition at line 72 of file main.c.

Referenced by Commutate().

Definition at line 64 of file main.c.

Referenced by main(), and ushell_task().

Definition at line 63 of file main.c.

Referenced by main().

volatile Bool is_speed_to_read = FALSE

Definition at line 60 of file main.c.

Referenced by HallChangeISR(), main(), and SpeedMeasurement().

Definition at line 55 of file main.c.

Definition at line 59 of file main.c.

Definition at line 68 of file main.c.

Referenced by ushell_task().

volatile U16 SpeedMeasure

Definition at line 57 of file main.c.

Referenced by main().

volatile U16 timer_value

Definition at line 58 of file main.c.

Referenced by HallChangeISR().


Generated on Wed Oct 22 16:03:25 2008 for AVR496 : Atmel BLDC control on ATAVRMC301 with ATtiny861 by  doxygen 1.5.7.1