| BLDC control on ATAVRMC303 with ATxMega128A1

mc_drv.h
Go to the documentation of this file.
1
11
12 #ifndef _MC_DRV_H_
13 #define _MC_DRV_H_
14
15 // output configuration
16 #define output_disconnected 0x01
17
18 // #define Set_none() (TCCR1A=output_disconnected,TCCR3B=output_disconnected)
19 #define Set_timer_data_register_to_zero() (TCNT0=0x00)
20
21 // MACRO for PSC initialization
22 #define PSC_ONE_RAMP (0<<PMODE01)|(0<<PMODE00)
23 #define PSC_TWO_RAMP (0<<PMODE01)|(1<<PMODE00)
24 #define PSC_FOUR_RAMP (1<<PMODE01)|(0<<PMODE00)
25 #define PSC_CENTERED (1<<PMODE01)|(1<<PMODE00)
26
27 #define RAMP_MODE_NUMBER PSC_CENTERED
28
29 #define SET_PLOCK (1<<PLOCK0)
30 #define RELEASE_PLOCK (0<<PLOCK0)
31
32 #define PRESC_NODIV (0<<PPRE01)|(0<<PPRE00)
33 #define PRESC_DIV_BY_4 (0<<PPRE01)|(1<<PPRE00)
34 #define PRESC_DIV_BY_16 (1<<PPRE01)|(0<<PPRE00)
35 #define PRESC_DIV_BY_64 (1<<PPRE01)|(1<<PPRE00)
36
37 #define OUTPUT_ACTIVE_HIGH (1<<POP0)
38 #define OUTPUT_ACTIVE_LOW (0<<POP0)
39
40 #define Enable_over_current() \
41 PFRC0A = (1<<PELEV0A)|(1<<PFLTE0A)|\
42 (0<<PRFM0A3)|(1<<PRFM0A2)|(1<<PRFM0A1)|(1<<PRFM0A0);
43 #define Disable_over_current() \
44 PFRC0A = (1<<PELEV0A)|(1<<PFLTE0A)|\
45 (0<<PRFM0A3)|(0<<PRFM0A2)|(0<<PRFM0A1)|(0<<PRFM0A0);
46
47 // Comparator interruption
48 #define HALL_A() (PORTE_INT0_vect)
49 // #define HALL_B() (ANACOMP_1_vect)
50 // #define HALL_C() (ANACOMP_2_vect)
51
52
53 #define HALL_SENSOR_VALUE() (PORTE.IN & 0x07)
54
55 #define Clear_Port_Q1() (PORTC.OUTCLR = PIN5_bm)
56 #define Clear_Port_Q3() (PORTC.OUTCLR = PIN3_bm)
57 #define Clear_Port_Q5() (PORTC.OUTCLR = PIN1_bm)
58 #define Clear_Port_Q2() (PORTC.OUTCLR = PIN4_bm)
59 #define Clear_Port_Q4() (PORTC.OUTCLR = PIN2_bm)
60 #define Clear_Port_Q6() (PORTC.OUTCLR = PIN0_bm)
61 #define Set_Port_Q2() (PORTC.OUTSET = PIN4_bm)
62 #define Set_Port_Q4() (PORTC.OUTSET = PIN2_bm)
63 #define Set_Port_Q6() (PORTC.OUTSET = PIN0_bm)
64
65 // Six step commutation
66 #define Set_none() (AWEX_SetOutputOverrideValue(AWEXC,0))
67
68 #define Set_Q1Q4() AWEX_SetOutputOverrideValue(AWEXC,PIN5_bm | PIN2_bm)//(AWEXC.OUTOVEN = (PIN5_bm | PIN2_bm))
69
70 #define Set_Q1Q6() AWEX_SetOutputOverrideValue(AWEXC,PIN5_bm | PIN0_bm)//(AWEXC.OUTOVEN = (PIN5_bm | PIN0_bm))
71
72 #define Set_Q3Q2() AWEX_SetOutputOverrideValue(AWEXC,PIN3_bm | PIN4_bm)//(AWEXC.OUTOVEN = (PIN3_bm | PIN4_bm))
73
74 #define Set_Q3Q6() AWEX_SetOutputOverrideValue(AWEXC,PIN3_bm | PIN0_bm)//(AWEXC.OUTOVEN = (PIN3_bm | PIN0_bm))
75
76 #define Set_Q5Q2() AWEX_SetOutputOverrideValue(AWEXC,PIN1_bm | PIN4_bm)//(AWEXC.OUTOVEN = (PIN1_bm | PIN4_bm))
77
78 #define Set_Q5Q4() AWEX_SetOutputOverrideValue(AWEXC,PIN1_bm | PIN2_bm)//(AWEXC.OUTOVEN = (PIN1_bm | PIN2_bm))
79
80 // Macro for LED use
81 #define switch_ON_LED() (PORTE &= ~(1<<PE2))
82 #define switch_OFF_LED() (PORTE |= (1<<PE2))
83
84 //EXT PORT AS OUTPUT
85 #define Set_EXT1() (PORTB |= (1<<PB3)) // EXT1
86 #define Clear_EXT1() (PORTB &= ~(1<<PB3))
87 #define Set_EXT2() (PORTB |= (1<<PB4)) // EXT2
88 #define Clear_EXT2() (PORTB &= ~(1<<PB4))
89 #define Toggle_EXT2() (PINB |= (1<<PINB4))
90 #define Set_EXT3() (PORTC |= (1<<PC1)) // EXT3
91 #define Clear_EXT3() (PORTC &= ~(1<<PC1))
92 #define Set_EXT4() (PORTC |= (1<<PC2)) // EXT4
93 #define Clear_EXT4() (PORTC &= ~(1<<PC2))
94 #define Set_EXT5() (PORTB |= (1<<PB5)) // EXT5
95 #define Clear_EXT5() (PORTB &= ~(1<<PB5))
96 #define Set_EXT6() (PORTE |= (1<<PE1)) // EXT6
97 #define Clear_EXT6() (PORTE &= ~(1<<PE1))
98 #define Set_EXT7() (PORTD |= (1<<PD3)) // EXT7
99 #define Clear_EXT7() (PORTD &= ~(1<<PD3))
100 #define Set_EXT8() (PORTD |= (1<<PD4)) // EXT8
101 #define Clear_EXT8() (PORTD &= ~(1<<PD4))
102 #define Set_EXT9() (PORTE |= (1<<PE0)) // EXT9
103 #define Clear_EXT9() (PORTE &= ~(1<<PE0))
104 #define Set_EXT10() (PORTD |= (1<<PD2)) // EXT10
105 #define Clear_EXT10() (PORTD &= ~(1<<PD2))
106 //EXT PORT AS INPUT
107 #define Get_EXT1() ((PINB & (1<<PB3))>>PB3) // EXT1
108 #define Get_EXT2() ((PINB & (1<<PB4))>>PB4) // EXT2
109 #define Get_EXT3() ((PINC & (1<<PC1))>>PC1) // EXT3
110 #define Get_EXT4() ((PINC & (1<<PC2))>>PC2) // EXT4
111 #define Get_EXT5() ((PINB & (1<<PB5))>>PB5) // EXT5
112 #define Get_EXT6() ((PINE & (1<<PE1))>>PE1) // EXT6
113 #define Get_EXT7() ((PIND & (1<<PD3))>>PD3) // EXT7
114 #define Get_EXT8() ((PIND & (1<<PD4))>>PD4) // EXT8
115 #define Get_EXT9() ((PINE & (1<<PE0))>>PE0) // EXT9
116 #define Get_EXT10() ((PIND & (1<<PD2))>>PD2) // EXT10
117
118 // ADC scheduler definitions
119 #define CONV_INIT 0
120 #define CONV_POT 1
121 #define CONV_CURRENT 2
122
123 #define FREE 0
124 #define BUSY 1
125 /**************************/
126 /* prototypes declaration */
127 /**************************/
128
129 // Hardware initialization
130 void mc_init_HW(void);
131 void PWM_Init (unsigned int OCRnRB);
132
133 // Phases commutation functions
134 U8 mc_get_hall(void);
135 void mc_duty_cycle(U8 level);
136 void mc_switch_commutation(U8 position);
137
138 // Sampling time configuration
139 void mc_init_timer1(void);
140
141 // Estimation speed
142 void mc_init_timer0(void);
143 void mc_estimation_speed(void);
144
145 // ADC use for current measure and potentiometer...
146 void mc_ADC_Scheduler(void);
147 U8 mc_Get_Current(void);
148 U8 mc_Get_Potentiometer(void);
149
150 // Over Current Detection
151 void mc_set_Over_Current(U8 Level);
152 void mc_disable_during_inrush(void);
153 void mc_inrush_task(void); /* manage the inrush current */
154
155 #endif
mc_set_Over_Current
void mc_set_Over_Current(U8 Level)
Set the Over Current threshold.
Definition: mc_drv.c:415
mc_init_timer0
void mc_init_timer0(void)
Timer 0 Configuration The timer 0 is used to generate an IT when an overflow occurs.
Definition: mc_drv.c:313
mc_inrush_task
void mc_inrush_task(void)
the purpose of this function is to manage the delay used when the overcurrent detection is disabled...
Definition: mc_drv.c:431
mc_init_HW
void mc_init_HW(void)
init HW
Definition: mc_drv.c:95
mc_switch_commutation
void mc_switch_commutation(U8 position)
Set the Switching Commutation value on outputs according to sensor or estimation position.
Definition: mc_drv.c:226
mc_get_hall
U8 mc_get_hall(void)
Get the value of hall sensors (1 to 6)
Definition: mc_drv.c:179
mc_duty_cycle
void mc_duty_cycle(U8 level)
Set the duty cycle values in the PSC according to the value calculate by the regulation loop...
Definition: mc_drv.c:212
mc_estimation_speed
void mc_estimation_speed(void)
estimation speed
Definition: mc_drv.c:342
mc_disable_during_inrush
void mc_disable_during_inrush(void)
the purpose of this function is to disable the overcurrent detection during startup (inrush current...
Definition: mc_drv.c:422
mc_init_timer1
void mc_init_timer1(void)
timer 1 Configuration Use to generate a 250us activation for sampling speed regulation ...
Definition: mc_drv.c:283
PWM_Init
void PWM_Init(unsigned int OCRnRB)
Initialization of PWM generator PSC0.
Definition: mc_drv.c:153
mc_Get_Potentiometer
U8 mc_Get_Potentiometer(void)
mc_ADC_Scheduler
void mc_ADC_Scheduler(void)
Launch the scheduler for the ADC.
Definition: mc_drv.c:388
mc_Get_Current
U8 mc_Get_Current(void)