Go to the source code of this file.
Functions | |
| void | ADC_Init (void) |
| void | ADC_start_conv (void) |
| U16 | get_Hall_Period (void) |
| void | init (void) |
| int | main (void) |
| U16 | mc_control_speed_16b (U16 speed_ref, U16 speed_measure) |
| void | PSC_Init (unsigned int ot0, unsigned int ot1) |
| S16 | read_acquisition (void) |
| void | Start_ADC (void) |
| void | store_new_amplitude (U16 new_val) |
| void | ushell_task (void) |
| void | ushell_task_init (void) |
Variables | |
| volatile U8 | Flag_IT_ADC |
| volatile U8 | Main_Tick = 0 |
| Periodic clock to generate. | |
| volatile Bool | mci_run_stop |
| User Input parameter to launch or stop the motor. | |
| volatile U16 | Measured_Speed |
| measured speed | |
| volatile S16 | Ref_Speed = 0 |
| Reference speed from user. | |
| volatile S16 | User_Speed |
| User Input parameter to set motor speed. | |
| void ADC_Init | ( | void | ) |
Initialisation of the ADC
| none |
Definition at line 44 of file adc.c.
References ADC_INPUT_AMP1, ADC_PRESCALER_16, ADC_TRIG_SRC_FREE_RUNNING, ADC_VREF_INTERNAL, AMP_GAIN_5, and AMP_TRIG_ON_PSC0.
00045 { 00046 00047 /* Digital Input Disable Register 0 */ 00048 DIDR0 = (0<<ADC7D) \ 00049 | (0<<ADC6D ) \ 00050 | (0<<ADC5D ) \ 00051 | (0<<ADC4D ) \ 00052 | (0<<ADC3D ) \ 00053 | (0<<ADC2D ) \ 00054 | (0<<ADC1D ) \ 00055 | (0<<ADC0D ) \ 00056 ; 00057 00058 /* Digital Input Disable Register 1 */ 00059 DIDR1 = (0<<ACMP0D) \ 00060 | (1<<AMP0PD) /* disable digital input on AMP 0 */ \ 00061 | (1<<AMP0ND) /* disable digital input on AMP 0 */ \ 00062 | (0<<ADC10D) \ 00063 | (1<<ADC9D) \ 00064 | (1<<ADC8D) 00065 ; 00066 00067 /* init amplifier 0 */ 00068 AMP0CSR = (1 <<AMP0EN) /* Amplifier enable */ \ 00069 | (0 <<AMP0IS) \ 00070 | (AMP_GAIN_5 <<AMP0G0) \ 00071 | (AMP_TRIG_ON_PSC0 <<AMP0TS0) \ 00072 ; 00073 00074 /* init amplifier 1 */ 00075 AMP1CSR = (1 <<AMP1EN) /* Amplifier enable */ \ 00076 | (0 <<AMP1IS) \ 00077 | (AMP_GAIN_5 <<AMP1G0) \ 00078 | (AMP_TRIG_ON_PSC0 <<AMP1TS0) \ 00079 ; 00080 00081 /* init ADC */ 00082 ADMUX = (ADC_VREF_INTERNAL <<REFS0) \ 00083 | (0 <<ADLAR) \ 00084 | (ADC_INPUT_AMP1 <<MUX0) /* select input */ \ 00085 ; 00086 00087 ADCSRB = (1<<ADHSM) /* High Speed Mode */ \ 00088 | (0<<ADASCR) /* not used on AT90PWM3B */ \ 00089 | (ADC_TRIG_SRC_FREE_RUNNING <<ADTS0) \ 00090 ; 00091 00092 ADCSRA = (1<<ADEN) /* ADC enable */ \ 00093 | (0<<ADSC) \ 00094 | (0<<ADATE) \ 00095 | (1<<ADIE) /* interrupt enable */ \ 00096 | (ADC_PRESCALER_16 <<ADPS0) \ 00097 ; 00098 00099 }
| void ADC_start_conv | ( | void | ) |
| void init | ( | void | ) |
ports direction configuration, timer 0 configuration, run the PLL, allow interruptions
Definition at line 29 of file init.c.
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 }
| int main | ( | void | ) |
Definition at line 60 of file main.c.
References ADC_Init(), Clear_PE1, Flag_IT_ADC, get_Hall_Period(), init(), K_SPEED, Main_Tick, MAX_PWM, mc_control_speed_16b(), mci_run_stop, mci_store_measured_current(), mci_store_measured_speed(), Measured_Speed, PSC_Init(), read_acquisition(), Ref_Speed, RUN, store_new_amplitude(), User_Speed, ushell_task(), and ushell_task_init().
00061 { 00062 00063 init(); 00064 00065 ADC_Init(); 00066 00067 ushell_task_init(); 00068 00069 PSC_Init(2, MAX_PWM); 00070 00071 // PSC_Start(); /* for the first time MC100*/ 00072 00073 00074 while(1) 00075 { 00076 00077 if (Main_Tick) 00078 { 00079 Main_Tick=0; 00080 // Start_ADC(); 00081 00082 if (mci_run_stop == RUN) 00083 { 00084 Ref_Speed = User_Speed; 00085 } 00086 else 00087 { 00088 Ref_Speed = 0; 00089 } 00090 00091 Measured_Speed = (U16)K_SPEED / get_Hall_Period(); 00092 mci_store_measured_speed((U8)Measured_Speed); 00093 00094 #if (REGULATION_MODE == 2) 00095 store_new_amplitude( mc_control_speed_16b((U16) Ref_Speed , (U16) Measured_Speed) ); 00096 #else 00097 if (mci_run_stop == RUN) 00098 { 00099 00100 // store_new_amplitude( AMPLITUDE_IN_OPEN_LOOP ); 00101 store_new_amplitude( Ref_Speed ); 00102 } 00103 else 00104 { 00105 store_new_amplitude( 0 ); 00106 } 00107 #endif 00108 ushell_task(); 00109 } 00110 00111 if (Flag_IT_ADC) 00112 { 00113 00114 mci_store_measured_current(read_acquisition()); 00115 00116 Flag_IT_ADC=0; 00117 } /* end of Flag_IT_ADC */ 00118 00119 #ifdef MC200 00120 /* test the overcurrent input MC200 */ 00121 if (( PIFR0 & (1<<PEV0A)) !=0 ) 00122 { 00123 /* fault on overcurrent */ 00124 Set_PC3(); /* switch on the overcurrent led */ 00125 Clear_PE1(); /* switch off the run/stop led */ 00126 while (1) ; /* infinite loop */ 00127 } 00128 #endif 00129 00130 } /* end of while(1) */ 00131 } /* end of main */

speed controller
Definition at line 35 of file mc_control.c.
Referenced by main().
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
| none |
Definition at line 103 of file mc_drv.c.
Referenced by main().
00104 { 00105 Start_pll_64_mega(); // start the PLL at 64 MHz 00106 Wait_pll_ready(); 00107 00108 OCR0RAH = HIGH(ot0); 00109 OCR0RAL = LOW(ot0); 00110 OCR0RBH = HIGH(ot1); 00111 OCR0RBL = LOW(ot1); 00112 PCNF0 = (1<<PMODE01) | (1<<PMODE00) | (1<<POP0) | (1<<PCLKSEL0) ; /* fast clock input used */ 00113 PFRC0A = 0; 00114 // PFRC0A = (1<<PFLTE0A)|(0<<PRFM0A3)|(1<<PRFM0A2)|(1<<PRFM0A1)|(1<<PRFM0A0); /* overcurrent */ 00115 PFRC0B = 0; 00116 PCTL0 = (1<<PARUN0) | (1<<PCCYC0); 00117 00118 OCR1RAH = HIGH(ot0); 00119 OCR1RAL = LOW(ot0); 00120 OCR1RBH = HIGH(ot1); 00121 OCR1RBL = LOW(ot1); 00122 PCNF1 = (1<<PMODE11) | (1<<PMODE10) | (1<<POP1) | (1<<PCLKSEL1); /* fast clock input used */ 00123 PFRC1A = 0; 00124 PFRC1B = 0; 00125 PCTL1 = (1<<PARUN1) | (1<<PCCYC1); 00126 00127 OCR2RAH = HIGH(ot0); 00128 OCR2RAL = LOW(ot0); 00129 OCR2RBH = HIGH(ot1); 00130 OCR2RBL = LOW(ot1); 00131 PCNF2 = (1<<PMODE21) | (1<<PMODE20) | (1<<POP2) | (1<<PCLKSEL2); /* fast clock input used */ 00132 PFRC2A = 0; 00133 PFRC2B = 0; 00134 // PCTL2 = (1<<PCCYC2) | (1<<PRUN2) ; 00135 00136 // connect the PSC waveform generator outputs to the port outputs 00137 PSOC0 = (1<<POEN0B) | (1<<POEN0A) ; 00138 PSOC1 = (1<<POEN1B) | (1<<POEN1A) ; 00139 PSOC2 = (1<<POEN2B) | (1<<POEN2A) ; 00140 00141 }

| S16 read_acquisition | ( | void | ) |
Get the ADC conversion result
| none |
Definition at line 128 of file adc.c.
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
| none |
Definition at line 109 of file adc.c.
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 store_new_amplitude | ( | U16 | new_val | ) |
| void ushell_task | ( | void | ) |
Entry point of the explorer task management This function links perform ushell task decoding to access file system functions.
| none |
Definition at line 99 of file ushell_task.c.
Referenced by main().
00100 { 00101 U8 status = 0; 00102 00103 if(cmd==FALSE) 00104 { 00105 build_cmd(); 00106 } 00107 else 00108 { 00109 switch (cmd_type) 00110 { 00111 case CMD_HELP: 00112 print_msg(msg_help); 00113 break; 00114 case CMD_RUN: 00115 mci_run(); 00116 break; 00117 case CMD_STOP: 00118 mci_stop(); 00119 break; 00120 case CMD_FORWARD: 00121 mci_forward(); 00122 break; 00123 case CMD_BACKWARD: 00124 mci_backward(); 00125 break; 00126 case CMD_SET_SPEED: 00127 convert_param1(); 00128 mci_set_speed(param1); 00129 break; 00130 case CMD_GET_ID: 00131 print_hex16(BOARD_ID); 00132 putchar(' '); 00133 print_hex16(SOFT_ID); 00134 putchar(' '); 00135 print_hex16(REV_ID); 00136 break; 00137 case CMD_GET_STATUS0: 00138 status = (mci_direction<<3)|(mci_run_stop<<2); 00139 print_hex16(status); 00140 putchar(' '); 00141 print_hex16(mci_get_measured_speed()); 00142 putchar(' '); 00143 print_hex16(mci_get_measured_current()); 00144 break; 00145 case CMD_GET_STATUS1: 00146 print_hex16(0xDEA); 00147 putchar(' '); 00148 print_hex16(0x123); 00149 break; 00150 default: //Unknown command 00151 print_msg(msg_er_cmd_not_found); 00152 } 00153 cmd_type=CMD_NONE; 00154 cmd=FALSE; 00155 print_msg(msg_prompt); 00156 } 00157 }

| void ushell_task_init | ( | void | ) |
This function initializes the hardware/software ressources required for ushell task.
| none |
Definition at line 73 of file ushell_task.c.
Referenced by main().
00074 { 00075 uart_init(); 00076 #ifdef __GNUC__ 00077 fdevopen((int (*)(char, FILE*))(uart_putchar),(int (*)(FILE*))uart_getchar); //for printf redirection 00078 #endif 00079 print_msg(msg_welcome); 00080 print_msg(msg_prompt); 00081 cmd=FALSE; 00082 cmd_type=CMD_NONE; 00083 00084 00085 }

| volatile U8 Flag_IT_ADC |
| volatile Bool mci_run_stop |
User Input parameter to launch or stop the motor.
Definition at line 37 of file mc_interface.c.
Referenced by main(), mci_motor_is_running(), mci_run(), mci_stop(), and ushell_task().
| volatile U16 Measured_Speed |
| volatile S16 User_Speed |
User Input parameter to set motor speed.
Definition at line 38 of file mc_interface.c.
Referenced by main(), and mci_set_speed().
1.5.3