mc_drv.c File Reference

#include "table_sin81.h"
#include "lib_mcu/pll/pll_drv.h"

Include dependency graph for mc_drv.c:

Go to the source code of this file.

Defines

#define HAS1   (1<<PIND5) | (0<<PINC6) | (1<<PIND7)
#define HAS2   (0<<PIND5) | (0<<PINC6) | (1<<PIND7)
#define HAS3   (0<<PIND5) | (1<<PINC6) | (1<<PIND7)
#define HAS4   (0<<PIND5) | (1<<PINC6) | (0<<PIND7)
#define HAS5   (1<<PIND5) | (1<<PINC6) | (0<<PIND7)
#define HAS6   (1<<PIND5) | (0<<PINC6) | (0<<PIND7)

Functions

void Do_Sensor_Interrupt (void)
void init (void)
U16 mc_control_speed_16b (U16 speed_ref, U16 speed_measure)
void PSC_Init (unsigned int ot0, unsigned int ot1)
void PSC_Load (unsigned int dt0a, unsigned int dt1a, unsigned int dt0b, unsigned int dt1b, unsigned int dt0c, unsigned int dt1c)
void PSC_Start (void)
void PSC_Stop (void)
S16 read_acquisition ()
void Start_ADC (void)
void SVPWM (U16 amp, U8 Counter_Value, U8 Top, U8 Sector)

Variables

volatile U16 Amplitude = 0
 voltage to apply
volatile U8 Delay_For_Change = 1
 delay
volatile U8 Hall_Event_Count = 8
 measure the speed on 8 cycles
volatile U16 Hall_Period = 65535
 period of Hall sensor signal
volatile U8 Hall_Sector = HAS1
 current Hall sector
volatile U8 Main_Tick
 Tick signal for main.
Bool mci_direction
 User Input parameter to set motor direction.
volatile Bool mci_run_stop
 command for motor
volatile U16 Measured_Speed
 measured speed
volatile U8 Mem_Hall_Sector = HAS1
 future Hall sector
volatile U16 Mem_Hall_Time = 0
 memorisation of Hall event time
volatile U8 Mem_Top_Counter = 1
 memorization at top
volatile U16 Mem_Top_CounterX8 = 1
 for filter
volatile U16 Permanent_Clock = 0
volatile U8 Prescaler_Main_Tick
 prescaler for Tick
volatile U16 PWM0
volatile U16 PWM1
volatile U16 PWM2
 duty cycle
volatile U8 Sensor_Counter_Down = 1
 current counter for svpwm
volatile U8 Sensor_Counter_Up = 0
 measure of the time
volatile U8 Top_Counter = 1
 current top counter


Define Documentation

#define HAS1   (1<<PIND5) | (0<<PINC6) | (1<<PIND7)

Definition at line 46 of file mc_drv.c.

#define HAS2   (0<<PIND5) | (0<<PINC6) | (1<<PIND7)

Definition at line 47 of file mc_drv.c.

#define HAS3   (0<<PIND5) | (1<<PINC6) | (1<<PIND7)

Definition at line 48 of file mc_drv.c.

#define HAS4   (0<<PIND5) | (1<<PINC6) | (0<<PIND7)

Definition at line 49 of file mc_drv.c.

#define HAS5   (1<<PIND5) | (1<<PINC6) | (0<<PIND7)

Definition at line 50 of file mc_drv.c.

#define HAS6   (1<<PIND5) | (0<<PINC6) | (0<<PIND7)

Definition at line 51 of file mc_drv.c.


Function Documentation

void Do_Sensor_Interrupt ( void   ) 

Referenced by mci_run().

Here is the caller graph for this function:

void init ( void   ) 

ports direction configuration, timer 0 configuration, run the PLL, allow interruptions

Definition at line 29 of file init.c.

References Enable_interrupt.

00030 {
00031    /*************************************************************************************/
00032    /*           ports direction configuration                                           */
00033    /*************************************************************************************/
00034 
00035    DDRB = 0xC3;
00036 //   DDRC = 0x89; MC200
00037    DDRC = 0x8A; //MC100
00038    DDRD = 0x01;
00039 //   DDRE = 0x02; // MC200
00040    DDRE = 0x04; // MC100
00041 
00042    PORTC = 0x06;     /* enable pull up */
00043 
00044    /*************************************************************************************/
00045    /*     Timer 0 Configuration : generates the sampling fréquency                      */
00046    /*************************************************************************************/
00047    TCCR0A = (1<<WGM01);   // mode CTC : Clear Timer on Compare
00048    TCCR0B = (1<<CS02);    // f_quartz = 8 MHz / 256 = 32 kHz
00049 //      OCR0A = 0x20;          // one interruption every 1.056ms @8 Mhz
00050 //      OCR0A = 0x10;          // one interruption every 0.544ms @8 Mhz
00051 //      OCR0A = 0x08;          // one interruption every 0.288ms @8 Mhz (0.144ms @16 Mhz)
00052 //   OCR0A = 0x02;          // one interruption every 48µS @16 Mhz
00053    OCR0A = 0x03;          // one interruption every 64µS @16 Mhz
00054 
00055 // !! 48µS is too quick for GCC , please use at least 64µS 
00056 //                                and adjust Prescaler_Main_Tick
00057 //                                in mc_drv.c
00058 
00059 
00060    TIMSK0 = (1<<OCIE0A);  // allow interruption when timer=compare
00061 
00062    init_comparator0();
00063    init_comparator1();
00064    init_comparator2();
00065 
00066 
00067 //    initialize external interrupts /* MC200 */
00068 
00069 //   EICRA =(0<<ISC21)|(1<<ISC20)|(0<<ISC11)|(1<<ISC10)|(0<<ISC01)|(1<<ISC00);
00070 //   EIFR = (1<<INTF2)|(1<<INTF1)|(1<<INTF0); // clear possible IT due to config
00071 //   EIMSK=(0<<INT2)|(1<<INT1)|(1<<INT0);
00072 
00073    Enable_interrupt();            // allow interruptions
00074 }

U16 mc_control_speed_16b ( U16  speed_ref,
U16  speed_measure 
)

speed controller

Returns:
value of speed (duty cycle on 16 bits) speed_measure has 10 bits resolution

Definition at line 35 of file mc_control.c.

References MAX_AMPLITUDE, speed_error, speed_integ, speed_integral, and speed_proportional.

00036 {
00037    U16 result = 0;
00038    S16 increment = 0;
00039 
00040    // Error calculation
00041 //   speed_error = speed_ref - speed_measure/2 ;
00042    speed_error = speed_ref - speed_measure;
00043 
00044    // proportional term calculation : Kp= 7/64=0.1
00045    speed_proportional = speed_error * 4;
00046 
00047    // integral term calculation
00048    speed_integral = speed_integral + speed_error;
00049 
00050    // speed integral saturation
00051    if(speed_integral >  32000) speed_integral =  32000;
00052    if(speed_integral < -32000) speed_integral = -32000;
00053 
00054    speed_integ = (speed_integral - speed_integral/8 + speed_integral/32) / 16 ;
00055 
00056    // amplitude calculation
00057    increment = speed_proportional + speed_integ;
00058 
00059    // saturation of the PI output
00060    if (increment > 0)
00061    {
00062       if  (increment <= (S16)(MAX_AMPLITUDE)) result = (S16)increment ;
00063       else result = MAX_AMPLITUDE ;
00064    }
00065    else result = 0;
00066 
00067    // return Duty Cycle
00068    return result;
00069 }

void PSC_Init ( unsigned int  ot0,
unsigned int  ot1 
)

This function initiliazes the 3 PSC

void PSC_Load ( unsigned int  dt0a,
unsigned int  dt1a,
unsigned int  dt0b,
unsigned int  dt1b,
unsigned int  dt0c,
unsigned int  dt1c 
) [inline]

Load the OCR0SA et OCR0SB registers with new duty cycle values

Parameters:
new duty cycles
Precondition:
all psc are initialized
Returns:
none /

Definition at line 150 of file mc_drv.c.

References HIGH, and LOW.

00153 {
00154    PCNF0 = (1<<PLOCK0)|(1<<PMODE01) | (1<<PMODE00) | (1<<POP0) | (1<<PCLKSEL0);
00155    PCNF1 = (1<<PLOCK1)|(1<<PMODE11) | (1<<PMODE10) | (1<<POP1) | (1<<PCLKSEL1);
00156    PCNF2 = (1<<PLOCK2)|(1<<PMODE21) | (1<<PMODE20) | (1<<POP2) | (1<<PCLKSEL2);
00157 
00158    OCR0SAH = HIGH(dt0a);
00159    OCR0SAL = LOW(dt0a);
00160    OCR0SBH = HIGH(dt1a);
00161    OCR0SBL = LOW(dt1a);
00162 
00163    OCR1SAH = HIGH(dt0b);
00164    OCR1SAL = LOW(dt0b);
00165    OCR1SBH = HIGH(dt1b);
00166    OCR1SBL = LOW(dt1b);
00167 
00168    OCR2SAH = HIGH(dt0c);
00169    OCR2SAL = LOW(dt0c);
00170    OCR2SBH = HIGH(dt1c);
00171    OCR2SBL = LOW(dt1c);
00172 
00173    PCNF0 = (1<<PMODE01) | (1<<PMODE00) | (1<<POP0) | (1<<PCLKSEL0);
00174    PCNF1 = (1<<PMODE11) | (1<<PMODE10) | (1<<POP1) | (1<<PCLKSEL1);
00175    PCNF2 = (1<<PMODE21) | (1<<PMODE20) | (1<<POP2) | (1<<PCLKSEL2);
00176 }

void PSC_Start ( void   ) 

This function starts the PSCs

Parameters:
none 
Precondition:
all psc are initialized
Returns:
none /

Definition at line 184 of file mc_drv.c.

Referenced by mci_run().

00185 {
00186    PCTL2 = (1<<PCCYC2) | (1<<PRUN2) ;
00187 }

Here is the caller graph for this function:

void PSC_Stop ( void   ) 

This function stops the PSCs

Parameters:
none 
Precondition:
none
Returns:
none /

Definition at line 196 of file mc_drv.c.

Referenced by mci_stop().

00197 {
00198    PCTL2 = (1<<PCCYC2);
00199 }

Here is the caller graph for this function:

S16 read_acquisition ( void   ) 

Get the ADC conversion result

Parameters:
none 
Precondition:
none
Returns:
16 bit result /

Definition at line 128 of file adc.c.

References LSB, MSB, and Union16::w.

00129 {
00130    Union16     resultADC ;
00131 
00132    LSB(resultADC) = ADCL ;  // the ADC output has 10 bit resolution
00133    MSB(resultADC) = ADCH ;
00134 
00135    return (resultADC.w) ;
00136 }

void Start_ADC ( void   ) 

Start the ADC conversion

Parameters:
none 
Precondition:
none
Returns:
none /

Definition at line 109 of file adc.c.

References ADC_PRESCALER_16.

00110 {
00111    ADCSRA =   (1<<ADEN)  /* ADC enable          */ \
00112             | (1<<ADSC)  /* start conversion */ \
00113             | (0<<ADATE) \
00114             | (1<<ADIE)  /* interrupt enable    */ \
00115             | (ADC_PRESCALER_16 <<ADPS0) \
00116    ;
00117 
00118 }

void SVPWM ( U16  amp,
U8  Counter_Value,
U8  Top,
U8  Sector 
)


Variable Documentation

volatile U16 Amplitude = 0

voltage to apply

Definition at line 80 of file mc_drv.c.

volatile U8 Delay_For_Change = 1

delay

Definition at line 76 of file mc_drv.c.

volatile U8 Hall_Event_Count = 8

measure the speed on 8 cycles

Definition at line 85 of file mc_drv.c.

volatile U16 Hall_Period = 65535

period of Hall sensor signal

Definition at line 87 of file mc_drv.c.

volatile U8 Hall_Sector = HAS1

current Hall sector

Definition at line 69 of file mc_drv.c.

volatile U8 Main_Tick

Tick signal for main.

Definition at line 51 of file main.c.

Bool mci_direction

User Input parameter to set motor direction.

Definition at line 36 of file mc_interface.c.

Referenced by mci_backward(), mci_forward(), mci_get_direction(), mci_set_direction(), and ushell_task().

volatile Bool mci_run_stop

command for motor

Definition at line 37 of file mc_interface.c.

volatile U16 Measured_Speed

measured speed

Definition at line 82 of file mc_drv.c.

volatile U8 Mem_Hall_Sector = HAS1

future Hall sector

Definition at line 70 of file mc_drv.c.

volatile U16 Mem_Hall_Time = 0

memorisation of Hall event time

Definition at line 86 of file mc_drv.c.

volatile U8 Mem_Top_Counter = 1

memorization at top

Definition at line 74 of file mc_drv.c.

volatile U16 Mem_Top_CounterX8 = 1

for filter

Definition at line 75 of file mc_drv.c.

volatile U16 Permanent_Clock = 0

Definition at line 84 of file mc_drv.c.

volatile U8 Prescaler_Main_Tick

prescaler for Tick

Definition at line 78 of file mc_drv.c.

volatile U16 PWM0

Definition at line 81 of file mc_drv.c.

volatile U16 PWM1

Definition at line 81 of file mc_drv.c.

volatile U16 PWM2

duty cycle

Definition at line 81 of file mc_drv.c.

volatile U8 Sensor_Counter_Down = 1

current counter for svpwm

Definition at line 72 of file mc_drv.c.

volatile U8 Sensor_Counter_Up = 0

measure of the time

Definition at line 71 of file mc_drv.c.

volatile U8 Top_Counter = 1

current top counter

Definition at line 73 of file mc_drv.c.


Generated on Tue Sep 16 18:11:23 2008 for Atmel BLDC Sinusoidal on ATAVRMC100 by  doxygen 1.5.3